【Cheatsheet】LaTeX的常用代码、常见问题解决

2023-10-12 22:40

本文主要是介绍【Cheatsheet】LaTeX的常用代码、常见问题解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1. 前言
  • 2. 环境
  • 3. Preamble
  • 4. 文档属性设置
    • 4.1. 文档字体
    • 4.2. 文档的页面大小,边距(margin)
  • 5. 正文处理
    • 5.1. 缩进
      • 5.1.1. 段落缩进
      • 5.1.2. 段落无缩进
    • 5.2. 段落间距
    • 5.3. 章节的组织:chapter,section
  • 6. 参考文献
    • 6.1. 【问题解决】IEEETrans balance reference (平衡参考文献)
      • 6.1.1. 方法1
      • 6.1.2. 方法2
  • 7. 表格
    • 7.1. 单个cell居中
    • 7.2. threeparttable模板
      • 7.2.1. 插入tablenotes
      • 7.2.2. tablenotes 和 table 对齐
  • 8. latexdiff的使用
  • 9. Box的使用
    • 9.1. tcolorbox
    • 9.2. tcolorbox 进阶使用
  • 10. bibliography参考文献
    • 10.1. 添加url bib(我的artifact)
  • 11. 中文文档操作
    • 11.1. 使用中文、设置字体
    • 11.2. highlight中文文本且自动换行
  • 12. 公式相关
    • 12.1. 取消公式的自动编码
    • 12.2. 将latex的公式转换到word里面!(以及打出中文公式)
  • 13. enumerate
    • 13.1. 编号格式、取消缩进、调整间距
  • 14. 注释
  • 15. url 超链接
    • 15.1. 去除frame,以及修改颜色
  • 16. xcolor 颜色的使用
    • 16.1. 报错:Option clash for package xcolor
  • 17. 代码高亮 minted
  • 18. 算法 Algorithm 伪代码
  • 19. 小结
  • 20. 参考文献

1. 前言

创作开始时间:2021年11月23日14:50:41

用LaTeX很久了,感觉经常会反复用到一些操作,有时候记不住,所以在这里写一个cheatsheet,记录一些常用的操作。

2. 环境

  • Overleaf

3. Preamble

把preamble单独放到一个sty文件中,然后\usepackage。

  • Place preamble packages in a different tex file https://tex.stackexchange.com/questions/339923/place-preamble-packages-in-a-different-tex-file

4. 文档属性设置

4.1. 文档字体

\documentclass[12pt]{article}\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default

这种字体还挺好看的。

参考:

  • 我之前的overleaf项目
  • How to change the font size of a document? [duplicate] https://tex.stackexchange.com/questions/10619/how-to-change-the-font-size-of-a-document

4.2. 文档的页面大小,边距(margin)

\documentclass[12pt,english]{article}\usepackage[a4paper,bindingoffset=0.2in,left=1in,right=1in,top=1in,bottom=1in,footskip=.25in]{geometry}

自己调一下就行。

参考:

  • Setting exact margins https://tex.stackexchange.com/questions/46175/setting-exact-margins

5. 正文处理

5.1. 缩进

5.1.1. 段落缩进

\setlength{\parindent}{0em}
\setlength{\parskip}{1ex}

In LaTeX standard class documents, the default value for \parindent in one-column documents is 15pt when the default text size is 10pt, 17pt for 11pt, and 1.5em for 12pt. In two-column documents it is 1em. (These values are set before LaTeX calls \normalfont so em is derived from the default font, Computer Modern. If you use a different font then to set \parindent to 1em matching that font, put \AtBeginDocument{\setlength{\parindent}{1em}} in the preamble.)

  • https://latexref.xyz/_005cparindent-_0026-_005cparskip.html

5.1.2. 段落无缩进

\setlength{\parindent}{0em}

5.2. 段落间距

\setlength{\parskip}{1em}

参考:

  • Paragraph formatting - Overleaf, Online LaTeX Editor

5.3. 章节的组织:chapter,section

在这里插入图片描述

  • Sections and chapters https://www.overleaf.com/learn/latex/Sections_and_chapters

6. 参考文献

6.1. 【问题解决】IEEETrans balance reference (平衡参考文献)

6.1.1. 方法1

有时候参考文献是不平衡的。如图:右边一片空白。

在这里插入图片描述

我的项目的相关代码:

\documentclass[10pt,journal,compsoc]{IEEEtran}xxx (省略)\bibliographystyle{IEEEtran}
\bibliography{mybib}

改成以下:

\documentclass[10pt,journal,compsoc]{IEEEtran}xxx (省略)\bibliographystyle{IEEEtran}
% 添加下面两行
\IEEEtriggercmd{\enlargethispage{-5.31in}}
\IEEEtriggeratref{50}
\bibliography{mybib}

注意:50 代表参考文献的编号。-5.31代表间距的调整。这些都需要自己去按照具体的参考文献数目情况进行调整。

我调整之后得到下图:(已经balance了)

在这里插入图片描述

备注:
在IEEEtran 模板下面使用balance这个package不管用,所以才用的上面的方法。

参考:

  • Even columns in document with long bibliography https://tex.stackexchange.com/questions/102754/even-columns-in-document-with-long-bibliography
  • Balancing the Two Columns of Text on the Last Page https://cs.brown.edu/about/system/managed/latex/doc/balance.pdf
  • How can I balance columns in the last page of a bibliography section? https://tex.stackexchange.com/questions/38430/how-can-i-balance-columns-in-the-last-page-of-a-bibliography-section/273076
  • How to level columns in bibliography? https://tex.stackexchange.com/questions/49918/how-to-level-columns-in-bibliography

6.1.2. 方法2

有时候上面这招也不顶用,那么就用这个:

\bibliographystyle{IEEEtran} %IEEEtran, plain
% \IEEEtriggercmd{\enlargethispage{-0.4in}}
% 只插入\IEEEtriggeratref{118} 的指令,在编号为118的文献前面加一个column break
\IEEEtriggeratref{118}
\bibliography{mybib}

在这里插入图片描述
参考:

  • Even columns in document with long bibliography https://tex.stackexchange.com/questions/102754/even-columns-in-document-with-long-bibliography

7. 表格

7.1. 单个cell居中

\multicolumn{1}{|r|}{Item3}
  • Change align of a single cell in tabular [duplicate] https://tex.stackexchange.com/questions/47675/change-align-of-a-single-cell-in-tabular

7.2. threeparttable模板

7.2.1. 插入tablenotes

如下:
1)添加 \tnote{*}
2)添加 \footnotesize\item[*]

\setcounter{magicrownumbers}{0} 
\begin{table*}[t]\centering\caption{Study of biases in APR evaluation in the literature.}\label{tab:uncovered_bias_review}% \resizebox{1\linewidth}{!}{\begin{threeparttable}\begin{tabularx}{\textwidth}{c L{4.5cm} c c c c c}\toprule\textbf{ID} & \multicolumn{1}{c}{\textbf{xx}} & \textbf{Phase}\tnote{*} & \textbf{xx}  & \textbf{xx} & \textbf{xx} &       \textbf{xx}\\ \midrule \rownumber& xx& DS& xx        & xx&  xx& xx\\ \bottomrule\end{tabularx}\begin{tablenotes}[para, flushleft]
\footnotesize
%\item[*] DS stands for the xx phase. 
DS stands for the xx phase. 
\end{tablenotes}\end{threeparttable}

备注:2022年6月9日03:19:13 记住一定要把tablenotes放在threeparttable 环境里面,不然就没法对齐了。我被这个困扰了很久。。。难顶。

参考:

  • threeparttable: changing indentation of table footnotes https://tex.stackexchange.com/questions/134322/threeparttable-changing-indentation-of-table-footnotes
  • The threeparttable package https://mirror-hk.koddos.net/CTAN/macros/latex/contrib/threeparttable/threeparttable.pdf

7.2.2. tablenotes 和 table 对齐

核心就是: 把tablesnote的环境放到threeparttable环境里面,我之前都放外面,导致无法对齐!

2022年6月9日03:20:13 2022年6月9日03:20:16 这个时候又犯了同样的错误。。。又找了很久,看来自己不长记性,抑或是人是会同样错误好几次的?

\documentclass[a4paper]{article}
\usepackage{booktabs}
\usepackage{threeparttable}\begin{document}\begin{table}\centering\footnotesize\caption{Descriptive statistics of total sample based on modified data set}\label{table:descrtotal}\medskip\begin{threeparttable}\begin{tabular}{lrrr}\toprule& \% E & n UE & n E \\\midruleNo Denomination & 0.63 & 1425 & 2470 \\ Buddhist & 0.70 & 312 & 731 \\ Hindu & 0.24 & 482 & 149 \\ Muslim & 0.23 & 3993 & 1185 \\ Orthodox & 0.51 & 1574 & 1609 \\ Other Christian groups & 0.53 & 697 & 779 \\ Other Denominations & 0.53 & 294 & 326 \\ Other Muslim groups & 0.14 & 1428 & 238 \\ Protestant & 0.55 & 1214 & 1472 \\ Roman Catholic & 0.56 & 2031 & 2550 \\ \bottomrule\end{tabular}\begin{tablenotes}[para,flushleft]Note: Share of aliens employed (\% E), Number of aliens unemployed (n 
UE) and Number of aliens employed (n E).\end{tablenotes}\end{threeparttable}
\end{table}\end{document}
  • Align Note below table tex.stackexchange.com/questions/19991/align-note-below-table

8. latexdiff的使用

参考我之前的博客:

  • 【全面】在Ubuntu下安装、配置latexdiff,并且成功编译latexdiff后的项目_the Blog of 等不到天亮丶等时光-CSDN博客 优先参考。2021写的
  • latexdiff使用(latex下的diff,可用于论文修改版本的提交)(附perl的一系列操作)_the Blog of 等不到天亮丶等时光-CSDN博客 没想到2020年的时候也写过,,,早就不记得了!

9. Box的使用

9.1. tcolorbox

参考这两个,很全了:

  • https://tools.ietf.org/doc/texlive-doc/latex/tcolorbox/tcolorbox.pdf
  • https://www.overleaf.com/project/62908a819afb7b583d123dec
% 定义
\newtcolorbox{boxB}{% fontupper = \bf\color{main}, % font colorboxrule = 1pt,colframe = main,colback=white,width=0.98\linewidth,boxsep = 1mm,left = 1mm,right = 1mm,top = 1mm,bottom = 1mm,rounded corners,arc = 0pt   % corners roundness
}% 使用
\begin{boxB}
% \setlength{\parindent}{1em} 
xxx
\end{boxB}

9.2. tcolorbox 进阶使用

参考我的博客:

  • 【较全面】LaTeX tcolorbox的使用(添加阴影,支持跨页,设置颜色,设置缩进,设置边距,设置线框类型,隐藏线框) https://blog.csdn.net/weixin_39278265/article/details/125053192

10. bibliography参考文献

10.1. 添加url bib(我的artifact)

@Misc{artifact_xxx_2021,
howpublished = {[Online]},
note = {Available: \url{https://github.com/xxx}, 2021},
title = {Artifact page of our study},
author={xxx},
year={2021}
}
  • How to add a URL to a LaTeX bibtex file? https://tex.stackexchange.com/questions/35977/how-to-add-a-url-to-a-latex-bibtex-file

11. 中文文档操作

11.1. 使用中文、设置字体

参考这两篇文献就可:

  • Chinese https://www.overleaf.com/learn/latex/Chinese 使用中文
  • Which OTF or TTF fonts are supported via fontspec? https://www.overleaf.com/learn/latex/Questions/Which_OTF_or_TTF_fonts_are_supported_via_fontspec%3F#Chinese 使用中文字体

核心代码:

\usepackage{xeCJK} 
\setCJKmainfont{FandolKai}

11.2. highlight中文文本且自动换行

发现无法用下划线!!醉了,可能是overleaf的问题,都不好说。

只能用highlight了。
然而这个也比较难调。

最后得到代码如下:

\documentclass[fntef,nofonts,11pt,a4paper]{ctexbook}  %
\usepackage[utf8]{inputenc}% 中文
\setCJKmainfont{FandolKai}% highlight
\usepackage{xcolor}
\usepackage{ulem}
\newcommand\hl{\bgroup\markoverwith{\textcolor{yellow}{\rule[-.5ex]{2pt}{2.5ex}}}\ULon}

备注:

  • fntef 非常重要。换成CJKfntef都不行了(无法换行)。
  • ctexbook 这个也挺重要。

参考:

  • Is there any solution for highlighting text in CJK? https://tex.stackexchange.com/questions/75019/is-there-any-solution-for-highlighting-text-in-cjk

12. 公式相关

12.1. 取消公式的自动编码

\begin{equation*}\end{equation*}

参考:

  • How do I turn off equation auto numbering https://tex.stackexchange.com/questions/2597/how-do-i-turn-off-equation-auto-numbering

12.2. 将latex的公式转换到word里面!(以及打出中文公式)

2021年12月8日18:40:23
这个问题还没有完全解决,但是我么得时间了,先在这里记一笔,后面解决了再开单章!

首先解决方案有2种:
1)将latex写的pdf转word,然后copy。但是这个对公式好像不太好用,所以不太推荐。
2)使用Word里面的Aurora插件。 这个感觉不错。

关于Aurora的安装,网上有很多教程了:

  • word2016安装Aurora,并添加到选项卡 https://blog.csdn.net/qq_35987777/article/details/103499757 这里给了下载链接,还是比较给力的
  • Aurora的安装和中文配置 https://blog.csdn.net/wdkirchhoff/article/details/72903885

安装还是比较简单的,备注:由于我本地就有MikTex,所以我没有安装下载包里面的Micro-miktex.exe

但我不知道为啥,就是打不出来中文公式。(英文公式没问题的)。我的miktex也安装了各种ctex,xecjk包来着,按道理没问题,但是没得办法。搞不定。如下图:

在这里插入图片描述

我在本地的vscode上试了下:

\documentclass{article}% \usepackge{xeCJK}
\usepackage[UTF8]{ctex}%\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amssymb}
% \usepackage{euler}\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{KaiTi}\providecommand{\abs}[1]{\left\lvert#1\right\rvert}
\providecommand{\norm}[1]{\left\lVert#1\right\rVert}\begin{document}\begin{equation}\stackrel{\stackrel{\mbox{\text{测试}}}{\mbox{用例}}}{%\begin{bmatrix}t_1 \\t_2 \\\vdots \\\quadt_m\end{bmatrix}%}\ \quad\stackrel{\textrm{\normalsize \textit{stmts}: \quad}\begin{bmatrix}\ s_1 & \ \ s_2 & \ \cdots & \ s_n \end{bmatrix} }{\textrm{\textit{spectrum}:}\begin{bmatrix}0/1 & 0/1 & \cdots & 0/1 \\0/1 & 0/1 & \cdots & 0/1 \\\vdots & \vdots & \ddots & \vdots \\0/1 & 0/1 & \cdots & 0/1\end{bmatrix}%}\ \quad\stackrel{\stackrel{\mbox{test}}{\mbox{result}}}{%\begin{bmatrix}pass/fail \\pass/fail \\\vdots \\pass/fail\end{bmatrix}%}\label{eq:spectrum}
\end{equation}测试测试测试\end{document} 

一点问题没有:
在这里插入图片描述

先就这样吧,告一段落,后续有需要再研究。2021年12月8日18:47:55

13. enumerate

13.1. 编号格式、取消缩进、调整间距

% leftmargin 是取消左边的缩进
% \arabic是阿拉伯数字,最后出来的编号是:(1) 这种
\begin{enumerate}[label=(\arabic*), leftmargin=*]\end{enumerate}
  • https://blog.chapagain.com.np/latex-roman-numbers-in-enumerate-list-and-adjust-space-between-list-items/

更好的办法

\usepackage{enumitem}% \SetEnumitemValue{label}{numeric}{(\arabic*)}
\setlist[enumerate]{label=(\arabic*), leftmargin=*, nosep, itemsep=5pt}
% nosep} , parsep=0pt
% \setlist{nosep}

参考:

  • 官方文档。 https://mirrors.cqu.edu.cn/CTAN/macros/latex/contrib/enumitem/enumitem.pdf
  • 调整间距。 https://tex.stackexchange.com/questions/434407/setting-enumerate-item-separation-in-preamble

14. 注释

多行注释:

  • Multi-line (block) comments in LaTeX https://tex.stackexchange.com/questions/87303/multi-line-block-comments-in-latex

15. url 超链接

15.1. 去除frame,以及修改颜色

2022年6月9日22:27:19

\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\hypersetup{colorlinks,linkcolor={red!50!black},citecolor={blue!50!black},urlcolor={blue!80!black}
}

参考:

  • Remove ugly borders around clickable cross-references and hyperlinks https://tex.stackexchange.com/questions/823/remove-ugly-borders-around-clickable-cross-references-and-hyperlinks

感觉审美是真的可以,我觉得这个颜色挺好看的。

16. xcolor 颜色的使用

16.1. 报错:Option clash for package xcolor

  • 【已解决】acmart模板中使用xcolor出错:Option clash for package xcolor.https://blog.csdn.net/weixin_39278265/article/details/125257636

17. 代码高亮 minted

参考我的博客:

  • 【已解决】VSCode LaTeX支持minted https://blog.csdn.net/weixin_39278265/article/details/126907724

18. 算法 Algorithm 伪代码

参考我的博客:

  • LaTeX Algorithm相关写法 https://blog.csdn.net/weixin_39278265/article/details/127099037

19. 小结

创作结束时间:

20. 参考文献

这篇关于【Cheatsheet】LaTeX的常用代码、常见问题解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/198938

相关文章

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

MySQL安装时initializing database失败的问题解决

《MySQL安装时initializingdatabase失败的问题解决》本文主要介绍了MySQL安装时initializingdatabase失败的问题解决,文中通过图文介绍的非常详细,对大家的学... 目录问题页面:解决方法:问题页面:解决方法:1.勾选红框中的选项:2.将下图红框中全部改为英

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui