LaTeX入门
什么是LaTeX
LaTeX(读作/ˈlɑːtɛx/或/ˈleɪtɛx/)是一个让你的文档看起来更专业的排版系统,而不是文字处理器。它尤其适合处理篇幅较长、结构严谨的文档,并且十分擅长处理公式表达。它是免费的软件,对大多数操作系统都适用。
LaTeX 基于 TeX(Donald Knuth 在 1978 年为数字化排版设计的排版系统)。TeX 是一种电脑能够处理的低级语言,但大多数人发现它很难使用。LaTeX 正是为了让它变得更加易用而设计的。目前 LaTeX 的版本是 LaTeX 2e。
如果你习惯于使用微软的 Office Word 处理文档,那么你会觉得 LaTeX 的工作方式让你很不习惯。Word 是典型的「所见即所得」的编辑器,你可以在编排文档的时侯查看到最终的排版效果。但使用 LaTeX 时你并不能方便地查看最终效果,这使得你专注于内容而不是外观的调整。
一个 LaTeX 文档是一个以 .tex
结尾的文本文件,可以使用任意的文本编辑器编辑,比如 Notepad,但对于大多数人而言,使用一个合适的 LaTeX 编辑器会使得编辑的过程容易很多。在编辑的过程中你可以标记文档的结构。完成后你可以进行编译——这意味着将它转化为另一种格式的文档。它支持多种格式,但最常用的是 PDF 文档格式。
文章基本信息
文章标题 \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*}
图片
插入图片
% 常用选项[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}