pezayo's Blog

蓦然回首,那人却在灯火阑珊处

0%

Some tips you should know in Latex

Latex#

visual studio code#

The visual studio code cannot generate the table of contents regularly. And I have searched the solution of the problem.

OK, it’s not the perfect solution, for I have now got the hang of the principle in the compiling procedure.

  1. the .aux and the .toc are the particular files that .aux is associated with bibliography while .toc is associated with table of contents. And we need to compile four times to successfully generate the PDF that we want
  2. xelatex, compiling ends with the .aux and .toc
  3. bibtex, compiling using .aux and .toc to deploy the bibliography and table of contents
  4. xelatex * 2, successfully generate the PDF(I don’t know why 2)

Moreover, for the template of njuthesis, this template uses the biblatex to deploy the bibliography. So we must use the prompt biber but not bibtex to generate bibliography.

And the biber should make the \bibliography located in preamble and add \printbibliography in the end of a chapter.

And for visual studio code, I always employ the set of xelatex-bibtex-xelatex-xelatex , so I cannot compile successfully using the njuthesis template.

On top of that, the setting of the cleaning file also matters a lot. In the settings.json in visual studio code, the latexworkshop configuration, among all the json, the latex-workshop.latex.autoClean.run, is given two options. OnBuilt and OnFailed. When the compilation has built or successfully ended, the files generated in the procedure will be cleaned. And the imperative .aux and .toc will be cleaned as well, which may cause the major problems.

At the same time, the latexmk -xelatex can also compile successfully the njuthesis when I have downloaded and installed the strawberry perl. And I also downloaded the .vscode configuration for vscode from the gitlab of NJU and I have imported it to the vs code merging the previous version. And I also added the recipe of xelatex-biber-xelatex-xelatex which also works regularly with the template.

TexStudio#

TexStudio is also a popular major Tex Editor, which has the equal position with vscode in the industry. And you can choose which to use depending on your emotion habit and preference.

In TexStudio, you are allowed to modify the configuration in the option as you please. The first thing you should do shall be modifying the default annoying engine. The default compiling engine the pdflatex, which is not so friendly to most of us normal students in China. In the most time xelatex is needed to employ rather than the pdflatex which may result in errors.

SumatraPDF#

SumatraPDF is an excellent PDF reader with lightweight features, which is a better option than Adobe Acrobat if you just read the PDF not modify or do other things.

What makes me like SumatraPDF is actually that both TexStudio and vscode are able to open an executable file which is a PDF reader when you click the view the PDF. At the time, SumatraPDF stands out with its lightweight. Because of that, we can open the PDF quickly and efficiently.

About the issue that how to set the SumatraPDF be the default external PDF reader in the vscode or TexStudio, there are too many tutorials in the web. Just search and deploy.

Syntax#

Space in filename#

As for the bibliography, if you employ the bibtex, it is forbidden to make the filename attached space, while you employ the biblatex it is not so restricted, you can use \addbibliography which can only add one bib file once used.

change ref name#

  1. To change the ref name in the end of file, it is an available way to redefine the \refname . The code is \renewcommand\refname{the new name} in the preamble.
  2. Using the cleveref package. \crefname{figure}{图}{图}, The last two parameters are the names used for the figure notes and references respectively.

new float#

The float is always a whole box that occupy some space. And it will be a problem if the box is too long or large to completely display in a page. So it should employ the new float characteristic. In the minted package, you can use the newfloat argument to do this. And what is the most suitable solution is using the lstlistings and new an environment to do that.

1
2
3
4
5
6
7
8
9
10
\usepackage{lstlistings}
\usepackage[newfloat]{minted}

\newenvironment{longlisting}{\captionsetujp{tyhpe=listing}}

\begin{document}
\begin{longlisting}
very very long in the vertical aspect code block...
\end{longlisting}
\end{document}

breaklines#

The same as new float, if the inline code or code block is too long in the horizontal aspect, you can use the breaklines to avoid this. for the minted, you can use breaklines argument in the prenium. Also useful with mintiline.

Arguments#

There are many optional arguments for the compiling engine to compile the destination file. And for the minted package, the --shell-escape is necessary. And why the argument comes about the answer in stackexchange. It means that the minted package is a file external which will be regarded as not secure for the major file to be compiled causing the interrupt of the compilation.

Package#

minted#

Package minted can be employed to make the code highlight, which can render the code block not so boring and become very cool. And here are the example of the implementation of the package.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\usepackage{minted}
\usepackage{xcolor}
\definecolor{LightGray}{gray}{1}
\begin{document}
\begin{minted}
[
frame=lines,
framesep=5mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
]{matlab}
x = 100;
y = 100;
z = 'hello world!';
if x == y
disp(z);
else
disp(x);
end
\end{minted}
\end{document}

This is a code block whose the language is matlab. And you can compile the tex file and get the result that is very beautiful a pdf file.

Attention, minted package must be invoked by –shell-escaped prompt, which means if you compile it in the powershell or cmd you have to use the prompt xelatex --shell-escaped main.tex . And you should config the compile option when using the minted. However, in my vscode, I have config it in the settings.json. I am so clever a man/boy.

fontspec#

This package is almost used to change the font of text.

ctex#

The author not only provides a documentclass but also compress it into a package. ctexart and the ctex is the same in a term.

In its manual, there are many wonderful function we can use, such as changing the section settings like internal, font and size. And the useful one is space=true

And the most useful is the \ctexset={} , in the curly bracket, we can custom our wanted setting

biblatex#

The hyperref package transforms citations into hyperlinks. See the hyperref and backref package options in § 3.1.2.1 for further details. When using the hyperref package, it is preferable to load it after biblatex.

latexmk#

After the struggle with the minted package, I feel so tired and think of that I am so weak to get the hang of latex. It’s so difficult, so difficult……And now I have come to some conclusions. As for latexmk, it is an tool that very strong and powerful which can make the compilation realized by one time while other tools cannot do that when the file attached with bib, toc and so on. On top of that, there are also a file named latexmkrc or .latexmkrc. Inside the file are is the detailed configuration info about the compiling engines. And if it is in the overleaf, it is very necessary to config a latexmkrc file. Just like the minted package that needs the –shell-escape, in the local IDE we can get the arguments to be treated properly while in the overleaf we can’t, so we need to get the work done with the help of latexmkrc and .latexmkrc.

What is the most useful command prompt is as follows

1
latexmk -xelatex -shell-escape && latexmk -c

This command can detect the compiling engine that is needed and the repeat time as well as the compiling sequence automatically. Very useful! Strongly recommend!

tabularx#

It can be used to specify the width of the tabular environment

1
2
3
4
5
6
\begin{table}
\centering
\begin{tabularx}{\textwidth}{|c|c|c|}
...
\end{tabularx}
\end{table}

tabularray#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{tblr}
{
colspec={Q[c,m]Q[c,m]Q[c,m]},
hlines,vlines,
row{1}={bg=gray8},
}
$p$ & $q$ & $p\land q$ \\
T & T & T \\
T & F & F \\
F & T & F \\
F & F & F
\end{tblr}
\end{document}

The result is as follows:

tabular.jpg

Three-line meter#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\usepackage{booktabs} % preamble

\begin{table}[H]
\belowrulesep=0pt
\aboverulesep=0pt
\centering%把表居中
\begin{tabular}{c|ccc}%四个c代表该表一共四列,内容全部居中
\toprule%第一道横线
POS & Before & After & Wordle\\

\midrule%第二道横线
Noun & 59.44\% & 54.59\% & 60.17\% \\
Adjective & 21.12\% & 23.82\% & 22.01\%\\
Verb & 15.55\% & 17.21\% & 9.19\% \\
Others & 1.98\% & 2.23\% & 5.29\% \\
Adverb & 1.91\% & 2.15\% & 3.34\% \\
\bottomrule%第三道横线
\end{tabular}
\caption{\textbf{Percentage before and after deletion of database and in Wordle}}%标题
\label{tab:before vs. after}
\end{table}
image-20230303015033832

Tree directory diagram#

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
\begin{figure}[h!]
\centering
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {CNNdetection}
child { node {demo\_test.py}}
child { node {demo\_dir.py}}
child { node {widget.py}}
child { node {train.py}}
child { node {validate.py}}
child { node [selected] {dataset}
child { node {train}
child{ node {0\_real}}
child {node {1\_fake}}
}
child [missing] {}
child [missing] {}
child { node [optional] {val}
child{ node {0\_real}}
child {node {1\_fake}}
}
child [missing] {}
child [missing] {}
child { node {test}
child{ node {0\_real}}
child {node {1\_fake}}
}
}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child { node {examples/}}
child { node {checkpoints/}}
child { node {weights/}};
\end{tikzpicture}
\caption{文件树状图}
\label[figure]{fig:文件树状图}
\end{figure}
image-20230303020910671

macros#

minted#

I want to use the macro to replace the complex environment with many optional parameters. But it is not easy to realize in the minted because of it’s nature.(very special and make the \newenvironment cannot work properly)

It should be that using the \newenvironment to make a new environment defined by myself.

In terms of minted, in its documentation, there is the interpretation about the alias for specific language settings. We should use the \setminted or \newminted to make it done. On top of that, to make \newenvironment into use, it should include the fancyvrb package. And in the {before}, the \VerbatimEnvironment must be placed in front of the \begin{thecode}. Here is the tex code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\usepackage{fancyvrb}

\setminted[shell]{
frame=lines,
framesep=5mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
}

\newminted[linux]{shell}{
frame=lines,
framesep=5mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
}

\newenvironment{myminted}{\VerbatimEnvironment\begin{linux}}{\end{linux}}

color#

1
2
3
4
5
6
\textcolor{name}{...}
\textcolor[color model]{color specification}{...}
or

\color{name}
\color[color model]{specification}

\textcolor{name}{...} can color the content in the curly bracket while \color{name} can color the environment within. {\color{red} ....} or \begin{table}\color{blue} \end{table}

And \definecolor{name}{model}{color specification} is often used to define a new color. the general model are rgb RGB cmyk gray.

box#

\mbox and \makebox can make a box

\fbox and \framebox can make a box with four lines(frame)

\colorbox{name}{...} can make a box with a background in name while \fcolorbox plus a frame on the base.

Tikz#

-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道