LaTex 入门

LaTex简介

TeX 是由Donald Knuth创造的基于底层编程语言的电子排版系统[1](TEX是Honeywell公司在1980年为其Text Executive文本处理系统注册的商标,它与 TeX是两回事)。TeX能够对文档的排版进行非常精细的操作,可以生成十分精美的文档。TeX系统生成的是DVI(Device Independent)文件。

LaTex是由Leslie Lamport开发的的TeX扩展命令集合[5]。LaTex可生成DVI和PDF格式文件。由于LaTex强大的排版功能,特别是对科技文档的支持,LaTex 已经成为撰写科研论文的事实上的标准[5]。

TeX在不同的硬件和操作系统上有不同的实现版本。目前Unix/Linux上常用的TeX系统是teTeX,Windows中有MiKTeX和fpTeX。CTeX是中文套装[4]。下面仅简要介绍在使用LaTex中一些常见问题。

LaTex 与 Word

Word 是微软公司推出的一款文档和文字处理软件,它也具有较强的排版功能。与LaTex相比,Word最大优势是其”所见既所得”的特点,因而入门门槛低。另一方面Word的语法和拼写错误检查等能力比LaTex强。

LaTex作为一款排版系统,格式控制、公式编辑方面比Word好用。LaTex生成的文档比Word文档更美观。LaTex的入门门槛比较高,使用LaTex不仅要编辑文本,整理文档格式,还要处理编译过程中出现的bugs,如果使用中文,更要注意GBK,UTF-8等文档编码这些琐碎问题。另外LaTex做的幻灯片没有Word做出来的好看。

LaTex 和 Word两款软件针对的用户群体不一样,在文字处理和文档排版方面有不同的理念。 两者在功能上有许多重合之处,各有优势和不足。

安装

Latex的衍生版本众多,中文环境中常用的时CTex。下载地址为

http://www.ctex.org/CTeXDownload

Ctex套装中自带Latex编辑器WinEdt

LaTex文件格式简介

TeX文件样例

下面是一个TeX文件的简单样例:

This is an example of TeX file format.

An example of mathematical formulaion: $ ds = \sqrt{dx^2 + dy^2} $
\bye 

编译说明:
前提: 已经正确安装TeX编译环境,例如:LaTeX
操作步骤:
1) 将上述内容保存在一文本文件中,例如: foo.tex.
2) 在Windows控制台环境中,使用

tex foo.tex

既可生成相应的foo.dvi文件。

扫描二维码关注公众号,回复: 1104967 查看本文章

LaTex文件样例

下面一个样例[3]展示了LaTex文件的基本结构:

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{comment} 

% Title
\title{Document Title}
\author{Nobody \thanks{Somebody}}
\date{Some Date}

\begin{document}

\begin{titlepage}
\maketitle
\end{titlepage}

\tableofcontents

\begin{abstract}
This is a simple paragraph at the beginning of the 
document. A brief introduction about the main subject.
\end{abstract}

First document. This is a simple example, with no 
extra parameters or packages included.

% Comments 
\begin{comment}
This text won't show up in the compiled pdf
this is just a multi-line comment. Useful
to, for instance, comment out slow-rendering
while working on the draft.
\end{comment}

\end{document} 

正文

在\begin{document}和\end{document }之间的是文档的正文内容。

导言

在\begin{document}之前的命令称为preamble(导言)。 在preamble中通常定义文档的格式、语言等。例如:

\documentclass[12pt, letterpaper]{article}

设置LaTex文件所生成文档的格式

\usepackage[utf8]{inputenc}

设置LaTex文件保存时使用的编码格式

\usepackage{comment}

设置在编译LaTex文件时依赖的扩展包

\title{Document Title}
\author{Nobody \thanks{Somebody}}
\date{Some Date}

设置所要生成文档的封面内容: 文档名,作者,日期等。

注释

% Comments 

从百分号%开始到这一个行结束的部分是LaTex文件的注释内容,不在编译后生成的pdf文档中显示。
在\begin{comment}和\end{comment }之间也不在编译后生成的pdf文档中显示。

使用中文

要在Latex环境中使用中文,首先要选择tex文件的编码(encoding)方式,常用的有UTF-8和GBK等。在Windows中,使用WinEdt编辑UTF-8格式tex文件时,修改配置文件(Options -> Options Interface -> Language, Unicode, Sorting -> Unicode(UTF-8) Support )中UTF8FILTER项

UTF8FILTER="Tex;UTF-8|ACP;EDT;INI;"

将WinEdt设置为默认使用UTF8格式打开tex文件。
如果WinEdt打开一个UTF-8格式的文件显示乱码,可在Document->Document Setting->Format->File Format中选择UTF-8
或者在在该文件的开始处添加一行

% !Mode:: "TeX:UTF-8"

Latex环境中使用中文需要注意的一点是: Latex编辑器读入文件的使用的编码格式一定要与该文件的编码格式一致。

1) 使用CJK中文包(UTF8格式)

\documentclass[UTF8]{article}
\usepackage{CJK}  
\begin{document}
\begin{CJK}{UTF8}{song}
Hello, World!

世界, 你好!

\emph{世界, 你好!}

\textbf{世界, 你好!}
\end{CJK}
\end{document}

第一行 \documentclass 后的 [ U T F 8 ] 指定编辑器按照UTF8格式读入该文件,所以也要以UTF8格式保存该文件。

2) 使用CTEX中文包(UTF8格式)

\documentclass[UTF8]{article}
\usepackage{CTEX}

\begin{document}

\section{字体设置}

{\kaishu 楷体}

{\songti 宋体}

{\heiti 黑体}

{\fangsong 仿宋}

{\lishu 隶书}

{\youyuan 幼圆}  

\end{document} 

pdf文档效果
这里写图片描述

3) 使用CJK中文包(GBK格式)

\documentclass{article}
\usepackage{CJK}  
\begin{document}
\begin{CJK*}{GBK}{song}
Hello, World!

世界, 你好!

\emph{世界, 你好!}

\textbf{世界, 你好!}
\end{CJK*}
\end{document}

LaTex命令模块简介

封面

\begin{titlepage}
\maketitle
\end{titlepage}

按照在preamble中设置的封面格式生成文档封面

目录

\tableofcontents

生成文档目录

页码

     \pagenumbering{digit type}
  • arabic 阿拉伯数字(1,2,3,4),默认样式
  • roman 小写罗马数字(i,ii,iii,iv)
  • Roman 大写罗马数字(I,II,III,IV)
  • alph 小写拉丁字母(a,b,c,d)
  • Aiph 大写拉丁字母(A,B,C,D)
  \setcounter{page}{page number}

文件拆分

当文档内容的结构复杂,需要保存在多个LaTex文件时,可以使用\input 或\include命令构建文档结构。例如:

\input{filename1}

或者

\include{filename1}

[6]”\input{filename} imports the commands from filename.tex into the target file; it’s equivalent to typing all the commands from filename.tex right into the current file where the \input line is.”

[6]”\include{filename} essentially does a \clearpage before and after \input{filename}, together with some magic to switch to another .aux file, and omits the inclusion at all if you have an \includeonly without the filename in the argument. This is primarily useful when you have a big project on a slow computer; changing one of the include targets won’t force you to regenerate the outputs of all the rest.”

[6]”\include{filename} gets you the speed bonus, but it also can’t be nested, can’t appear in the preamble, and forces page breaks around the included text.”

图片插入及引用

下面样例展示如何在文档中插入图片并在文中通过图片编号引用图片。

\usepackage[pdftex]{graphicx}
% 设置图片文件存放路径
\graphicspath{{../figures}  

\begin{document}
% 在正文中引用图片时使用\ref 
  In Figure \ref{fig:foo} 
\begin{figure}
%设置对齐格式
\centering   %图片居于页面中部
%指定图形大小和图形名称  
\includegraphics [width=0.8,height=2.5]{foo.png} 
%设置标题 
\caption{Foo} 
%设置图形引用名称
\label{fig:foo} 
\end{figure}

\end{document}

有关插入图形的更多设置请参阅[9]

数学公式

按照数学公式在文中的位置可分为两种: 行中公式和独立公式.

行中公式

行中公式可使用如下两种形式:

毕达哥拉斯定理 \begin{math} x^{2}+y^{2}=z^{2} \end{math}又称勾股定理。

毕达哥拉斯定理 $ x^{2}+y^{2}=z^{2} $又称勾股定理。

使用上面两种方法编译后得到的文档是一样的,具体如下图所示:
这里写图片描述

独立公式

独立公式可使用

$$
v = v^{1}e_{1} + v^{2}e_{2} + v^{3}e_{3} = v^{i}e_{i}, i = 1,2,3
$$
或
\begin{equation}
v = v^{1}e_{1} + v^{2}e_{2} + v^{3}e_{3} = v^{i}e_{i}, i = 1,2,3
\end{equation}

效果如下图所示:
这里写图片描述
由上图可看出,使用 {equation} 命令默认带公式编号,使用 $$ 命令默认不带公式编号。

引用公式

在文中引用公式编号可使用以下方式

\begin{equation}\label{eq:Pythagorean theorem}
x^{2}+y^{2}=z^{2}
\end{equation}
公式\ref{eq:Pythagorean theorem}是毕达哥拉斯定理,在中国又称勾股定理。

效果如下:
这里写图片描述

在数学公式中插入中文

使用\mbox{}可在数学公式中插入中文。

$$
\mbox{例如:} x_{1}, x_{2}, \cdots, x_{N} 
$$ 

效果如下图:
这里写图片描述

多行公式

$$
\left [
\begin{array}{cc}
v^{i} e_{i} & v^{i} e_{j}  \\
v^{j} e_{i} & v^{j} e_{j}  \\
\end{array}
\right ]
$$

效果如下图所示:
这里写图片描述

下标

1) 一般数学公式中下标可以使用

$$ s = \int_{a}^{b} |\dot{x}(t)| dt  $$

2) 有时需要将下标放在正下方, 如果是数学符号,可使用 \limits 命令

$$ r'(t) = \lim \limits_{\triangle t \rightarrow 0} \frac{ r(t + \triangle t) - r(t)}{ \triangle t }$$

这里写图片描述

3) 如果是普通符号,那么要用 \mathop 先转成数学符号再用$\limits$

根号

The square root of 100 is $\sqrt{100}=10$. 
\\
The cubic root of 64 is $\sqrt[3]{64}=4$.

得到的文档如下图所示
这里写图片描述

使用参考文献

Latex中参考文献命令格式是[7]:

 \begin{thebibliography}{9}
\bibitem{latexcompanion} 
Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
\textit{The \LaTeX\ Companion}. 
Addison-Wesley, Reading, Massachusetts, 1993.

\bibitem{einstein} 
Albert Einstein. 
\textit{Zur Elektrodynamik bewegter K{\"o}rper}. (German) 
[\textit{On the electrodynamics of moving bodies}]. 
Annalen der Physik, 322(10):891–921, 1905.

\bibitem{knuthwebsite} 
Knuth: Computers and Typesetting,
\\\texttt{http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html}
\end{thebibliography}

注意:\bibitem命令中 参考文献引用名中可以有空格。

Bibtex是常用的参考文献管理工具。Bibtex使用的参考文献格式如下所示[7][8]:

@article{name1,
     author = {Auther 1 and Auther 2},
     title = {The \LaTeX\ Companion},
     journal = {Journal name},
     volume = {49},
     pages = {409-436},
     year = {19xx},
     abstract = {摘要主要是给自己参考, 这一项不是必须的}
}
@book{name2,
     author = {Auther 1 and Auther 2},
     title = {The TeX Book},
     publisher = "Springer",
     year = {19xx},
     abstract = {摘要}
}
@article{einstein,
    author =       "Albert Einstein",
    title =        "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
        [{On} the electrodynamics of moving bodies]",
    journal =      "Annalen der Physik",
    volume =       "322",
    number =       "10",
    pages =        "891--921",
    year =         "1905",
    DOI =          "http://dx.doi.org/10.1002/andp.19053221004"
}

@book{latexcompanion,
    author    = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
    title     = "The \LaTeX\ Companion",
    year      = "1993",
    publisher = "Addison-Wesley",
    address   = "Reading, Massachusetts"
}

@misc{knuthwebsite,
    author    = "Donald Knuth",
    title     = "Knuth: Computers and Typesetting",
    url       = "http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html"
}

@article{article,
  author  = {Peter Adams}, 
  title   = {The title of the work},
  journal = {The name of the journal},
  year    = 1993,
  number  = 2,
  pages   = {201-213},
  month   = 7,
  note    = {An optional note}, 
  volume  = 4
}

@book{book,
  author    = {Peter Babington}, 
  title     = {The title of the work},
  publisher = {The name of the publisher},
  year      = 1993,
  volume    = 4,
  series    = 10,
  address   = {The address},
  edition   = 3,
  month     = 7,
  note      = {An optional note},
  isbn      = {3257227892}
}

@booklet{booklet,
  title        = {The title of the work},
  author       = {Peter Caxton}, 
  howpublished = {How it was published},
  address      = {The address of the publisher},
  month        = 7,
  year         = 1993,
  note         = {An optional note}
}

@conference{conference,
  author       = {Peter Draper}, 
  title        = {The title of the work},
  booktitle    = {The title of the book},
  year         = 1993,
  editor       = {The editor},
  volume       = 4,
  series       = 5,
  pages        = 213,
  address      = {The address of the publisher},
  month        = 7,
  organization = {The organization},
  publisher    = {The publisher},
  note         = {An optional note}  
}

@inbook{inbook,
  author       = {Peter Eston}, 
  title        = {The title of the work},
  chapter      = 8,
  pages        = {201-213},
  publisher    = {The name of the publisher},
  year         = 1993,
  volume       = 4,
  series       = 5,
  address      = {The address of the publisher},
  edition      = 3,
  month        = 7,
  note         = {An optional note}
}

@incollection{incollection,
  author       = {Peter Farindon}, 
  title        = {The title of the work},
  booktitle    = {The title of the book},
  publisher    = {The name of the publisher},
  year         = 1993,
  editor       = {The editor},
  volume       = 4,
  series       = 5,
  chapter      = 8,
  pages        = {201-213},
  address      = {The address of the publisher},
  edition      = 3,
  month        = 7,
  note         = {An optional note}
}

@manual{manual,
  title        = {The title of the work},
  author       = {Peter Gainsford}, 
  organization = {The organization},
  address      = {The address of the publisher},
  edition      = 3,
  month        = 7,
  year         = 1993,
  note         = {An optional note}
}

@mastersthesis{mastersthesis,
  author       = {Peter Harwood}, 
  title        = {The title of the work},
  school       = {The school of the thesis},
  year         = 1993,
  address      = {The address of the publisher},
  month        = 7,
  note         = {An optional note}
}

@misc{misc,
  author       = {Peter Isley}, 
  title        = {The title of the work},
  howpublished = {How it was published},
  month        = 7,
  year         = 1993,
  note         = {An optional note}
}

@phdthesis{phdthesis,
  author       = {Peter Joslin}, 
  title        = {The title of the work},
  school       = {The school of the thesis},
  year         = 1993,
  address      = {The address of the publisher},
  month        = 7,
  note         = {An optional note}
}

@proceedings{proceedings,
  title        = {The title of the work},
  year         = 1993,
  editor       = {Peter Kidwelly},
  volume       = 4,
  series       = 5,
  address      = {The address of the publisher},
  month        = 7,
  organization = {The organization},
  publisher    = {The name of the publisher},
  note         = {An optional note}
}

@techreport{techreport,
  author       = {Peter Lambert}, 
  title        = {The title of the work},
  institution  = {The institution that published},
  year         = 1993,
  number       = 2,
  address      = {The address of the publisher},
  month        = 7,
  note         = {An optional note}
}

@unpublished{unpublished,
  author       = {Peter Marcheford}, 
  title        = {The title of the work},
  note         = {An optional note},
  month        = 7,
  year         = 1993
}

将上述内容保存到一个.bib后缀文件,例如foo.bib
注意:bib文件中 参考文献引用名中不能有空格。

在preamble中添加(不加这个命令也能用)

\usepackage{cite}

在正文中引用时方法如下:

\begin{document}

\cite{name1}"The geometric definition of dot product is coordinate independent, ..."\cite{latexcompanion,knuthwebsite}

\medskip

%% IEEEtran是参考文献显示格式
\bibliographystyle{IEEEtran}
%% foo is the bib file name
\bibliography{foo} 

\end{document} 

注意
1) 应用多个文献时,例如:\cite{latexcompanion,knuthwebsite}, 参考文献引用名之间用逗号分隔,不能插入空格。
2) \bibliography{foo} 中使用文件名(本例中是foo),不能带.bib后缀.

常用的bibliographystyle有: abbrv, acm, alpha, apalike, ieetr, plain,siam, unsrt等。
BibTeX常用的项有[7]:
address annote author
booktitle chapter crossref
edition editor institution
journal key month
note number organization
pages publisher school
series title type
volume year URL
ISBN ISSN LCCN
abstract keywords price
copyright language contents

Beamer样例

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%    设置文档类型为 beamer
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\documentclass{beamer}
\usepackage{beamerthemesplit}

\title{Example Presentation Created with the Beamer Package}
\author{Till Tantau}
\date{\today}

\begin{document}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%    TITLE PAGE
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\frame{\titlepage}

\section*{Outline}

\frame{\tableofcontents}

\section{Introduction}
\subsection{Overview of the Beamer Class}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%    使用 frame 命令生成一页
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\frame
{
    \frametitle{Features of the Beamer Class}
    \begin{itemize}
    \item<1-> Normal LaTeX class.
    \item<2-> Easy overlays.
    \item<3-> No external programs needed.
    \end{itemize}
}

\begin{frame}{Metropolis title formats}
	  supports 4 different title formats:
	\begin{itemize}
		\item Regular
		\item \textsc{Small caps}
		\item \textsc{all small caps}
		\item ALL CAPS
	\end{itemize}
	They can either be set at once for every title type or individually.
\end{frame}

\end{document}

LaTeX文件编译

常用的LaTeX文件编辑软件有WinEdt等,也可以自己动手编译,例如使用Windows的批处理,使用Visual Studio的make等.
1) 使用WinEdt
Shift+Ctrl+P

2)Windows 10中使用批处理编译
使用 Windows的 记事本(在文件资源管理器中打开文件夹(如D:\foo), 点击鼠标右键->新建->文本文档), 也可以使用别的编译软件如Notepad++等。写入以下命令

pdflatex %1 
bibtex %1 
pdflatex %1 
pdflatex %1 
start %1.pdf

保存为bat文件(可以命名为foo.bat)
开始->Windows附件->命令提示符,输入以下命令

cd d:\foo 
foo.bat

3) Visual Studio的make
下面是一例用于编译LaTeX文件的Makefile文件:

BINPATH = D:\CTEX\MiKTeX\miktex\bin\  

TeX = $(BINPATH)\pdflatex.exe   
TeX_FLAGS = -shell-escape -interaction=nonstopmode -file-line-error  
PRE =  $(TeX) -ini -job-name="preamble" "&pdflatex preamble.tex\dump"  
BIB = $(BINPATH)\bibtex.exe  

FileName = main  

all: $(FileName).pdf  

main.pdf: $(FileName).tex   
    $(TeX) $(TeX_FLAGS) $(FileName).tex  
    $(BIB) $(FileName).tex  
    $(TeX) $(FileName).tex  
    $(TeX) $(FileName).tex  

clean:   
    del $(FileName).pdf  
    del $(FileName).log  

常见编译错误

“豢\documentclass{article}”
在Windows系统中,当文档中需要插入中文字符时,需将Latex文件使用UTF-8编码保存。如果使用Windows系统自带的记事本编辑Latex文件,经常遇到下面的编译错误:

豢\documentclass{article}

出现这种编译错误的原因是因为 Windows 会在 UTF 编码的文件最开始加一个 BOM。

解决办法:
不要使用Windows系统自带的记事本保存.tex文件,直接使用别的编辑软件(例如WinEdt, Notepad++等)创建.tex文件。

“Too Many Unprocessed Floats”
如果一浮动对象不能被立即处理,它就会被放到未处理的浮动对象队列中,
Latex中这个队列一般只保存18个未处理浮动对象,当未处理的浮动对象的数目过多时,就会出现”Too Many Unprocessed Floats”错误[10]。

解决办法:
1) 调整文本
2) 在浮动图形对象之间加入 \clearpage
3) 将浮动图形对象改为非浮动图形对象[11]

使用WinEdt打开UTF-8格式文件,文件的中文字符显示为乱码
解决办法:
在Document->Document Setting->Format->File Format中选择UTF-8格式
或者在在该文件的开始处添加一行

% !Mode:: "TeX:UTF-8"

参考文献

[1] https://en.wikipedia.org/wiki/TeX
[2] http://hubl82.blog.163.com/blog/static/12676948520134593321565/
[3] https://www.shareLaTex.com/learn/Creating_a_document_in_LaTex
[4] http://www.ctex.org/HomePage/
[5] https://www.LaTex-project.org
[6] https://tex.stackexchange.com/questions/246/when-should-i-use-input-vs-include
[7] https://www.sharelatex.com/learn/Bibliography_management_with_bibtex
[8] https://www.verbosus.com/bibtex-style-examples.html
[9] http://www.ctex.org/documents/latex/graphics/node61.html
[10] http://www.ctex.org/documents/latex/graphics/node66.html
[11] http://www.ctex.org/documents/latex/graphics/node87.html#chap:nonfloat
[12] User’s Guide to the Beamer Class. http://www.tuteurs.ens.fr/noncvs/docs/beamer/beameruserguide.pdf
[13] https://www.r-bloggers.com/create-your-own-beamer-template/
[14] https://hamaluik.com/posts/better-beamer-themes/

猜你喜欢

转载自blog.csdn.net/cocoonyang/article/details/78036326