用markdown和latex写论文

前言

我是用vnote写好论文的markdown初稿。vnote是专门为程序员开发的markdown编辑器,其除了支持常规的mermaid.js、latex数学公式外可以支持plantuml(可以方便的画uml图)。当然普通人可以使用typora作为markdown编辑器。markdown初稿的主要是用于完成大致的文章内容。

在编写好markdown初稿后,使用pandoc将其转换成latex格式,以便套用latex模板。pandoc MANUAL

正文

markdown转成latex

我平时用的转换命令如下:

pandoc  --top-level-division=chapter --natbib --columns=38 -o xxx.tex xxx.md

pandoc -s可以让输出的文件自成一体,即输出的文件自带一些辅助用的配置项和宏之类的。部分辅助配置也是重要的

% \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
% \PassOptionsToPackage{hyphens}{url}
%
% \documentclass[]{book}
% \usepackage{lmodern}
% \usepackage{amssymb,amsmath}
% \usepackage{ifxetex,ifluatex}
% \usepackage{fixltx2e} % provides \textsubscript
% \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
%   \usepackage[T1]{fontenc}
%   \usepackage[utf8]{inputenc}
%   \usepackage{textcomp} % provides euro and other symbols
% \else % if luatex or xelatex
%   \usepackage{unicode-math}
%   \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
% \fi

% % use upquote if available, for straight quotes in verbatim environments
% \IfFileExists{upquote.sty}{\usepackage{upquote}}{}

% % use microtype if available
% \IfFileExists{microtype.sty}{%
% \usepackage[]{microtype}
% \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
% }{}

% \IfFileExists{parskip.sty}{%
% \usepackage{parskip}
% }{% else
% \setlength{\parindent}{0pt}
% \setlength{\parskip}{6pt plus 2pt minus 1pt}
% }

% \usepackage{hyperref}
% \hypersetup{
%             pdfborder={0 0 0},
%             breaklinks=true}
% \urlstyle{same}  % don't use monospace font for urls

\usepackage{color}
\usepackage{fancyvrb}
% \newcommand{\VerbBar}{|}
% \newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
% \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line

% \newenvironment{Shaded}{}{}

% \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
% \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
% \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}}
% \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
% \newcommand{\BuiltInTok}[1]{#1}
% \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
% \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{#1}}}
% \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
% \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}}
% \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
% \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{#1}}
% \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
% \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}}
% \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
% \newcommand{\ExtensionTok}[1]{#1}
% \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
% \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{#1}}
% \newcommand{\ImportTok}[1]{#1}
% \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
% \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
% \newcommand{\NormalTok}[1]{#1}
% \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
% \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{#1}}
% \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}}
% \newcommand{\RegionMarkerTok}[1]{#1}
% \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
% \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}}
% \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
% \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}}
% \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
% \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}

\usepackage{longtable,booktabs}
% Fix footnotes in tables (requires footnote package)
\IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{longtable}}{}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother


% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\emergencystretch}{3em}  % prevent overfull lines

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% \setcounter{secnumdepth}{0}

% % Redefines (sub)paragraphs to behave more like sections
% \ifx\paragraph\undefined\else
% \let\oldparagraph\paragraph
% \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
% \fi
% \ifx\subparagraph\undefined\else
% \let\oldsubparagraph\subparagraph
% \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
% \fi

% % set default figure placement to htbp
% \makeatletter
% \def\fps@figure{htbp}
% \makeatother

图片

可以在markdown中引入图片

![XXX](xxx.pdf){width=100% #fig:xxid}

论文通常要引用图片,我这里直接用的latex的语法

\ref{fig:xxid}

另外如何用Markdown写论文?中说{@fig:id}也可以,不过我尝试后,没有成功的得到相应的latex语句。

表格

给表格起名字,在markdown的表格后面的紧贴着的一行输入

Table: xxxtablename

但是似乎没有给表格添加label以供引用的命令,需要自己手动在生成的.tex文件中的\caption{xxxtablename}后添加\lable{tab:xxxtableid}以后其他引用。

引用表格和引用图片相似,我是在markdown中直接用latex的语法:

\ref{tab:xxxtableid}

参考文献

使用.bib文件作为参考文献存放处,可以在markdown中[@xxx]引用参考文献。

latex需要编译四次,才能正确的编号参考文献等。

uml

之前在vnote里画plantuml和画mermaid里的图类似

    ```puml
    @startuml

    xxx

    @enduml
    ```

但是在调用pandoc转换成latex后,其会被当做verbatim环境。

有两种方法可以在latex渲染出plantuml

  1. 将plantuml语句单独拿出来成文件,调用相应的jar,生成图片(png、eps等文件),然后按照引入图片的形式引入。
  2. 使用plantuml.sty。其提供在latex里渲染plantuml代码的能力。需要注意的是其要求plantuml环境。以及如何安装.sty(我是将plantuml.sty文件放入 C:\texlive\texmf-local\tex\latex\local\plantuml文件夹)

ps: 第2中方法可能无法运行plantuml命令

方法一:
我把作者的

\newcommand{\plantuml@cmd}{plantuml}

换成

\newcommand{\plantuml@cmd}{java -jar C:\plantuml.jar -charset UTF-8}

就可以了。最后一个{}里是关于plantuml程序的命令行启动命令,你根据自己的实际情况调整吧。

方法二:
在shell里定义命令plantuml,使其可以启动plantuml程序。

另外,作者脚本里默认输出png格式的,如果想改为eps格式的话,
将作者的

\newcommand{\plantuml@parm}{-tpng}

改为

\newcommand{\plantuml@parm}{-teps}

以及将作者的

\newcommand{\plantuml@outext}{png}

改为

\newcommand{\plantuml@outext}{eps}

即可。

posted @ 2020-08-20 18:52  Tifa_Best  阅读(995)  评论(0编辑  收藏  举报