Featured image of post Memo: Lang - Latex | Common Usages

Memo: Lang - Latex | Common Usages

Table of contents

(Feature image from: latex · GitHub Topics · GitHub)


Basics

文档页面旋转90度

Rotate a page 180 degrees in LaTeX

For the whole document:

1
\documentclass[landscape]{article}

交叉引用替换为文字

LaTeX/Hyperlinks

用法:

1
2
3
\usepackage{hyperref}
...
\hyperref[label_name]{''link text''}

\ref{label_name} 只是数字


Cross-references

post

\pageref{key} command, which prints the number of the page where the \label{key} was inserted.


Basic usage

(2021-01-15)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
\documentclass{article}	%文档类型:文章
\documentclass{book}	%书
\documentclass{beamer}	%幻灯片格式
\documentclass[UTF8]{ctexart}	%ctexart支持中英文混拍,指定文档编码类型
\documentclas[UTF8]{ctexbook}	%对书籍排版

\title{文章标题}
\author{文章作者}
\date{文档的修改日期}
\date{\today}	%自动生成当天日期

\begin{document}	%下面是正文,上面是前言,\begin和\end之间是环境/作用域,位于同一个环境中的内容将会共享相同的文字格式
\maketitle	%在当前位置生成文档的标题(前言区设置的信息)

\textbf{加粗文字}	%bold font
\textit{斜体}		%italic
\underline{加下划线}
%两个回车是换段,一个回车是一个空格

\part{书籍的第一部分}
\chapter{书籍的第一章}
\section{这是地一个章节}
你好!
\subsection{子章节}
二级章节下的内容
\subsubsection{这是一个三级章节}
三级章节下的内容
\section{这是第二个章节}
第二章节下的内容

%插入图片
\usepackage{graphicx}	%引用包,包含了若干绘制图片的指令
\begin{figure}	%把图片嵌套到figure环境中,可以指定标题
\centering		%将图片居中显示
\includegraphics[width=0.5\textwidth]{图片名字可省略后缀}	%在当前位置添加一张图片,图片宽度等于0.5倍的当前文本区域的宽度
\caption{这是图片的标题}
\end{figure}

%列表
\begin{itemize}	%无序列表的创建:列表中的每一个元素都需要以\item开头
\item 列表项1
\item 列表项2
\item 列表项3
\end{itemize}

\begin{enumrate}	%有序列表:前面带编号
\item 列表项1
\item 列表项2
\item 列表项3
\end{enumerate}

%数学公式
质能方程:$E=mc^2$

\begin{equation}
E=mc^2
\end{equation}

\[
E=mc^2
\]

\over 是几分之几
\[
d={k \varphi(n)+1} \over e
\]
codecogs 可以测试公式

%表格
\begin{table}	%table环境设置标题
\caption{表格的标题}
\center	%表格居中显示
\begin{tabular}{|c| c| c|}	%有三列,每列都居中(centering),| 代表竖直边框。
% {l c c} 则表示第一列左对齐(left)
% {p{2cm} c c}	设置列宽2cm
\hline	#水平边框
单元格1 & 单元格2 & 单元格3 \\
\hline\hline	%双横线
单元格4 & 单元格5 & 单元格6 \\
\hline
单元格7 & 单元格8 & 单元格9 \\
\hline
\end{tabular}
\end{table}

引用

条目后显示页码

(2024-06-22)

  • Use package backref.

    (Ask chatGPT4o: “How to add the page number where a reference gets cited behind the entry item automatically in latex”)

    \usepackage[backref=page]{hyperref}

    (Ref: Backref package for page reference Found in DDG)


显示引用自某一页

(2024-06-22)

1
2
3
4
5
6
7
In-text citation:

(Moghaddam, 2018, p. 30)

Reference:

Moghaddam, F. M. (2018). Mutual radicalization: How groups and nations drive each other to extremes. https://doi.org/10.1037/0000089-000

公式

矩阵每行加 label

1
\usepackage{blkarray}

Vector arrow

(2024/06/21)

Search: "" in DDG


Fonts

  1. Bold and italic

    (2024-06-23)

  2. Caligraphy: \mathcal, where \cal f will make the entire line to be caligraphy.

    Two different calligraphic font styles in math mode

  3. 公式加框 (ChatGPT: Using \textbf{}, \textit{}, or \boxed{} for Emphasis)

    1
    
    \boxed{E = mc^2}
    

长公式换行

(2024-06-23)


文字颜色

(2024/07/17)

Overleaf Tutorial

  • Use package xcolor:

    1
    2
    3
    
    \usepackage[dvipsnames]{xcolor}
    
    {\color{ForestGreen} Sentences to be highlighted}.
    
  • 论文新加内容用森林绿 ForestGreen,删除内容用砖红色 BrickRed

    1
    2
    3
    
    {\color{BrickRed}
    \sout{Text to be deleted}
    }
    

两组公式并排

References:

  1. DeepSeek

Notes:

(2025/02/08)

  1. Use {array} environment

    $$ \begin{array}{c|c} \begin{aligned} x + y = 35 \end{aligned} & \begin{aligned} 2x + 4y = 94 \end{aligned} \end{array} $$
  2. Use {align*} environment r1-DS:

    1
    2
    3
    4
    
    \begin{align*}
        x + y &= 5 && \text{and} && 2x - y &= 3 \\\
        a &= b + c && \text{or} && d &= e - f
    \end{align*}
    
    $$ \begin{align*} x + y &= 5 && \text{and} && 2x - y &= 3 \\\ a &= b + c && \text{or} && d &= e - f \end{align*} $$

推导注释

References:

  1. DeepSeek
    • Prompt: “What are the conventions for adding comments in formula derivations?”

Notes:

(2025/02/08)

  1. intertext doesn’t work in markdown

    1
    2
    3
    4
    5
    
    \begin{align}
        F &= ma \\
        &= m \cdot \frac{dv}{dt} \intertext{Since acceleration is the derivative of velocity} 
        &= \frac{d}{dt} (mv)
    \end{align}
    
    $$ \begin{align} F &= ma \\ &= m \cdot \frac{dv}{dt} \intertext{Since acceleration is the derivative of velocity} &= \frac{d}{dt} (mv) \end{align} $$
  2. stackrel

    1
    
    x \stackrel{\text{def}}{=} y + z
    
    $$ x \stackrel{\text{def}}{=} y + z $$

删除线

Problems:

  1. \cancel{} 是斜线,如何在文字上加 strikethrough

References:

  1. 元宝 | LaTeX 文字删除线实现方法

Notes:

(2025-05-13T23:19)

  1. Use package ulem and command \sout{}r1-元宝

代码

插入代码块

matlab Highlighting MATLAB Code in LaTeX with mcode


高亮行内代码

(2024-06-20)


Table

Table每行自动标号

Reference table row in LaTeX

使用 counter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
\usepackage{array}
\newcounter{rowcntr}[table]
\renewcommand{\therowcntr}{\arabic{rowcntr}}

% A new columntype to apply automatic stepping
\newcolumntype{N}{>{\refstepcounter{rowcntr}\therowcntr}w{c}{0.1em}}

% Reset the rowcntr counter at each new tabular
% \AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}
.......

\begin{tabular}{|N|c|c|c|}
\end{tabular}

How does the \newcolumntype command work?

w{c}{0.1em} 是控制对齐和列宽


长表格自动换页

latex long table automatic move to next page

\usepackage{longtable}

Multi-page tables using \longtable

里面不能用 tabular 环境,不过据说它支持大部分tabular 特性

1
2
3
4
\begin{longtable}{|c|c|c|c|}
Title & Nerf’s Problems & Solutions & Flaw
\endhead % 让表头出现在每页上
\end{longtable}

表格内换行

  1. cell 内部再用个 tabular 环境:

    \begin{tabular}[t]{@{}p{\linewidth}@{}}PlenOctrees for Real-time Rendering of Neural Radiance Fields \\ ICCV 2021 Oral \end{tabular}

  2. \newline ICCV 2021


表格列宽

(2024/10/03)

  • Vertical: p, m,b, Horizontal: \raggedright (left-align), \centering, \raggedleft (righ-align)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    \usepackage{array}
    
    %-----
    \begin{tabular}{p{0.2\textwidth}>{\centering}p{0.2\textwidth}>{\centering}p{0.2\textwidth}>{\centering\arraybackslash}p{0.2\textwidth}}
    \hline
    \multirow{2}{*}{Country}&\multicolumn{2}{c}{Population}&\multirow{2}{*}{Change (\%)}\\\cline{2-3}
    &2016&2017&\\
    \hline
    China&1,403,500,365&1,409,517,397&+0.4\%\\
    \hline
    \end{tabular}
    

1个cell内多行

(2024/10/03)


缩放表格

References:

  1. Is there a way to slightly shrink a table, including font size, to fit within the column boundaries?

  2. Missing \endgroup inserted. \endgroup \end{tabular}} when using \scalebox - LaTeX Stack Exchange

    • Searched by latex threeparttable Missing \endgroup inserted. in DDG
  3. Use resizebox with math mode; Missing } inserted - TeX - LaTeX Stack Exchange

    • Searched by latex resizebox Missing \endgroup inserted. in DDG
  4. ChatGPT

    • Prompted by I am using a double-column layout in Latex. How to resize a table to fix the width of a single column? Please do not use \resizebox in

Notes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
\usepackage{graphics}
% ...

\begin{table}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{r|lll}
\multicolumn{1}{r}{}
& \multicolumn{1}{l}{Heading 1}
& \multicolumn{1}{l}{Heading 2}
& \multicolumn{1}{l}{Heading 3} \\ \cline{2-4}
Row 1 & Cell 1,1 & Cell 1,2 & Cell 1,3 \\
Row 2 & Cell 2,1 & Cell 2,2 & Cell 2,3
\end{tabular}%
}
\end{table}

(2024/10/03)

  • resizebox doesn’t compatible with Springer Nature template

    The following snippet will run into errors:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
     \begin{table}[htbp]
     \centering
     \caption{Number of Points}
     \resizebox{\textwidth}{!}{
     \begin{tabular}{llllllllllll}
     \toprule
     Method   & Scan1  & Scan4  & Scan9   \\ \hline
     COLMAP   & 388 & 449 & 91  \\
     CasMVSNet & 0.63 M & 0.57 M & 0.42 M  \\ 
     \hline
     Method   & Scan32 & Scan33 & Scan34  \\ \hline
     COLMAP   & 322 & 110 & 332  \\
     CasMVSNet & 0.45 M & 0.26 M & 0.61 M   \\
     \bottomrule
     \end{tabular}
     \label{tab:n_points_dtu}
     } % curly brace of resizebox
     \end{table}
    

    Error:

    1
    2
    3
    4
    
    Missing \endgroup inserted. 
    Missing } inserted. 
    LaTeX Error: \begin{threeparttable} on input line 440 ended by \end{tabular}. 
    Package graphics Error: Division by 0.
    

    Processing: I gave up using resizebox.

(2024/11/13)

  1. Do not use \resizebox r2-SE, r3-SE

  2. Use {tabularx} (Prompted by r4-Chat)

    • Do not forget \usepackage{tabluarx}
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    \begin{table}[ht]
        \centering
        \begin{tabularx}{\columnwidth}{XXX}
            \hline
            Header 1 & Header 2 & Header 3 \\ \hline
            Data 1   & Data 2   & Data 3   \\ \hline
            Data 4   & Data 5   & Data 6   \\ \hline
        \end{tabularx}
        \caption{Table caption}
        \label{tab:example}
    \end{table}
    

label

(2024-06-20)

  1. Add label before \end{table} labels with tabular

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    \documentclass{article}
    \begin{document}
    Critical temperature for different Type I superconductors is given in Table~\ref{Tab:Tcr}
    
    \begin{table}[ht]
    \caption{My Table}
    \centering
      \begin{tabular}{l l}
        material  & T [K]\\
        \hline
        Sn                     & 3,7 \\
        Pb                     & 7,2 \\
        Al                     & 1,2\\
      \end{tabular}
      \label{Tab:Tcr}
    \end{table}
    
    \end{document}
    

Table Notes

(2024-07-07)

Ref: Zeng’s thesis:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
\begin{table}[htbp]
\caption{Comparison with 3 MLS-based upsampling methods}
\resizebox{\columnwidth}{!}{%
\begin{threeparttable}
\begin{tabular}{lccc}
\toprule
\textbf{Method}        & \textbf{Acc. (mm)} & \textbf{Comp. (mm)} & \textbf{Overall (mm)} \\
\midrule
1 & \textbf{0.373}     & 2.454               & 1.414 \\
2 & 0.391              & \underline{2.436}  & \underline{1.414} \\
3 & 0.389              & 2.459               & 1.424 \\
4 & 0.390              & 2.448               & 1.419 \\
5 & 0.455              & \textbf{2.010}      & \textbf{1.232} \\ 
\bottomrule
\end{tabular}
\begin{tablenotes}
        \footnotesize
        \item[$\cdot$] Values in \textbf{BOLDFACED} are the best results among all methods.
        \item[$\cdot$] Values in \underline{UNDERLINED} are the second best results among all methods.
\end{tablenotes}
\end{threeparttable}
}
\label{tab:comparison}
\end{table}

Paper tips

(2024-03-21)

guanyingc/latex_paper_writing_tips


Plots

画程序流程图

Code using algorithm2e package


画坐标系

Coordinate system in LaTeX with TikZ

Pgfplots package overleaf

Overfull \hbox with pgfplots graph


画夹角

How to draw a simple angle, two intersecting lines Tikz


画圆柱体

generate simple cylindrical shape with text in latex (tikz)

Learn How to Draw a Cylinder Shape in TikZ

同步缩放 TikZ 与其中的 node

在 LaTeX 中同步缩放 TikZ 与其中的 node


Tikz Examples

(2024-04-11)

Example of Petar Veličković X


Inkscape

(2024-04-23)


  1. 生成 latex 代码

    • Problems:

      (2025-09-12T09:56)

      1. 我想用 Quarto 写 slides,但是它不支持 GoAT。

        我喜欢 GoAT ascii art, 是因为我在终端里看文档的时候,也可以看到“图”

        次于以上的方法是:我想把图和文字放在一起,而 不用单独保存图片文件, 就像 tikz 和 latex 那样,图片在 build 时转换成 svg, 但是我不会写 tikz

      2. InkScape 可以将矢量图形导出成 .tex 代码,那我应该就可以加入 Quarto 里面了? 但是把全部代码粘贴到文档中,可能太长了,还是另存为一个文件吧。

      3. Tex 代码虽然不如 GoAT 那么直观,但是可以用于制作 Quarto slides, 而 slides 最好还是在浏览器上看吧,终端里看不了。

    • References: {{{

      1. 【InkScape+LaTeX】如何绘制矢量图并优雅地用在LaTeX文档中? - bilibili - 豪底狄
        • Searched by inkscape科研绘图 at bilibili }}}
    • Supports:

      (2025-09-12T09:55)

      1. 在 GUI 中绘制,之后另存为 pdf,同时会生成 .pdf_tex 文件,然后将其加入 .tex r1-LaTex

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        
        \documentclass{article}
        
        \usepackage{graphicx}
        \usepackage{xcolor}
        % 如果 svg 图片中含有数学公式,则需要使用数学库
        \usepackage{}
        
        \begin{document}
        
        \begin{figure}
        \def\svgwidth{\columnwidth}
        \input{output.pdf_tex}
        \end{figure}
        
        \end{document}
        

Take Lecture Notes

(2024-04-23)


PseudoCode

长伪代码跨栏

Placing a single algorithm in two columns

1
2
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{multicol}

把算法 内容\begin{multicols}[2] ... \end{multicols} 包起来,而不是在 \begin{algorithm} ... \end{algorithm}外面。


Comments

(2024-06-01)

  • Curious how to add comments like this:

  • 3DGS paper 里是一样的 pdf


References:

  1. Algorithms - Overleaf, Online LaTeX Editor

    • Searched by latex pseudocode block algorithm comment in DDG
  2. algorithms - How do you add a comment to pseudocode in LaTeX? - TeX - SE


Notes:

(2024-10-24)

  1. Overleaf has the same style comments (Pcmt-Ovrl):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    \documentclass{article}
    
    \usepackage{algorithm}
    \usepackage{algpseudocode}
    
    \begin{document}
    
    \begin{algorithm}
    \caption{An algorithm with caption}\label{alg:cap}
    \begin{algorithmic}
        \State $N \gets \frac{N}{2}$  \Comment{This is a comment}
    \end{algorithmic}
    \end{algorithm}
    
    \end{document}
    
  2. The \COMMENT{} of the package algorithms doesn’t work in Overleaf. (Pcmt-SE)


(2024-07-08)


(2024/06/15)


Images

1
2
3
4
5
6
\begin{figure}
    \centering
    \includegraphics[width=0.6\textwidth]{sections/Images/240520.jpg}
    \caption{}
    \label{fig:BPA_o3d}
\end{figure}

多张图片并排

subfigure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
\usepackage{subfigure}%并排子图 共享标题 有子标题

\begin{document}
\begin{figure}[H]
  \centering
  \subfigure[一次函数]{
    \label{fig:subfig:onefunction} 
    \includegraphics[scale=0.3]{figureDemo2}}
  \hspace{0.5in} % 两图片之间的距离
  \subfigure[二次函数]{
    \label{fig:subfig:twofunction} 
    \includegraphics[scale=0.3]{figureDemo3}}
  \caption{2个图片并排演示}
  \label{fig:twopicture} 
\end{figure}
\end{document} 

(2024-06-20)

  • Use \subcaptionbox, without using any new package. (Found when ask chatGPT to revise the above code.)

    • subfigure has been deprecated?
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    \begin{figure}[H]
      \centering
      \subcaptionbox{pic1\label{fig:subfig:onefunction}}{
        \includegraphics[scale=0.3]{sections/Images/240520.jpg}
      }
      \hfill
      \subcaptionbox{pic2\label{fig:subfig:twofunction}}{
        \includegraphics[scale=0.3]{sections/Images/240520.jpg}
      }
      \caption{2pics}
      \label{fig:twopicture}
    \end{figure}
    

    Effect:

    ( a ) p i c 1 F i g . 1 : 2 p i c s ( b ) p i c 2

(2024-07-09)

  • 3x2 images array:

    ( ( ( a c e ) ) ) p p p i i i c c c F 1 3 5 i g . 1 : 6 p ( ( ( i b d f c ) ) ) s p p p i i i c c c 2 4 6

    Ask chatGPT-4o

    Code snippet
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    
    \documentclass{article}
    \usepackage{graphicx}
    \usepackage{subcaption}
    
    \begin{document}
    
    \begin{figure}[htbp]
        \centering
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic1}
            \caption*{(a) pic1}
        \end{minipage}
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic2}
            \caption*{(b) pic2}
        \end{minipage}
    
        \vspace{0.5cm} % Space between rows
    
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic3}
            \caption*{(c) pic3}
        \end{minipage}
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic4}
            \caption*{(d) pic4}
        \end{minipage}
    
        \vspace{0.5cm} % Space between rows
    
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic5}
            \caption*{(e) pic5}
        \end{minipage}
        \begin{minipage}[b]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{pic6}
            \caption*{(f) pic6}
        \end{minipage}
    
        \caption{6pics}
        \label{fig:six_pics}
    \end{figure}
    
    \end{document}
    

minipage

1
2
3
4
\begin{figure}[htbp]
\centering

\end{figure}

Insert svg

(2024/06/16)

Ref: How to Use SVG Images in LaTeX | Baeldung

Use package: \usepackage[inkscapearea=page]{svg}

1
2
3
4
5
6
\begin{figure}
    \centering
    \includesvg[scale=0.5]{sections/Images/txt.svg}
    \caption{MVSNet pipeline}
    \label{fig:MVSNet}
\end{figure}

Span 2 Cols

References:

  1. Prompt for ChatGPT: “How to make a wide image displayed across two columns?”

Notes:

  1. Use figure* environment: r1-Chat

    1
    2
    3
    4
    5
    6
    7
    8
    
    \documentclass[iicol]{sn-jnl}
    
    \begin{figure*}[ht]
        \centering
        \includegraphics[width=\textwidth]{your_image_file.png}
        \caption{Your caption here}
        \label{fig:wide_image}
    \end{figure*}
    

Manage

Version Control

References:

    • Searched by Should I use git to manage latex project? in DDG

Use Git

Notes:

(2024/12/08)

  1. Fesible r1-Reddit

Caption

Notes:

(2024/12/16)

  1. Cancel the : after Fig. 1 (MVA requirement)

    1
    2
    
    \usepackage{caption}
    \captionsetup[figure]{labelsep=space}
    
    • Asked ChatGPT and Claude:

      In latex, how to cancel the : after figure index, to satisfy the journal requirement:

      Figure captions begin with the term Fig. in bold type, followed by the figure number, also in bold type. No punctuation is to be included after the number, nor is any punctuation to be placed at the end of the caption.


编号

Notes:

(2024/12/16)

  1. Appendix 中的表格的编号 继续 正文的编号,而不是 A1

    1
    2
    3
    4
    5
    
    \begin{appendices}
    \renewcommand{\thetable}{\arabic{table}} % Reset table numbering to Arabic
    \setcounter{table}{2}
    
    \section{Table of number of points}\label{secA1}
    

Editors

Vim

Problems:

  1. Overleaf’s editor is not good.

References:

  1. 我在neovim中的LaTeX编辑环境 - 帕特里柯基

Notes:

(2025-05-03T19:47)

  1. 用 nvim 插件实现编译快捷键r1-柯基
Built with Hugo
Theme Stack designed by Jimmy