环境配置

说明配置问题前得先提一下Tex,Latex,Texlive的关系——Texlive实际就是Tex的一种发行版,可以类比成编译器、而Latex可以简单理解成一种排版语言,用latex写好文章之后交给Texlive编译就可以得到对应的pdf,很多资料中提到的Texstudio实际上就只是一个专门写latex的编辑器就像pycharm专门写python一样。我的主力编辑器是sublime,本着降低学习成本的原则,就不装texstudio了,直接在sublime里面配好latex的环境。

要想在sublime中使用latex得下载:

  • Texlive(编译器),现在转到用Miktex编译器了,简洁很多
  • Sumatra(极简版pdf阅读器)
  • LaTeXTools(sl的插件):核心解决的是编辑-编译-预览-调试工作流,使得sublime可以去调用latex的编译器、实时预览编译结果并双向同步(使用sumatra这个软件作为pdf预览的后端,sublime是不直接支持pdf预览的)、提供latex的语法高亮、以及latex中常用环境的snippet。
  • Latex-cwl(sl的插件):提供超全、精准、带参数提示的命令和环境补全功能。

然后需要设置下laTexTools的相关配置:

配置pdf预览后端
"windows": { // Path used when invoking tex & friends; "" is fine for MiKTeX // For TeXlive 2011 (or other years) use // "texpath" : "C:\\texlive\\2011\\bin\\win32;$PATH", "texpath" : "D:\\texlive\\2017\\bin\\win32;$PATH", // TeX distro: "miktex" or "texlive" "distro" : "texlive", // Command to invoke Sumatra. If blank, "SumatraPDF.exe" is used (it has to be on your PATH,注意改成自己的地址) "sumatra": "D:\\Program Files\\SumatraPDF\\SumatraPDF.exe", // Command to invoke Sublime Text. Used if the keep_focus toggle is true. // If blank, "subl.exe" or "sublime_text.exe" will be used. "sublime_executable": "", // how long (in seconds) to wait after the jump_to_pdf command completes // before switching focus back to Sublime Text. This may need to be // adjusted depending on your machine and configuration. "keep_focus_delay": 0.5 },
latex相关快捷键
// 给latex环境绑定的快捷键 跳转到pdf、查看目录(实际上用sumlime自带的ctrl+r也行) { "keys": ["ctrl+1"], "context": [ {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}], "command": "latextools_jumpto_pdf", "args": {"from_keybinding": true}}, { "keys": ["ctrl+2"], "args": {"only_current_file": true}, "context": [ {"key": "selector", "operator": "equal", "operand": "text.tex"}], "command": "latextools_toc_quickpanel"},

然后来添加正向索引(从tex文件索引到pdf对应位置)和反向索引(从pdf对应位置索引到tex文件对应位置)的功能:

  1. 把sumatra添加到环境变量(切换为自己相应的位置),让系统能找到它。
    添加环境变量
    D:\Program Files\SumatraPDF
  2. 在cmd中输入如下命令
    配置反向搜索的命令
    sumatrapdf.exe -inverse-search "\"C:\Program Files\Sublime Text\sublime_text.exe\" \"%f:%l\"
  3. 最后在sumatra的设置-选项中将双击时执行的命令一栏设置为
    "C:\Program Files\Sublime Text\sublime_text.exe" "%f:%l"

至此,就成功在sublime上搭建好latex的环境了!

latex排版入门

关于latex的包管理

和python一样,基础的latex提供有限的功能,想要更多的功能必须使用第三方包,如果下载的式texlive,那么基本上大部分包都已经安装到电脑上了。如果需要对包进行管理,可以使用texlive提供的tlmgr(但这种方式老是报错),下面就介绍怎么手动安装自己需要的latex宏包(\documentcalss{}中写的文档模板也是一样的操作)。就我的电脑而言,所有的宏包都放在C:/texlive/2017/texmf-dist/tex/latex这个路径下边。要新增宏包,就在这个目录下新建一个同名文件夹然后把对应的包文件(如后缀为sty,cls的文件)放到这个目录下,最后以管理员身份在命令行中输入texhash 命令刷新,就可以在tex文件中导入你新增的宏包了。

想要什么包都可以去 https://ctan.org/pkg 这个网站下载相应的sty文件。

关于latex的模板

latex强就强在可以非常快速的套别人做好的模板,例如https://www.overleaf.com/latex/templates? 这个网站基本上可以搜索到所有期刊提供写作模板,下载下来,放到C:/texlive/2017/texmf-dist/tex/latex这个路径下边,就可以开始愉快的套模板了。下面以trasportaion science提供的模板为例:
(1) 去上面提到的网站找模板,复制下来。
Fig1. 去网站上找模板
  1. 然后把复制内容写道tex文件里编译,这时会提醒你缺失了informs3.cls模板文件。因为上面的trasportaion science模板用了这个模板文件,所以我们必须下载它。上github找这个模板文件,然后放到C:/texlive/2017/texmf-dist/tex/latex这个路径下边(别忘了管理员模式下texhash下),最后就大功告成了。
Fig2. 编译看看缺了inform3.cls文件
Fig3. 去网上找inform3.cls文件然后放到本地
Fig4. 再次编译,成功运行

关于latex语法

这块儿目前还没有很迫切的需求去详细学习,计划在使用别人做好的模板的过程中遇到问题再补充,就先略过了。

一个tex文件的例子
% document环境之前的部分都叫做序言 % 定义了文档类(类型)以及诸如编写文档时使用的语言等细节,加载您想要使用的包 \documentclass[12pt, letterpaper]{article} % 定义文章类型是article,并在[]中传递附加参数 \title{My first LaTeX document} % 序言中的内容不会显示在编译好的pdf中,这里只是在设置题目、作者和日期信息,想在正文的什么位置显示就得在正文使用\maketitle命令 \author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}} \date{August 2022} \usepackage{graphicx} % 支持latex中显示图像的包 \usepackage{amsmath} % 提供了额外的公式环境 \graphicspath{{images/}} % 配置图像放在当前文件夹的images目录下,下面导入图片的地址就可以用基于images的相对路径了 % document环境之间的部分叫做正文 \begin{document} \maketitle % 添加标题信息的命令 \tableofcontents % 添加目录的命令 \begin{abstract} % 使用abstract环境排版摘要 This is a simple paragraph at the beginning of the document. A brief introduction about the main subject. \end{abstract} % 分段通过空行实现,和markdown一样,换行通过\\或\newline实现,建议新用户不要使用多个\\ 或 \换行符来“模拟”段落之间有较大间距,因为这会干扰 LaTeX 的排版算法。推荐的方法是继续使用空白行来创建新段落 \section{Test section} \subsection{Test subsection} After our abstract we can begin the first paragraph, then press ``enter'' twice to start the second one. This line will start a second paragraph. I will start the third paragraph and then add \\ a manual line break which causes this text to start on a new line but remains part of the same paragraph. Alternatively, I can use the \verb|\newline|\newline command to start a new line, which is also part of the same paragraph. First document. This is a simple example, with no extra parameters or packages included. Some of the \textbf{greatest} discoveries in \underline{science} were made by \textbf{\textit{accident}}. % The \includegraphics command is % provided (implemented) by the % graphicx package \begin{figure}[h] \centering \includegraphics[width=0.35\textwidth]{testpic} % 图像宽度设置为文本宽度的35% \caption{The emblem of Dalian Maritime University.} % 图像说明 \label{fig:testpic} % 设置图像标签,课配合\ref{fig:testpic}命令引用这个图 \end{figure} % Although the full file name, including its extension, is allowed in the \includegraphics command, it’s considered best practice to omit the file extension because it will prompt LaTeX to search for all the supported formats. % Unordered lists are produced by the itemize environment. Each list entry must be preceded by the \item command \begin{itemize} \item The individual entries are indicated with a black dot, a so-called bullet. \item The text in the entries may be of any length. \end{itemize} % Ordered lists use the same syntax as unordered lists but are created using the enumerate environment \begin{enumerate} \item This is the first entry in our list. \item The list numbers increase with each entry we add. \end{enumerate} % 内联数学公式 In physics, the mass-energy equivalence is stated by the equation $E=mc^2$, discovered in 1905 by Albert Einstein. % 显示数学公式To typeset display-mode math you can use one of these delimiter pairs: \[ ... \], \begin{displaymath} ... \end{displaymath} or \begin{equation} ... \end{equation}. Historically, typesetting display-mode math required use of $$ characters delimiters, as in $$ ... display math here ...$$, but this method is no longer recommended: use LaTeX’s delimiters \[ ... \] instead. The mass-energy equivalence is described by the famous equation \[ E=mc^2 \] discovered in 1905 by Albert Einstein. In natural units ($c = 1$), the formula expresses the identity. Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined and nested to write expressions such as \[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \] We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscripts and subscripts: \[ \int_0^1 \frac{dx}{e^x} = \frac{e-1}{e} \] Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are written as $\Omega$ $\Delta$. Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc. % asmath提供的多行公式环境align和数组环境array \begin{align} y & = x \\ y & = 3x^2 + 5x + 2 \end{align} % 在latex中只用自带的tabular环境创建表 \begin{table} \begin{center} \begin{tabular}{|c |l| r|} % clr表示表格有三列,分别居中、左对齐和右对齐,|表示在相应列添加竖线 \hline cell1 & cell2 & cell3 \\ cell4 & cell5 & cell6 \\ cell7 & cell8 & cell9 \\ \hline \end{tabular} \end{center} \caption{Table to test captions and labels.} \label{table:data} \end{table} \end{document}

不得不说,latex排版确实是比word排版看着舒服。

参考

  1. 全网最全最细的LaTeX排版教程
  2. overleaf——30分钟入门latex排版
  3. latex基础语法教程(github)
  4. CTAN官网