LaTex 入门

LaTex简介

TeX 是由Donald Knuth创造的基于底层编程语言的电子排版系统[1](TEX是Honeywell公司在1980年为其Text Executive文本处理系统注册的商标,它与 TeX是两回事)。使用TeX可以对版面做精细的操作,生成精美的文档。TeX提供版面编辑的底层命令,但是使用时操作复杂,工作效率不高. TeX允许用这些底层命令定义出可以执行复杂操作的扩展命令. LaTex是由Leslie Lamport开发的的TeX扩展命令集合[5]。LaTexj通过整合常用的版面设置操作,降低排版的工作量和难度。 LaTex 强大的排版功能,特别是对科技文档的支持,已经使它成为撰写科研论文的事实上的标准[5]。

TeX系统生成的是DVI(Device Independent)文件。随着pdf文件格式的流形,LaTex在支持DVI文件格式的同时也可生成PDF格式文件。

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 命令架构

Tex环境中,字符,图片等内容都放在隐形的盒子(box)容器内。这些盒子根据Tex和Latex的默认规则以及相关命令粘结成更大的盒子。字符组合成单词,单词组合出语句。语句在放入页面盒子时,根据版面格式分解成行列。

版面格式布置盒子在页面中的位置,字符等文档内容的位置是由它们在盒子中相对坐标确定。例如,一个字符盒子如下图[20]所示。
在这里插入图片描述[]1
width是盒子的宽度。基线(baseline)将盒子分割成上下两部分。height和depth分别确定这两部分的高度。 字符盒子基于基线相互粘结。

LaTex提供不同层次的命令帮助用户设置文档版面。它最底层是大约300个TeX基础命令,例如 \hbox, \vskip等。

LaTex的核心是一组建立在TeX基础命令上的宏命令,例如 \documentclass, \usepackage, \begin.等。

再往上就是类文件,例如foo.cls等。当\documentclass{foo}时,就是加载foo.cls文件。

再往上是扩展包, 例如pic.sty等。我们可以使用\usepackage命令加载扩展包。

安装

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文件。

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} 

LaTex文件结构

一个LaTex文件分为两个部分:导言和正文。 在\begin{document}和\end{document }之间的是文档的正文内容。 在\begin{document}之前的命令称为preamble(导言)。 在preamble中通常定义文档的格式、语言等。上文所示LaTex文档其结构如下图所示:
这里写图片描述

文件拆分

当文档内容的结构复杂,需要分别保存在多个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.”

导言部分 LaTex命令简介

preamble(导言)部分通常用于定义文档的格式、语言等。常用的LaTex命令主要有\documentclass,\usepackage等。下面分别对几个常用的命令做简单介绍。

\documentclass

\documentclass命令是用于设置LaTex文件所生成文档的格式. 其命令语法如下所示:

\documentclass[options]{class} 

常用的文档格式有:

Class 简介
article 科技论文,报告,软件文档等
IEEEtran IEEE Transactions 格式.
proc 法律文书
report 长篇报告(如:学位论文)
book 书籍
slides 幻灯片
memoir 传记
letter 信件
beamer PPT

文档格式修正项

Options 简介
12pt 文档正文使用的字体大小(默认为10pt)
a4paper, letterpaper 页面规格(默认为letterpaper或a4paper)
fleqn Typesets displayed formulas left-aligned instead of centered.
leqno Places the numbering of formulas on the left hand side instead of the right.
titlepage, notitlepage Specifies whether a new page should be started after the document title or not. The article class does not start a new page by default, while report and book do.
twocolumn Instructs LaTeX to typeset the document in two columns instead of one.
twoside, oneside Specifies whether double or single sided output should be generated. The classes article and report are single sided and the book class is double sided by default. Note that this option concerns the style of the document only. The option twoside does not tell the printer you use that it should actually make a two-sided printout.
landscape Changes the layout of the document to print in landscape mode.
openright, openany Makes chapters begin either only on right hand pages or on the next page available. This does not work with the article class, as it does not know about chapters. The report class by default starts chapters on the next page available and the book class starts them on right hand pages.
draft makes LaTeX indicate hyphenation and justification problems with a small square in the right-hand margin of the problem line so they can be located quickly by a human. It also suppresses the inclusion of images and shows only a frame where they would normally occur.

例如:

\documentclass[12pt, a4paper]{article}

意思是正文字体大小为12pt, 页面规格是A4, 使用article文档格式.

\usepackage

\usepackage命令设置在编译LaTex文件时要导入的扩展包,例如:

\usepackage[utf8]{inputenc}
\usepackage{comment}

封面格式

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

设置所要生成文档的封面内容: 文档名,作者,日期等(这只是设置了封面格式,生成封面的是\maketitle命令)。

注释

% Comments 

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

保留字符

# $ % ^ & _ { } \

这些字符(reserved characters)在Latex中有特殊的意义,要想在生成的文档中显示这些字符,Latex文档中这些字符前加反斜杠" \ \backslash \" :

\#   \$   \%   \^   \&   \_  \{   \} 

因为两个反斜杠"\\"在Latex中是换行命令,可使用

$\backslash$

输出反斜杠 \ \backslash \

~波浪线(tidle)在LaTeX中是插入空格命令,可用数学公式环境的$ \sim $向TeX文档中插入波浪线.

正文部分LaTex命令模块简介

生成封面

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

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

生成目录

\tableofcontents

生成文档目录

页码

设置页码数字类型

\pagenumbering{digit type}

其中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}

如果想让当前页不标页码,可使用

\thispagestyle{empty}

例如,如果在封面不标记页码,目录页使用小写罗马数字标记页码,正文部分使用阿拉伯数字标记页码

\begin{document}

%% Making title pate
\begin{titlepage}
\maketitle
\thispagestyle{empty} 
\end{titlepage}

%% Contents
\pagenumbering{roman}
\tableofcontents

\newpage
\pagenumbering{arabic}
\setcounter{page}{1} 

版面设置

空白

下面是LaTex插入空格命令小结[24]

\,    inserts a \thinspace (equivalent to .16667em) in text mode, 
      or \thinmuskip (equivalent to 3mu) in math mode;
\!    inserts a negative \thinmuskip in math mode;
\>    inserts \medmuskip (equivalent to 4.0mu plus 2.0mu minus 4.0mu) in math mode;
\;    inserts \thickmuskip (equivalent to 5.0mu plus 5.0mu) in math mode;
\:    is equivalent to \> (see above);
\enspace   inserts a space of .5em in text or math mode;
\quad      inserts a space of 1em in text or math mode;
\qquad     inserts a space of 2em in text or math mode;
\kern <len>      inserts a skip of <len> (may be negative) in text or math mode (a plain TeX skip);
\hskip <len>   (similar to \kern);
\hspace{<len>}   inserts a space of length <len> (may be negative) in math or text mode (a LaTeX \hskip);
\hphantom{<stuff>}   inserts space of length equivalent to <stuff> in math or text mode. 
      Should be \protected    when used in fragile commands (like \caption and sectional headings);
\     inserts what is called a "control space" (in text or math mode);
    (直接键入空格) inserts an inter-word space in text mode (and is gobbled in math mode). 
    Similarly for \space and { }.
~     inserts an "unbreakable" space (similar to an HTML &nbsp;) (in text or math mode);
\hfill    inserts a so-called "rubber length" or stretch between elements (in text or math mode). 
          Note that you may need to provide a type of anchor to fill from/to;  

段落行间距

使用{setspace}扩展包. 例如

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

\begin{document}

%%双倍行间距 
\begin{spacing}{2.0}
段落内容。
\end{spacing}

%%单倍行间距 
\begin{spacing}{1.0}
段落内容。
\end{spacing}

\end{document}

段落间空白

\hspace{1cm}
\vspace{5pt}

居中

\begin{center} 
...
\end{center}

左对齐

\begin{flushleft}
...
\end{flushleft}

右对齐

\begin{flushright}
...
\end{flushright}

样例

\begin{center} 
登鹳雀楼 \\
~~~~~~~~ {\footnotesize 王之涣} \\
\hfill \\ 
白日依山尽 \\
黄河入海流 \\ 
欲穷千里目 \\
更上一层楼 \\
\end{center}

\begin{flushleft}
这一行在左侧 \\ 
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值