Introduction to Latex
Learning approach: Getting started with LaTex_ Beep beep beep_ bilibili
Operating environment: textlive2021, textstudio-4.1.2-win-qt6
1. Basic structure
The whole Latex file is divided into two parts. The first part is the introduction area and the second part is the body area. The details are as follows
% Introduction area \documentclass{article} %book, report, letter \title{Digital twin} \author{xiaxiang} \date{\today} % Display Chinese package \usepackage{ctex} % There is only one text area document environment \begin{document} % Give Way title Show \maketitle Section I \end{document}
2. Chinese processing
- Set editor, view options - Settings - build - default editor - XeLaTex
- Ensure that the Latex source file is in UTF-8 format to support Chinese. The options are in the lower right corner of the Texstudio editor
- Import Chinese macro package, \ usepackage{ctex}
View various related documents texdoc xxx
- texdoc ctex
- Texdoc lshort Zh, this material is an introductory tutorial for Latex Chinese, which is very good
3. Font setting
In Latex, a font has five attributes: font code, font family, font family, font shape and font size
The relevant settings are as follows
% Usually only 10-12 Pounds,[12pt] Represents the normal font size \documentclass{article}[12pt] %book, report, letter \usepackage{ctex} % It is recommended to use custom fonts instead of system fonts \newcommand{\myfont}{\textrm{\textmd}} % Yes and only one document environment \begin{document} % font family (Roman font, sans serif font, typewriter font) \textrm{Roman Family} \textsf{Sans Serif Family} \texttt{Typewriter Family} % Font scope {\rmfamily Roman Family} {\sffamily Sans Serif Family} {\ttfamily Typewriter Family} \sffamily Hello global family % Font family settings \textmd{Medium Series} \textbf{Boldface Series} {\mdseries Medium Series} {\bfseries Boldface Series} % Font shape \textup{Upright Shape} \textit{Italic Shape} %\textsl{Slanted Shape} \textsc{Small Caps Shape} %{\upshape Upright Shape} {\itshape Italic Shape} {\slshape Slanted Shape} {\scshapeSmall Caps Shape } % Chinese font {\songti Song typeface} \quad {\heiti Blackbody} \quad {\fangsong Imitation Song Dynasty} \quad {\kaishu regular script} % font size tiny scriptsize footnotesize small normalsize large Large LARGE huge Huge {\tiny Hello} {\zihao{5} Hello} \end{document}
For more detailed documents, please refer to texdoc ctex. One thing to note is that
- normal font size can be set in documentclass
- When using, you usually customize the font instead of directly using the system font
4. Text structure
- section, subsection and subsection constitute the text structure, and table of contents constitutes the table of contents
\begin{document} % catalogue \tableofcontents % \chapter{introduction} \section{introduction} % \par subsection \\ Line breaks usually use blank lines \section{Experimental method} \section{experimental result } \subsection{data} \subsection{Chart} \subsubsection{experimental condition} \subsubsection{Experimental process} \subsection{Result analysis} \section{conclusion} \section{thank} \end{document}
Blank lines can be segmented; \par can also be segmented, with two empty spaces at the beginning\ No segmentation, only line feed, and the beginning will not be empty
- You can also use \ chapter to divide into chapters, but at this time, the document category must be set to book. In this case, the subsection will be invalid
- If you want to customize the title style, you can set \ ctexset. See texdoc ctex for the specific implementation method
5. Special characters
- Blank match
% Blank lines are segmented, and multiple blank lines are equal to one % Auto indent, never use spaces instead % Multiple spaces in English are treated as one space, and spaces in Chinese will be ignored % The spacing between Chinese characters and other characters is automatically determined by XeLaTeX handle % Chinese full width spaces are prohibited \quad % 1em \qquad % 2em \, \thinspace % 1/6em \enspace % 0.5em \ % Space ~ % Hard space(Non separable spaces) \kern 1pc % 1pc=12pt=4.218mm Custom length spaces \kern -1em % Use negative sign length \\hskip 1em % custom \hspace{35pt} % custom \hphantom{xyz} % Space occupying width \hfill % Elastic length blank
- Control character
% Escape these matches \# \$ \% \{ \} \~{} \_{} \^{} \& \textbackslash % Escape \\,reason \\ Represents line feed
-
Typesetting symbols, \ S \P \dag \ddag \copyright \pounds
-
Identifier, \ tex {} \ latex {} \ latex {}
-
Quotation marks
% Single and double quotation marks on the left ` `` % Single and double quotation marks on the right ' ''
-
Hyphen ------
-
Non English characters \ oe \OE \ae \AE \aa \AA \o \O \l \L \ss \SS!
-
Accent, take o as an example
\`o \'o \^o \''o \~o \=o \.o \u{o} \v{o} \H{o} \r{o} \t{o} \b{o} \c{o} \d{o}
6. Illustrations
Use the illustration to install the macro package first, introduce the image path, and finally reference
- Use macro package and import picture path
% Import picture macro package \usepackage{graphicx} % You can specify multiple paths \graphicspath{{figures/}, {pics/}}
- Insert a picture without writing a picture suffix. It supports EPS, PDF, PNG, JPEG and BMP
\includegraphics{img2.jpg} % No suffix is OK \includegraphics{tiger} % Scale rotation \includegraphics[scale=0.3]{tiger}
Command details texdoc graphicx
7. Forms
The form has a special environment: tabular
\begin{document} % | Represents the vertical bar of the table || Double vertical line \begin{tabular}{|| l | c || c | c | p{1.5cm} ||} % hline Represents a horizontal line and two are double horizontal lines \hline full name & semantics & mathematics & Foreign Languages & remarks \\ \hline \hline Zhang San & 15 & 10 & 20 & good \\ Li Si & 20 & 5 & 8 & pass \\ Wang Wu & 15 & 10 & 20 & good \\ \end{tabular} \end{document}
Note that these symbols
- l c r p: l indicates left alignment, c indicates center alignment, r indicates right alignment, and p indicates the specified width
- \hline represents a horizontal line, and two consecutive lines are double horizontal lines| Indicates a vertical line, | | indicates a double vertical line
- &Split each item of data in a row
- \Indicates a line break
See texdoc booktab, texdoc longtab and texdoc tabu for details
8. Floating body
For example, images and tables have their own floating environment
% Usually only 10-12 pound \documentclass{article}[12pt] %book, report, letter \usepackage{graphicx} \graphicspath{{figures/}} \usepackage{ctex} % Yes and only one document environment \begin{document} This is a lovely cat (picture)\ref{fig-tiger}) \begin{figure}[htbp] \centering \includegraphics[scale=0.3]{tiger} \caption{tiger} \label{fig-tiger} \end{figure} Fundamentals of iconography (Fig\ref{fig-graphics}) \begin{figure}[htbp] \centering \includegraphics[scale=0.3]{img2} \caption{tiger} \label{fig-graphics} \end{figure} This is the grade sheet of class 5, grade 3\ref{tab-score}) \begin{table}[h] \centering \caption{Examination transcript} \label{tab-score} \begin{tabular}{|| l | c || c | c | p{1.5cm} ||} % hline Table stars, two are double horizontal lines \hline full name & semantics & mathematics & Foreign Languages & remarks \\ \hline \hline Zhang San & 15 & 10 & 20 & good \\ Li Si & 20 & 5 & 8 & pass \\ Wang Wu & 15 & 10 & 20 & good \\ \hline \end{tabular} \end{table} \end{document}
Explain the meaning of the code
- figure and table are floating layouts of tables and images respectively, which are used to set the positions of the two elements
- [htbp] indicates the allowable position, h, here; t. Page top; b. Bottom of page; p. A separate page; Default tbp
- \Centring means horizontal
- \caption indicates the title
- \label represents the representation of the current drawing and can be referenced elsewhere to achieve the effect of cross reference
The figure and table are calculated respectively, and the value will increase automatically from 1 to 2. Extension: Title Control (caption, bicapton, etc.), side-by-side and sub charts (subcapition, subfig, floatrow, etc.), wrapping (picinpar, wrapfig, etc.)
9. Mathematical formula
The typesetting content is divided into text mode and mathematical mode. Text mode is used for ordinary text typesetting, and mathematical mode is used for mathematical formula typesetting
\begin{document} \section{brief introduction} The typesetting content is divided into text mode and mathematical mode. Text mode is used for ordinary text typesetting, and mathematical mode is used for mathematical formula typesetting. \section{Inline formula} \subsection{Dollar sign} $a+b=c=d+f=e$ \subsection{parentheses} \(a+b=c=d+f=e\) \subsection{math environment} \begin{math} a+b=c=d+f=e \end{math} \section{Superscript and subscript} $x^3_1 + 3x^2_2 + 2x_{11} + 9 = 0$ \section{Greek alphabet} $\alpha$ $\beta$ $\gamma$ $\epsilon$ $\pi$ $\omega$ $\Gamma$ $\Delta$ $\Theta$ $\Pi$ $\alpha^2 + \beta^2 +\gamma^2 = 1$ \section{Mathematical function} $\log$ $\sin$ $\cos$ $\arcsin$ $\arccos$ $\ln$ $ y=\log_2{n} + \sqrt[3]{m} + \sin^-1$ \section{fraction} $a/b \frac{3}{8}$ \section{Line formula} \subsection{Dollar sign} $$ y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8} $$ \subsection{Bracket} \[ y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8}\] \subsection{displaymath} \begin{displaymath} y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8} \end{displaymath} \subsection{Automatic numbering formula equation environment(Such as formula\ref{eq:y1})} \begin{equation} y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8} \label{eq:y1} \end{equation} \end{document}
Note that only the last automatic numbering formula is explained. This formula can extract the serial number of the current formula to achieve the effect of cross reference
10. Matrix layout
Matrix operation requires an environment with mathematical formulas, so the off-line formulas in Section 9 can be used. First, various matrices are introduced
$ \begin{matrix} 0 & 0 \\ 1 & 1 \end{matrix} $ \ $ \begin{pmatrix} 0 & 0 \\ 1 & 1 \end{pmatrix} $ \ $ \begin{bmatrix} 0 & 0 \\ 1 & 1 \end{bmatrix} $ \ $ \begin{Bmatrix} 0 & 0 \\ 1 & 1 \end{Bmatrix} $ \ $ \begin{vmatrix} 0 & 0 \\ 1 & 1 \end{vmatrix} $ \ $ \begin{Vmatrix} 0 & 0 \\ 1 & 1 \end{Vmatrix} $
The above are the environments of various matrices, and various calculations can also be carried out in the matrix
$$ \begin{bmatrix} a^2_{11} & a^2_{12} & a^2_{13} \\ 0 & a^2_{22} & a^2_{23} \\ 0 & 0 & a^2_{33} \\ \end{bmatrix} $$
Various ellipsis can also be written
\newcommand{\adots}{\mathinner{\mkern2mu% \raisebox{0.1em}{.}\mkern2mu\raisebox{0.4em}{.}% \mkern2mu\raisebox{0.7em}{.}\mkern1mu}} $$ A = \begin{bmatrix} a^2_{11} & \dots & a^2_{1n} \\ \adots & \ddots & \vdots \\ & & a^2_{nn} \\ \end{bmatrix}_{n \times n} $$
Combining block matrix and long ellipsis
$$ \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ \hdotsfor{4} \\ & & \ddots & \vdots \\ \multicolumn{2}{c}{\raisebox{1.3ex}[0pt]{\Huge 0}} & & a^2_{nn} \\ \end{bmatrix} $$
More complex matrices can be represented using array
$$ \begin{array}{c@{\hspace{-5pt}}l} \left( \begin{array}{ccc|ccc} a & \cdots & a & b & \cdots & b \\ & \ddots & \vdots & \vdots & \adots \\ & & a & b \\ & & & c & \cdots & c \\ & & & \vdots & & \vdots \\ \multicolumn{3}{c}{\raisebox{2ex}[0pt]{\Huge 0}} & c & \cdots & c \end{array} \right) & \begin{array}{l} \left.\rule{0mm}{7mm} \right\}p\\ \\ \left. \rule{0mm}{7mm} \right\}q \end{array} \\[-5pt] % Second line \begin{array}{cc} \underbrace{\rule{17mm}{0mm}}_m & \underbrace{\rule{17mm}{0mm}}_m \end{array} \end{array} $$
Parse this expression
- The outermost layer is covered with an array, which includes matrix, pq and two m. The big framework has been determined
- The matrix is an array, which includes a, b, 0 and c
- As long as the overall structure is well grasped, it is easier to see the patchwork logic
11. Multiline mathematical formula
The environments for multiline formulas include gather|gather *, align|align *, split, and case
% Yes and only one document environment \begin{document} % Numbered formula \begin{gather} a+b=b+a \notag \\ ab = ba \end{gather} % Without number \begin{gather*} a+b=b+a \\ ab = ba \end{gather*} % align Environmental zone number \begin{align} x & = a + \cos(b) + \sin(c) \\ y & = 1 \\ z & = \frac{20}{79} \times 987 \end{align} \begin{align*} x &= t & x &= \cos y \\ y &= u & y &= \sin (u+1) \end{align*} % split Alignment and align Consistent, but the whole belongs to one formula \begin{equation} \begin{split} \cos 2x &= \cos^2 x - \sin^2 x \\ &= 2\cos^2 x -1 \end{split} \end{equation} \begin{equation} D(x) = \begin{cases} 1, & \text{If} x \in \mathbb{Q} \\ 2, & \text{If} x \in \mathbb{R} \setminus \mathbb{Q} \end{cases} \end{equation} \end{document}
Note:
- An asterisk is a mathematical formula without a number
- In the gather environment, \ notag indicates that there is no formula in the current row
- In the align environment, you can use &
- In the split environment, you can also use & for alignment, and the whole belongs to one formula
- In the cases environment, the whole belongs to a formula
- You cannot enter text in a mathematical formula, but you can enter text by \ text
12. References
The typesetting of references can be divided into several categories. If it is only used for a single time, it can be directly used in the bibliography environment, and \ cite can be used for citation; If you want a single management and multiple use, you can use another document definition for the reference
- Single use
Quote the first article \cite{article1},Quote the second book \cite{book1} wait % Reference typesetting % One management, one use, 99 is the number sample \begin{thebibliography}{99} \bibitem{article1} quote\emph{a key}Article 1 \bibitem{book1} Reference book 1 \end{thebibliography}
- Multiple use
Create a new file in bib format, text Bib, data source, Google academic, HowNet
@article{Tao Fei 2018 digital twin and its application, title={Digital twinning and its application}, author={Tao Fei and Liu Weiran and Liu Jianhua and Liu Xiaojun and Qiang Liu and Qu Ting and Tian Liang Hu and Zhang Zhinan and Xiang Feng and Xu Wenjun and others}, journal={Computer integrated manufacturing system}, volume={24}, number={1}, pages={1--18}, year={2018} } @article{rego20153dmol, title={3Dmol. js: molecular visualization with WebGL}, author={Rego, Nicholas and Koes, David}, journal={Bioinformatics}, volume={31}, number={8}, pages={1322--1324}, year={2015}, publisher={Oxford University Press} }
Click reference first and then BibTeX to get the required data, which can be copied and pasted into the bib file we created
usage method
% 1 Style references \bibliographystyle{plain} % plain unsrt alpha abbrv \begin{document} % \cite The following parameters are bib First header in a single data This is a reference from HowNet \cite{__2021} % \citep{} \citet{} This is quoted bib References under document \cite{Tao Fei 2018 digital twin and its application} Easy to explore \nocite{*} \bibliography{text, zote} \end{document}
- \nocite {*} means to list the documents that are not cited
- \bibliography{text, zote} means importing the bib database. There are two databases here
- Import the literature in HowNet, Download zotero and google's extension tool, enter the search page of HowNet, and click the extension tool to obtain all the literature information of the current page. This information can be exported into a bib file through zotero for our use, or the information in this file can be copied and placed in a bib file
- The new reference typesetting engine BibLaTeX is troublesome to use. If you are interested, you can search by yourself
13. Define new commands and environments
You can define new commands to use
% Usually only 10-12 pound \documentclass{article}[12pt] %book, report, letter \usepackage{ctex} \newcommand{\newMessage}{This is a new command string} \newcommand{\loves}[2]{#1 like #2} \newcommand{\lovess}[3]{#1 like #2, #2 like #3,#3 like #1} % The first parameter is optional, and the default is like \newcommand{\defaultLove}[3][like]{#2 #1 #3} % \renewcommand{cmd}{def} And newcommand usage method % \newenvironment{envname}{begdef}{enddef} And newcommand The usage is consistent. See the document for details % Yes and only one document environment \begin{document} \newMessage \loves{kitten}{Small fish} \lovess{kitten}{Dog}{Sheep} \defaultLove{floret}{Xiao Ming} \defaultLove[Detestable]{floret}{Xiao Ming} \end{document}
- Note that default parameters can be used
- A custom command can output a specified string
- Redefine the environment and see relevant documents