第一次使用latex时踩了很多坑,在此记录一下并梳理一下它的基本用法

一、环境

这里先介绍一下我的latex环境,因为不同的环境下使用可能会有细微的差别

使用latex镜像是: texlive2022-20220321.iso

下载地址:https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/

编译器环境: TeXstudio 4.2.3

image-20230304105701973

相关配置

image-20230304105801557

二、基本使用技巧

1.基本结构

latex文档的后缀为.tex , 整体的结构是这样的

1
2
3
4
5
\documentclass{article}
\usepackage{ctex} %%所有的包在这里导入
\begin{document} %%正文内容开始标签
正文内容
\end{document} %%正文内容结束标签

2.编译使用

image-20230304110410898

点这里的build&view,即可编译latex,并在右边栏看到效果

image-20230304152124656

3.伪代码

  • 先导包
1
\usepackage[ruled]{algorithm2e}
  • 伪代码的latex代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\begin{algorithm}[hbp]  %伪代码开始,其中的hbp为伪代码的位置,可以自己调节
\caption{this is a algorithm} \label{algo1} %算法名字,\label里面的是文中要引用的
\LinesNumbered %要求显示行号
\KwIn{this is input} %输入
\KwData{this is data } %数据
print a "hello world"\; % \;用于换行
\While{条件}{ %while循环
print a "hello world"\; \tcc{这里是注释}
\eIf{条件}{ % if else 语句
true分支
}{
false分支
}
}
\end{algorithm}
  • 文中引用
1
I have a algorithm \ref{algo1}.
image-20230304152423263

4.图片

1
\usepackage{graphicx}
1
2
3
4
5
\begin{figure*}[htp] %如果是双栏的,加上*可以使图片横在中间,若果需要图片在一侧就去掉*
\centering %图片居中
\includegraphics[width=2in]{pic/picture.png} %图片保存到地址和宽度
\caption{this is a picture.} \label{fig1} %图片标题,lable内为下面引用的名字
\end{figure*}

文中引用

1
I have a picture \ref{fig1}.
image-20230304155303499

5.表格

  • 普通表格
1
2
3
4
5
6
7
8
9
10
11
\begin{table}[]
\caption{this is a table}\label{table1}
\begin{tabular}{|c|c|c|c|} %表示表格有4列,c表示居中,|表示表格的边框(|c|就是两边都有边框)
\hline % 表示表格横着的边框
A1 & A2 & A3 & A4 \\ %表格的第1行,每个单元格使用 & 分隔
\hline
B1 & B2 & B3 &B4 \\ %表格的第2行
C1 & C2 & C3 & C4 \\ %表格的第3行
\hline
\end{tabular}
\end{table}

image-20230304155552823

  • 表头带斜线的表格

导包

1
\usepackage{diagbox}
1
2
3
4
5
6
7
8
9
10
11
\begin{table}[]
\caption{this is a table}\label{table1}
\begin{tabular}{|c|c|c|c|} %表示表格有4列,c表示居中,|表示表格的边框(|c|就是两边都有边框)
\hline % 表示表格横着的边框
\diagbox{A11}{A12} & A2 & A3 & A4 \\ %表格的第1行,每个单元格使用 & 分隔
\hline
B1 & B2 & B3 &B4 \\ %表格的第2行
C1 & C2 & C3 & C4 \\ %表格的第3行
\hline
\end{tabular}
\end{table}

image-20230304155645970

  • 跨行跨列

导包

1
\usepackage{multirow}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\begin{table}[]
\caption{this is a table}\label{table1}
\begin{tabular}{|c|c|cc|cc|} %表示表格有4列,c表示居中,|表示表格的边框(|c|就是两边都有边框)
\hline % 表示表格横着的边框
\multirow{2}{*}{A1} & \multirow{2}{*}{A2} & % \multirow是跨行
\multicolumn{2}{c|}{A34} & \multicolumn{2}{c|}{A45} \\ % \multicolumn是跨列
\cline{3-6} % \cline为局部划线,表示3列到6列画横线
& & B3 & B4 & B5 &B6 \\
\hline
C1 & C2 & C3 & C4 &C5 &C6 \\
\hline
C1 & C2 & C3 & C4 &C5 &C6 \\
\hline
\end{tabular}
\end{table}
  • 文中引用
1
I have a table \ref{table1}.

image-20230304160515751

6.参考文献

先将要引用的参考文献的bib格式写到test1.bib文件内

image-20230304161828726

在tex文件中引用这个文件

1
2
\bibliographystyle{ACM-Reference-Format} %引用样式
\bibliography{test1}

在文中标注引用

1
I have a reference\cite{paper1,paper2,....}
image-20230304161955627

和我们平常看到的不一样,我们可以使用其他期刊的样式,把参考文献前面变成序号

参考文献的bib格式在谷歌学术或者论文网站都可以复制

image-20230304162406708

三、常用公式表达式整理

转载自 https://www.cnblogs.com/echo-coding/p/8663676.html

  • 常用表达式

image-20230304163235289

  • 常用希腊字母

image-20230304163331552

  • 常用数学公式

image-20230304163422004