通过写一些短文就可以很容易掌握 LaTeX 基础。让我们从 Opensource.com介绍页面借用一下内容,创建一个示例:
$ cat about.tex \documentclass{article}\begin{document}Opensource.com is a premier, daily publication focused onopen source and Linux tutorials, stories, and resources.We're a diverse and inviting group, made up of staffeditors, Correspondents, contributors, and readers. Wevalue differences in skills, talents, backgrounds, andexperiences. There are a few different ways to get involvedas a reader or a writer.\end{document}
类似其他文档格式程序, LaTeX 会将单词汇集起来,填充成段落 。这意味着你可以在段落中间添加新文本,而不用担心最终文档的段落参差不齐。只要你不在段落中添加空行, LaTeX 就会创建完全对齐的段落。当它找到一个空行时, LaTeX 会开启一个新段落。
使用 LaTeX 的 latex命令处理文档:
$ latex about.texThis is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=latex) restricted \write18 enabled.entering extended mode(./about.texLaTeX2e patch level 4(/usr/share/texlive/texmf-dist/tex/latex/base/article.clsDocument Class: article 2020/04/10 v1.4m Standard LaTeX document class(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)No file about.aux.[1] (./about.aux) )Output written on about.dvi (1 page, 736 bytes).Transcript written on about.log.
LaTeX 会输出许多文本,这样你就可以知道它在干什么。若你的文档包含错误, LaTeX 会报错并提示它可以做什么。大多数情况下,你可以在提示后输入 exit来强制退出 LaTeX 。
如果用 LaTeX 成功生成一个文档,会生成一个带 .dvi后缀的文件。DVI表示 “设备无关Device Independent”,因为你可以使用不同的工具来生成其他格式。例如,dvipdf程序可以将 DVI 文件转换为 PDF 文件。
$ dvipdf about.dvi
添加列表
LaTeX 支持两种列表:一种以数字开头的 “枚举” 列表,一种 “逐项” 或 “项目符号” 列表。在第二段后添加一个简短的枚举列表,列出人们可以参与 Opensource.com的方式:
\begin{enumerate}\item Be a writer\item Be a reader\end{enumerate}
与在文档定义中添加 \begin和\end声明类似,你也需要在列表前后添加\begin和\end声明。在列表中,每个项目以\item命令开始。当你用 LaTeX 处理该文档并转换为 PDF 格式后,你会看到该列表为数字列表:
你也可以在列表中嵌套列表。这是一个优雅的功能,如果你需要在列表中为每个条目添加选项。例如,你可以为想要在 Opensource.com中成为作者的人们提供一些不同的资源。嵌入列表使用单独的\begin和\end声明。为了看起来方便,我在示例中添加了空行,但是 LaTeX 会忽略这些空行:
\begin{enumerate}\item Be a writer \begin{itemize} \item Resources for writers \item Contributor Club \item Correspondent Program \end{itemize}\item Be a reader\end{enumerate}
作为嵌套列表,新列表嵌入在编号 1 的项目中,因为你在原先的 \item声明之间添加了列表。你可以通过在\end{enumerate}语句前添加新列表,作为编号 2 项目的嵌套列表。
章节和小节
你可以将冗长文章分成多个章节,这样更易于阅读。使用 \section{...}语句在大括号内添加章节标题。例如,你可以在文档顶部添加一个标题为 “AboutOpensource.com” 的新章节:
$ head about.tex \documentclass{article}\begin{document}\section{About Opensource.com}Opensource.com is a premier, daily publication focused onopen source and Linux tutorials, stories, and resources.We're a diverse and inviting group, made up of staffeditors, Correspondents, contributors, and readers. We
article文档类会在每个主要章节添加编号,并使字体变大来突出显示。
你可以使用 \subsection{...}命令来组织文档。就像\section{...}命令一样,在大括号中输入副标题名称。
$ head about.tex\documentclass{article}\begin{document}\section{About Opensource.com}Opensource.com is a premier, daily publication focused onopen source and Linux tutorials, stories, and resources.\subsection{Welcome to the Opensource.com community}
将短语“staff editors, Correspondents, contributors, and readers”放在斜体文本中,并将特定词“reader”和“writer”放在段落后面的强调文本中。你也可以将“skills, talents, backgrounds, and experiences”加粗。虽然这不是正确的样式设置方式,但你可以使用小型大写字母来键入 “Linux” 。
$ head -20 about.tex \documentclass{article}\begin{document}\title{About Us}\author{Opensource.com Editors}\date{July 10, 2022}\maketitle\section{About Opensource.com}Opensource.com is a premier, daily publication focused onopen source and \textsc{Linux} tutorials, stories, and resources.\subsection{Welcome to the Opensource.com community}We're a diverse and inviting group, made up of \textit{staffeditors, Correspondents, contributors, and readers}. Wevalue differences in \textbf{skills, talents, backgrounds, andexperiences}. There are a few different ways to get involvedas a \emph{reader} or a \emph{writer}.
该示例展示了不同样式的文本的应用方法。当你需要强调时,使用 \emph{...}命令,将强调主题放在大括号内。要以斜体、粗体或小型大写字母显示文本,使用\text命令的变体:\textit{...}用于斜体,\textbf{...}用于粗体,以及\ textsc{...}用于小型大写字母。LaTeX 支持许多其他方式来设置文本样式,这些样式有助于你编写科学技术类文章。