【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

相关文章

IDEA实现回退提交的git代码(四种常见场景)

《IDEA实现回退提交的git代码(四种常见场景)》:本文主要介绍IDEA实现回退提交的git代码(四种常见场景),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.已提交commit,还未push到远端(Undo Commit)2.已提交commit并push到

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java 压缩包解压实现代码

《Java压缩包解压实现代码》Java标准库(JavaSE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RA... 目录一、解压压缩包1.zip解压代码实现:2.rar解压代码实现:3.调用解压方法:二、注意事项三、总

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题

《Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题》:本文主要介绍Ubuntu上手动安装Go环境并解决“可执行文件格式错误”问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录一、前言二、系统架构检测三、卸载旧版 Go四、下载并安装正确版本五、配置环境变量六、验证安装七、常见

Python将字符串转换为小写字母的几种常用方法

《Python将字符串转换为小写字母的几种常用方法》:本文主要介绍Python中将字符串大写字母转小写的四种方法:lower()方法简洁高效,手动ASCII转换灵活可控,str.translate... 目录一、使用内置方法 lower()(最简单)二、手动遍历 + ASCII 码转换三、使用 str.tr