Latex学习
本文最后更新于:1 年前
这里是Latex相关的学习。近日进研究生组,需要使用Latex去做很多paper work,
Latex Introduction
- a tool for typesetting professional-looking documents
- TeX engine uses the commands embedded in your text file to guide and control the typesetting process, converting the LaTeX commands and document text into a professionally typeset PDF file.
Reasons for Learning Latex
Arguments in favour of LATEX include:
support for typesetting extremely complex mathematics, tables and technical content for the physical sciences;
facilities for footnotes, cross-referencing and management of bibliographies;
ease of producing complicated, or tedious, document elements such as indexes, glossaries, table of contents, lists of figures;
being highly customizable for bespoke document production due to its intrinsic programmability and extensibility through thousands of free add-on packages.
总结:好用、功能多、可复用模版、免费,可以多人协作编辑
An Example
- Latex代码例子:
1 |
|
- Latex代码效果:
多种Topic: https://www.ctan.org/topic/class,Different types of documents require different classes; i.e., a CV/resume will require a different class than a scientific paper which might use the standard LATEX
article
class. Other types of documents you may be working on may require different classes such asbook
orreport
. 不同类型的需求,可以使用不同的topic昂!
Document’s Preamable
按照惯例latex文件后缀为:
.tex
,main.tex是默认的主文件。.tex
文件中出现在\begin之前的所有内容都称为序言,它充当文档的“设置”部分。可以定义文档类别(类型)以及诸如编写文档时要使用的语言等细节,加载您想要使用的包等一个小例子:
1 |
|
- documentclass
\documentclass[12pt, letterpaper]{article}
defines the overall class (type) of document.- Additional parameters, which must be separated by commas, are included in square brackets (
[...]
) and used to configure this instance of the article class:12pt
sets the font sizeletterpaper
sets the paper size
- 9pt, 11pt, 12pt, can be used, but if none is specified, the default size is 10pt. Reference: https://www.overleaf.com/learn/latex/Page_size_and_margins
- usepackage
- an example of loading an external package.
- 引入不同的包会有不同的效果,参考:https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes#Finding_and_using_LaTeX_packages
Including title, author and date information
\title{My first LaTeX document}
: the document title\author{Hubert Farnsworth}
: here you write the name of the author(s) and, optionally, the \thanks command within the curly braces:\thanks{Funded by the Overleaf team.}
: can be added after the name of the author, inside the braces of theauthor
command. It will add a superscript and a footnote with the text inside the braces. Useful if you need to thank an institution in your article.
\date{August 2022}
: you can enter the date manually or use the command\today
to typeset the current date every time the document is compiledmaletitle and show:
1 |
|
Adding Comments
A LATEX comment is a section of text that will not be typeset or affect the document in any way—often used to add “to do” notes; include explanatory notes; provide in-line explanations of tricky macros or comment-out lines/sections of LaTeX code when debugging.
- An example
1 |
|
Bold, italics and underlining
- Bold: bold text in LaTeX is typeset using the
\textbf{...}
command. - Italics: italicised text is produced using the
\textit{...}
command. - Underline: to underline text use the
\underline{...}
command. - emph: to emphasize content depending on the context. Inside normal text, the emphasized text is italicized, but this behaviour is reversed if used inside an italicized text.
1 |
|
效果:
- Note: some packages, such as Beamer, change the behaviour of the
\emph
command.
Adding images
note that you need to upload images to your Overleaf project first.
- needs an add-on package which provides the commands and features required to include external graphics files.
\includegraphics{...}
to import graphics and\graphicspath{...}
to advise LATEX where the graphics are located.
1 |
|
The
\includegraphics{universe}
command does the actual work of inserting the image in the document. Here,universe
is the name of the image file but without its extension.Note:
- Although the full file name, including its extension, is allowed in the
\includegraphics
command, it’s considered best practice to omit the file extension because it will prompt LATEX to search for all the supported formats.- Generally, the graphic’s file name should not contain white spaces or multiple dots; it is also recommended to use lowercase letters for the file extension when uploading image files to Overleaf.
Captions, labels and references
1 |
|
\includegraphics[width=0.75\textwidth]{mesh}
: This form of\includegraphics
instructs LATEX to set the figure’s width to 75% of the text width—whose value is stored in the\textwidth
command.\caption{A nice plot.}
: As its name suggests, this command sets the figure caption which can be placed above or below the figure. If you create a list of figures this caption will be used in that list.\label{fig:mesh1}
: To reference this image within your document you give it a label using the\label
command. The label is used to generate a number for the image and, combined with the next command, will allow you to reference it.\ref{fig:mesh1}
: This code will be substituted by the number corresponding to the referenced figure.
Creating lists in LATEX
You can create different types of list using environments, which are used to encapsulate the LATEX code required to implement a specific typesetting feature. An environment starts with
\begin{*environment-name*}
and ends with\end{*environment-name*}
where*environment-name*
might befigure
,tabular
or one of the list types:itemize
for unordered lists orenumerate
for ordered lists.
Unordered lists
1 |
|
Ordered lists
1 |
|
Adding math to LATEX
LATEX provides two writing modes for typesetting mathematics:
inline math mode used for writing formulas that are part of a paragraph
display math mode used to write expressions that are not part of a text or paragraph and are typeset on separate lines
Inline math mode
- To typeset inline-mode math you can use one of these delimiter pairs:
\( ... \)
,$ ... $
or\begin{math} ... \end{math}
,Examples:
1 |
|
1 |
|
Display math mode
- Equations typeset in display mode can be numbered or unnumbered:
1 |
|
- To typeset display-mode math you can use one of these delimiter pairs:
\[ ... \]
,\begin{displaymath} ... \end{displaymath}
or\begin{equation} ... \end{equation}
. Historically, typesetting display-mode math required use of$$
characters delimiters, as in$$ *... display math here ...*$$
, but this method is no longer recommended: use LaTeX’s delimiters\[ ... \]
instead.
More complete examples
- Complex Example:
1 |
|
uses the
equation*
environment which is provided by theamsmath
package, so we need to add the following line to our document preamble:1
\usepackage{amsmath}% For the equation* environment
https://www.overleaf.com/learn/latex/Aligning_equations_with_amsmath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64\documentclass{article}
\usepackage{amsmath}% For the equation* environment
\begin{document}
\section{First example}
The well-known Pythagorean theorem \(x^2 + y^2 = z^2\) was proved to be invalid for other exponents, meaning the next equation has no integer solutions for \(n>2\):
\[ x^n + y^n = z^n \]
\section{Second example}
This is a simple math expression \(\sqrt{x^2+1}\) inside text.
And this is also the same:
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.
This is a simple math expression without numbering
\[\sqrt{x^2+1}\]
separated from text.
This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}
\ldots and this:
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}
![image-20230721215452982](https://cdn.jsdelivr.net/gh/alexanderliu-creator/blog_img/img/202307212154059.png)
- 写好数学公式的参考,The possibilities with math in LATEX are endless so be sure to visit our help pages for advice and examples on specific topics:
- [Mathematical expressions](https://www.overleaf.com/learn/latex/Mathematical_expressions)
- [Subscripts and superscripts](https://www.overleaf.com/learn/latex/Subscripts_and_superscripts)
- [Brackets and Parentheses](https://www.overleaf.com/learn/latex/Brackets_and_Parentheses)
- [Fractions and Binomials](https://www.overleaf.com/learn/latex/Fractions_and_Binomials)
- [Aligning Equations](https://www.overleaf.com/learn/latex/Aligning_equations_with_amsmath)
- [Operators](https://www.overleaf.com/learn/latex/Operators)
- [Spacing in math mode](https://www.overleaf.com/learn/latex/Spacing_in_math_mode)
- [Integrals, sums and limits](https://www.overleaf.com/learn/latex/Integrals%2C_sums_and_limits)
- [Display style in math mode](https://www.overleaf.com/learn/latex/Display_style_in_math_mode)
- [List of Greek letters and math symbols](https://www.overleaf.com/learn/latex/List_of_Greek_letters_and_math_symbols)
- [Mathematical fonts](https://www.overleaf.com/learn/latex/Mathematical_fonts)
# Basic document structure
## Abstracts
```latex
\documentclass{article}
\begin{document}
\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}
\end{document}
Paragraphs and new lines
- key problems:
- how a new paragraph is created by pressing the “enter” key twice, ending the current line and inserting a subsequent blank line;
- how to start a new line without starting a new paragraph by inserting a manual line break using the
\\
command, which is a double backslash; alternatively, use the\newline
command.
1 |
|
- tips: 建议不要使用多个 \ 或 \newlines 来“模拟”间距较大的段落,因为这会干扰 LATEX 的排版算法。 推荐的方法是继续使用空行创建新段落,不带任何 \,并通过在序言中添加 \usepackage{parskip} 来加载 parskip 包。
- Further information on paragraphs can be found in the following articles:
- Paragraphs and new lines
- How to change paragraph spacing in LaTeX
- LaTeX Error: There’s no line here to end provides additional advice and guidance on using
\\
.
Chapters and sections
Longer documents, irrespective of authoring software, are usually partitioned into parts, chapters, sections, subsections and so forth. LaTeX also provides document-structuring commands but the available commands, and their implementations (what they do), can depend on the document class being used. By way of example, documents created using the
book
class can be split into parts, chapters, sections, subsections and so forth but theletter
class does not provide (support) any commands to do that.
1 |
|
- Tips: Sections can be further divided into
\subsection{...}
and even\subsubsection{...}
. The numbering of sections, subsections etc. is automatic but can be disabled by using the so-called starred version of the appropriate command which has an asterisk (*) at the end, such as\section*{...}
and\subsection*{...}
. - LaTeX document classes provide the following sectioning commands, with specific classes each supporting a relevant subset:
\part{part}
\chapter{chapter}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\paragraph{paragraph}
\subparagraph{subparagraph}
- the
\part
and\chapter
commands are only available in thereport
andbook
document classes. - article about sections and chapters
Creating tables
- an example:
1 |
|
You must specify a parameter to this environment, in this case
{c c c}
which advises LATEX that there will be three columns and the text inside each one must be centred. You can also user
to right-align the text andl
to left-align it. The alignment symbol&
is used to demarcate individual table cells within a table row. To end a table row use the new line command\\
. Our table is contained within acenter
environment to make it centred within the text width of the page.
Adding borders
The tabular
environment supports horizontal and vertical lines (rules) as part of the table:
- to add horizontal rules, above and below rows, use the
\hline
command - to add vertical rules, between columns, use the vertical line parameter
|
1 |
|
1 |
|
Tip!
- Creating tables in LATEX can be time-consuming so you may want to use the TablesGenerator.com online tool to export LATEX code for tabulars.
Captions, labels and references
- You can caption and reference tables in much the same way as images. The only difference is that instead of the
figure
environment, you use thetable
environment.
1 |
|
Adding a Table of Contents
Creating a table of contents is straightforward because the command
\tableofcontents
does almost all the work for you
1 |
|
Sections, subsections and chapters are automatically included in the table of contents. To manually add entries, such as an unnumbered section, use the command
\addcontentsline
as shown in the example.
Downloading your finished document
On the web page download
Finding and using LaTeX packages
- LaTeX不仅提供了重要的排版功能,而且还通过使用附加包提供了可扩展性的框架。
Refer to: Adding images
- 然后引入这些包:
1 |
|
Loading packages
- packages are loaded in the document preamble via the
\usepackage
command but because (many) LATEX packages provide a set of options, which can be used to configure their behaviour
1 |
|
- The square brackets “
[...]
” inform LATEX which set of options should be applied when it loadssomepackage
. Within the set of options requested by the user, individual options, or settings, are typically separated by a comma; for example, the geometry package provides many options to configure page layout in LATEX, so a typical use ofgeometry
might look like this:
1 |
|
- If a LATEX package does not provide any options, or the user wants to use the default values of a package’s options, it would be loaded like this:
1 |
|
- When you write
\usepackage[...]{somepackage}
LATEX looks for a corresponding file called*somepackage*.sty
, which it needs to load and process—to make the package commands available and execute any other code provided by that package. If LATEX cannot find*somepackage*.sty
it will terminate with an error,
1 |
|
Finding information about packages: CTAN
- Packages怎么找,这里有些小妙招!
Packages are distributed through the Comprehensive TeX Archive Network, usually referred to as CTAN, which, at the time of writing, hosts 6287 packages from 2881 contributors. CTAN describes itself as
… a set of Internet sites around the world that offer TEX-related material for download.
You can browse CTAN to look for useful packages;
alphabetically (useful if you know the package name)
use the search facility (at the top of the page).
Packages available on Overleaf: Introducing TeX Live
- 发布还需要测试,可能导致CTAN上传的包在Tex Live中不可用。只有经过兼容性测试等工作,在Latex中成熟可用的时候,才会考虑在Tex Live中发布昂!给我的感觉就是:一个是仓库,一个是发行库。
Once per year a (large) subset of packages hosted on CTAN, plus LATEX-related fonts and other software, is collated and distributed as a system called TeX Live, which can be used to install your own (local) LaTeX setup. In fact, Overleaf’s servers also use TeX Live and are updated when a new version of TeX Live is released. Overleaf’s TeX Live updates are not immediate but take place a few months post-release, giving us time to perform compatibility tests of the new TeX Live version with the thousands of templates contained in our gallery. For example, here is our TeX Live 2022 upgrade announcement.
Although TeX Live contains a (large) subset of CTAN packages it is possible to find an interesting package, such as igo
for typesetting Go diagrams, which is hosted on CTAN but not included in (distributed by) TeX Live and thus unavailable on Overleaf. Some packages hosted on CTAN are not part of TeX Live due to a variety of reasons: perhaps a package is obsolete, has licensing problems, is extremely new (recently uploaded) or has platform dependencies, such as working on Windows but not Linux.
New packages, and updates to existing ones, are uploaded to CTAN all year round but updates to TeX Live are distributed annually; consequently, packages contained in the current version of TeX Live will not be as up-to-date as those hosted on CTAN. Because Overleaf’s servers use TeX Live it is possible that packages installed on our servers—i.e., ones available to our users—might not be the very latest versions available on CTAN but, generally, this is unlikely to be problematic.
References
- Latex Learn: https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes
- Latex Website: https://www.latex-project.org/
- Latex topics: https://www.ctan.org/topic/class
- Page size and margins: https://www.overleaf.com/learn/latex/Page_size_and_margins
- Finding and Using Latex packages: https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes#Finding_and_using_LaTeX_packages
- Including files in latex: https://www.overleaf.com/learn/how-to/Including_images_on_Overleaf
- Positioning of Figures
- Inserting Images
- Latex help list: https://www.overleaf.com/learn/latex/Lists
- amsmath help page: https://www.overleaf.com/learn/latex/Aligning_equations_with_amsmath
- The possibilities with math in LATEX are endless so be sure to visit our help pages for advice and examples on specific topics:
- new line is important:
- Paragraphs and new lines
- How to change paragraph spacing in LaTeX
- LaTeX Error: There’s no line here to end provides additional advice and guidance on using
\\
.
- article about sections and chapters
- TablesGenerator.com helps generating table in latex code
- Adding images
- geometry package