用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 	extsubscript
% ifnum 0ifxetex 1fiifluatex 1fi=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}
% 
ewcommand{VerbBar}{|}
% 
ewcommand{VERB}{Verb[commandchars=\{}]}
% DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\{}}
% Add ',fontsize=small' for more characters per line

% 
ewenvironment{Shaded}{}{}

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

usepackage{longtable,booktabs}
% Fix footnotes in tables (requires footnote package)
IfFileExists{footnote.sty}{usepackage{footnote}makesavenoteenv{longtable}}{}
usepackage{graphicx,grffile}
makeatletter
defmaxwidth{ifdimGin@nat@width>linewidthlinewidthelseGin@nat@widthfi}
defmaxheight{ifdimGin@nat@height>	extheight	extheightelseGin@nat@heightfi}
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{	ightlist}{%
  setlength{itemsep}{0pt}setlength{parskip}{0pt}}

% setcounter{secnumdepth}{0}

% % Redefines (sub)paragraphs to behave more like sections
% ifxparagraphundefinedelse
% letoldparagraphparagraph
% 
enewcommand{paragraph}[1]{oldparagraph{#1}mbox{}}
% fi
% ifxsubparagraphundefinedelse
% letoldsubparagraphsubparagraph
% 
enewcommand{subparagraph}[1]{oldsubparagraph{#1}mbox{}}
% fi

% % set default figure placement to htbp
% makeatletter
% deffps@figure{htbp}
% makeatother

图片

可以在markdown中引入图片

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

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


ef{fig:xxid}

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

表格

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

Table: xxxtablename

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

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


ef{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: exlive exmf-local exlatexlocalplantuml文件夹)

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

方法一:
我把作者的


ewcommand{plantuml@cmd}{plantuml}

换成


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

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

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

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


ewcommand{plantuml@parm}{-tpng}

改为


ewcommand{plantuml@parm}{-teps}

以及将作者的


ewcommand{plantuml@outext}{png}

改为


ewcommand{plantuml@outext}{eps}

即可。

原文地址:https://www.cnblogs.com/Tifa-Best/p/13537293.html