以下是我在Latex使用过程中用到的一些语法,可能不是很全面,但是足够我使用了。有说错的地方还请大家谅解,在评论区多多指正~下面分为几个部分说明(注意导入所需要的包!可参照文章末尾完整示例里的用法)
一、文章基本信息
- 文章标题 \title{ }
\title{这是标题}
需要在\begin{document}里面写\maketitle才能显示标题。
- 文章作者 \author{ }
\author{sillyxue}
- 写作时间 \date{ }
\date{\today}
二、文章结构
- 普通文章 \documentclass[UTF8]{ctexart}
\begin{document}
\section{第一章节}
这是第一章节内容,相当于1
\subsection{子章节}
这是第一章节子章节内容,相当于1.1
\subsubsection{子章节的子章节}
这是第一章节子章节的子章节内容,相当于1.1.1
\end{document}
- 书籍 \documentclass[UTF8]{ctexbook}
\begin{document}
\part{第一部分}
\chapter{第一大章}
\section{第一章节}
这是第一章节内容,相当于1
\subsection{子章节}
这是第一章节子章节内容,相当于1.1
\subsubsection{子章节的子章节}
这是第一章节子章节的子章节内容,相当于1.1.1
\end{document}
三、字体样式
\textbf{加粗!!}
\textit{斜体!!}
\emph{强调!!}
\underline{下划线!!}
四、列表
- 无序列表
\begin{itemize}
\item 吃饭
\item 睡觉
\item 弹尤克里里
\end{itemize}
- 有序列表
\begin{enumerate}
\item 第一步
\item 第二步
\item 第三步
\end{enumerate}
五、数学公式
- 行内公式:写在两个$之间
$E=mc^2$
- 普通公式
\begin{equation}
E=mc^2
\end{equation}
或者[E=mc^2],这种不会自动标注公式序号。
- 公式分行对齐:在每个需要对齐的位置前写&,除最后一行外每行末尾写\\
\begin{align*}
2^5 &= (1+1)^5 \\
&= \begin{multlined}[t]
\binom{5}{0} \cdot1^{5}+\binom{5}{1} \cdot1^{4}\cdot1+\binom{5}{2} \cdot1^{3}\cdot1^{2} \\
+\binom{5}{3} \cdot1^{2}\cdot1^{3}+\binom{5}{4} \cdot1\cdot1^{4}+\binom{5}{5} \cdot1^{5}
\end{multlined}\\
&= \binom{5}{0}+\binom{5}{1}+\binom{5}{2}+\binom{5}{3}+\binom{5}{4}+\binom{5}{5} \end{align*}
- 公式的书写方式
1)通过vscode自带的符号集,位置如下图所示:
2)macOS系统可以下载xFormula,输入公式后点击右上角的拷贝按钮即可复制(但是非VIP有字数限制的啊喂…每次都一半一半的写公式…)
六、图片
- 插入图片
% 常用选项[htbp]是浮动格式:(!是强制的意思)
% h:当前位置。将图形放置在正文文本中给出该图形环境的地方。
% t:顶部。将图形放置在页面的顶部。
% b:底部。将图形放置在页面的底部。
% p:浮动页。将图形放置在一只允许有浮动对象的页面上。
\begin{figure*}[!h]\label{yuce}
\centering
\includegraphics[width=0.5\textwidth]{预测}
\caption{这里是图片标题,上一行指定图片的宽度和路径}
\end{figure*}
- 引用图片
通过插入图片时指定的label标签引用相应图片。
\label{yuce} % 写在插入图片的位置(上面代码块里写了这个)
这里就可以引用啦啦啦~通过【\ ref {给图片指定的label名字} 】来引用:
图\ref{yuce}描述了......
- 多子图(引用同上)
\begin{figure}[!h]
\centering
\subfloat[第一部分]{\includegraphics[width=0.33\textwidth]{figure/3-1}}{}
\label{fig1}
\subfloat[第二部分]{\includegraphics[width=0.33\textwidth]{figure/3-2}}{}
\label{fig2}
\subfloat[第三部分]{\includegraphics[width=0.33\textwidth]{figure/3-3}}{}
\label{fig3}
\caption{示意图}
\label{short3}
\end{figure}
七、表格
% 表格(3*3),第四行c是居中的意思
\begin{table}[htbp]
\centering
\caption{表格演示}
\begin{tabular}{c | c | c}
\hline
单元格1 & 单元格2 &单元格3 \\
\hline
单元格4 & 单元格5 &单元格6 \\
\hline
单元格7 & 单元格8 &单元格9 \\
\hline
\end{tabular}
\end{table}
这里推荐一个好用的Excel插件,可以自动生成表格:
Excel2LaTeX
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{table}[htbp]
\centering
\caption{爱好表}
\begin{tabular}{c | c | c}
\hline
\textbf{名字} & \textbf{年龄} & \textbf{爱好} \\
\hline
小红 & 1 & 唱歌 \\
\hline
小明 & 2 & 跳舞 \\
\hline
\end{tabular}%
\label{这里指定用于引用的label}%
\end{table}%
表格的引用方式和图片的相同。
八、代码
- 普通代码
% 普通代码
\begin{verbatim}
#include{stdio.h}
int main(){
printf("hello,world!");
return 0;
}
\end{verbatim}
- 语法高亮代码
\lstset{
columns=fixed,
numbers=left, % 在左侧显示行号
numberstyle=\tiny\color{gray}, % 设定行号格式
frame=none, % 不显示背景边框
backgroundcolor=\color[RGB]{245,245,244}, % 设定背景颜色
keywordstyle=\color[RGB]{40,40,255}, % 设定关键字颜色
numberstyle=\footnotesize\color{darkgray},
commentstyle=\it\color[RGB]{0,96,96}, % 设置代码注释的格式
stringstyle=\rmfamily\slshape\color[RGB]{128,0,0}, % 设置字符串格式
showstringspaces=false, % 不显示字符串中的空格
language=c, % 设置语言
}
% 代码语法高亮
\begin{lstlisting}
#include{stdio.h}
int main(){
printf("hello,world!");
return 0;
}
\end{lstlisting}
- 算法
\begin{codebox}
\Procname{$\proc{Insertion-Sort(A)}$}
\li \For $j \gets 2$ \To $\id{length}[A]$ \label{li:for}
\li \Do $\id{key} \gets A[j]$ \label{li:for-begin}
\li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$.
\li $i \gets j-1$
\li \While $i>0$ and $A[i]>\id{key}$ \label{li:while}
\li \Do $A[i+1] \gets A[i]$ \label{li:while-begin}
\li $i \gets i-1$ \label{li:while-end}
\End
\li $A[i+1] \gets \id{key}$ \label{li:for-end}
\End
\end{codebox}
九、参考文献
- 插入参考文献
% 参考文献列表
% 宽度9,表示引用的个数不能超过9个
\begin{thebibliography}{9}
\bibitem[1]{liuhaiyang2013latex}
刘海洋.
\newblock \LaTeX {}入门\allowbreak[J].
\newblock 电子工业出版社, 北京, 2013.
\bibitem[2]{mathematical-modeling}
全国大学生数学建模竞赛论文格式规范
\end{thebibliography}
- 引用参考文献 \cite{ }
入门书籍可以看《\LaTeX{}入门》\cite{liuhaiyang2013latex}。
% 插入包 \hypersetup{hidelinks} 可以隐藏绿框框
十、其他
- 脚注
\footnote{脚注内容}
- 附录
\appendix
- 新页
\newpage
- 插入其他页面
\input{code/p1.tex}
十一、学习资料
十二、完整示例
\documentclass[UTF8]{ctexart}
% 导入需要的包
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{clrscode}
\title{这是标题}
\author{sillyxue}
\date{\today}
\begin{document}
\maketitle
\textbf{加粗了!!}
\textit{斜体!!}
\emph{强调!!}
\underline{有下划线!}
% 类型为ctexbook时:
% \part{第一部分}
% \chapter{第一大章}
\section{第一章节}
这是第一章节内容。
\subsection{子章节}
这是第一章节子章节内容欧呦
\subsubsection{子章节的子章节}
这是第一章节子章节的子章节内容哈哈哈哈hahahahahaahahahaha
图\ref{yuce}描述了啦啦啦啦啦啦
% 图片(t,h,b,p)
\begin{figure*}[!h]\label{yuce}
\centering
\includegraphics[width=0.5\textwidth]{预测}
\caption{预测的解释}
\end{figure*}
% 无序列表
\begin{itemize}
\item 吃饭
\item 睡觉
\item 弹尤克里里
\end{itemize}
% 有序列表
\begin{enumerate}
\item 第一步
\item 第二步
\item 第三步
\end{enumerate}
% 数学公式
行内公式写在两个美元符号之间,例如$E=mc^2$,好看诶!
\begin{equation}
E=mc^2
\end{equation}
或者用这种,但是不会自动标注公式序号\[E=mc^2\]
\begin{equation}
d={k \varphi(n)+1} \over e
\end{equation}
\begin{equation}
Y=\alpha \frac{\max_{\left( {}\beta_{1} X_{1}+\beta_{2} X_{2}+\beta_{3} X_{3}+\beta_{4} X_{4}+\beta_{5} X_{5}+\beta_{6} X_{6}\right) } }
{\min_{\left( {}\beta_{1} X_{1}+\beta_{2} X_{2}+\beta_{3} X_{3}+\beta_{4} X_{4}+\beta_{5} X_{5}+\beta_{6} X_{6} \right) } }
\end{equation}
% 公式对齐
\begin{align*}
2^5 &= (1+1)^5 \\
&= \begin{multlined}[t]
\binom{5}{0} \cdot1^{5}+\binom{5}{1} \cdot1^{4}\cdot1+\binom{5}{2} \cdot1^{3}\cdot1^{2} \\
+\binom{5}{3} \cdot1^{2}\cdot1^{3}+\binom{5}{4} \cdot1\cdot1^{4}+\binom{5}{5} \cdot1^{5}
\end{multlined}\\
&= \binom{5}{0}+\binom{5}{1}+\binom{5}{2}+\binom{5}{3}+\binom{5}{4}+\binom{5}{5}
\end{align*}
% 表格(3*3)
\begin{table}[htbp]
\centering
\caption{表格演示}
\begin{tabular}{p{2cm} | c | c}
\hline
单元格1 & 单元格2 &单元格3 \\
\hline
单元格4 & 单元格5 &单元格6 \\
单元格7 & 单元格8 &单元格9 \\
\hline
\end{tabular}
\end{table}
% 用Excel插件自动生成表格
% \input{table1.tex}
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{table}[htbp]
\centering
\caption{爱好表}
\begin{tabular}{c | c | c}
% \toprule
\hline
\textbf{名字} & \textbf{年龄} & \textbf{爱好} \\
% \midrule
\hline
小红 & 1 & 唱歌 \\
% \midrule
\hline
小明 & 2 & 跳舞 \\
% \bottomrule
\hline
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
% 普通代码
\begin{verbatim}
#include{stdio.h}
int main(){
printf("hello,world!");
return 0;
}
\end{verbatim}
% 代码语法高亮,加颜色行号参考listings宏包
\lstset{
columns=fixed,
numbers=left, % 在左侧显示行号
numberstyle=\tiny\color{gray}, % 设定行号格式
frame=none, % 不显示背景边框
backgroundcolor=\color[RGB]{245,245,244}, % 设定背景颜色
keywordstyle=\color[RGB]{40,40,255}, % 设定关键字颜色
numberstyle=\footnotesize\color{darkgray},
commentstyle=\it\color[RGB]{0,96,96}, % 设置代码注释的格式
stringstyle=\rmfamily\slshape\color[RGB]{128,0,0}, % 设置字符串格式
showstringspaces=false, % 不显示字符串中的空格
language=c % 设置语言
}
% 代码语法高亮
\begin{lstlisting}
#include{stdio.h}
int main(){
printf("hello,world!");
return 0;
}
\end{lstlisting}
% 算法
\begin{codebox}
\Procname{$\proc{Insertion-Sort(A)}$}
\li \For $j \gets 2$ \To $\id{length}[A]$ \label{li:for}
\li \Do $\id{key} \gets A[j]$ \label{li:for-begin}
\li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$.
\li $i \gets j-1$
\li \While $i>0$ and $A[i]>\id{key}$ \label{li:while}
\li \Do $A[i+1] \gets A[i]$ \label{li:while-begin}
\li $i \gets i-1$ \label{li:while-end}
\End
\li $A[i+1] \gets \id{key}$ \label{li:for-end}
\End
\end{codebox}
% 脚注
\subsection{脚注}
利用 \verb|\footnote{具体内容}| 可以生成脚注\footnote{脚注可以补充说明一些东西}。
% 矩阵
\[
\mathbf{X} = \left(
\begin{array}{cccc}
x_{11} & x_{12} & \ldots & x_{1n}\\
x_{21} & x_{22} & \ldots & x_{2n}\\
\vdots & \vdots & \ddots & \vdots\\
x_{n1} & x_{n2} & \ldots & x_{nn}\\
\end{array} \right)
\]
% 引用参考文献
% \hypersetup{hidelinks} 可以隐藏绿框框
\LaTeX{}的入门书籍可以看《\LaTeX{}入门》\cite{liuhaiyang2013latex}。这是一个简单的引用,用 \verb|\cite{bibkey}| 完成。
% 参考文献列表
% 宽度9,表示引用的个数不能超过9个
\begin{thebibliography}{9}
\bibitem[1]{liuhaiyang2013latex}
刘海洋.
\newblock \LaTeX {}入门\allowbreak[J].
\newblock 电子工业出版社, 北京, 2013.
\bibitem[2]{mathematical-modeling}
全国大学生数学建模竞赛论文格式规范
\end{thebibliography}
\end{document}