# 关于 TeX¶

## Installation of TeXLive¶

TL;DR

Following the official instruction,

1. download install-tl
2. run it, and optionally specify the mirror location for faster download speed

### 2021 on Rocky¶

did not click create symlinks to standard directories after running ./install-tl

create module file, /usr/share/Modules/modulefiles/texlive/2021,

set     version         2021
set     app             texlive
set     modroot         /usr/local/$app/$version

prepend-path PATH $modroot/bin/x86_64-linux prepend-path MANPATH$modroot/texmf-dist/doc/man
prepend-path INFOPATH $modroot/texmf-dist/doc/info  then call it after loading the module, $ which pdflatex
/usr/bin/pdflatex
$module load texlive/2021$ which pdflatex
/usr/local/texlive/2021/bin/x86_64-linux/pdflatex


### 2017 -> 2020¶

The TeXLive 2017 for Ubuntu cannot work for me, it reports

fatal: Could not undump 6994 4-byte item(s) …

and try

fmtutil-sys –all

refer to Error Message: “tex: fatal: Could not undump 1 4-byte item(s) from”, but does not work.

And I also try uninstall and reinstall TexLive, but it still does not work.

Then finally I decided to install the latest TeXLive 2020, TeX Live - Quick install, follow the instructions, but note that the mirror url should append path/systems/texlive/tlnet.

install-tl --location http://mirror.example.org/ctan/path/systems/texlive/tlnet


And note that the steps for completely removing the installed TeXLive.

If without root privilege, when running install-tl, type D to change the directory, and actually changing the first <1> would change all other directories.

### portable mode¶

TexLive 官方说明文档也介绍了 -portable 的安装选项，

./install-tl -repository https://mirrors.huaweicloud.com/repository/toolkit/CTAN/systems/texlive/tlnet/


<CTAN/mirror/root/URL>/systems/texlive/tlnet


## Basic¶

### line breaks¶

• \\, \newline, \tabularnewline : when writing a table, the first one can be confused, while the second one ends the line in a cell, and the latter ends the rows of the table. A good side is that when writing tex using julia, raw"\command" does not need to escape \, but it is still required to use raw"\\\\" to represent \\.
• break line in cells of table: \makecell{A\\ B} after loading \usepackage{makecell}.

### % at the end of lines¶

since % starts a comment that goes to the end of the line, then the normal effect is that it does not insert the space (or a par) from the newline. It would be necessary when creating multiple figures horizontally using subfigure environment. An real example. More details can be found in What is the use of percent signs (%) at the end of lines?

### fontsize¶

In the following command

\documentclass[12pt]{article}


the option can be recongized only 10pt, 11pt and 12pt.

### punctuation in formula¶

Displayed equation usually are considered to be part of the preceding sentence, that is it will get the very same punctuation as if it was inline math

## Beamer¶

### fragile option¶

Runaway argument?
! Paragraph ended before \lst@next was complete.
\par
l.68 \end{frame}
?


\begin{frame}[fragile]

\begin{lstlisting}
code
\end{lstlisting}
\end{frame}


### incompatible with enumitem¶

enumitem “disturbs” beamer.

### import video¶

Can XeLaTeX | LuaTeX import movies?

! Undefined control sequence. \pdfmark

The package can be used together with both dvips plus ps2pdf and pdflatex, though the special sound support is available only in pdflatex.

## Fonts¶

### some nice font family¶

and currently I prefer to newpxtext and newpxmath.

### missing font¶

sudo apt-get install texlive-fonts-extra


sudo apt-get install texlive-fonts-recommand


### bold math formula¶

\usepackage{amsmath}
\boldmath
$$f(x,y) = 3(x+y)y / (2xy-7)$$
\unboldmath


\usepackage{bm}
$$\bm{f(x,y) = 3(x+y)y / (2xy-7)}$$


### use serif math font in beamer¶

\usefonttheme[onlymath]{serif}


## List¶

### 设置item之间的间隔¶

\begin{itemize}
\setlength\itemsep{1em}
\item one
\item two
\item three
\end{itemize}


## Bibliography¶

• biber and bibtex are external programs to process .bib file
• biblatex and natbib are LaTeX packages that format citations and bibliographies
• natbib works only with bibtex
• biblatex works with both biber and bibtex

Based on my understanding and experience, biber + biblatex is more advanced, and it can easily handle accent characters.

• (2021-10-07 20:55:35) Just encountered that accented characters are correctly displayed in .bib file as UTF8 encoding, but after processing with bibtex, the accented characters are messy. (Refer to for more details.)

### biblatex -> natbib¶

Info

To switch from “biblatex + biber” to “natbib + bibtex”,

\usepackage[backend=biber,citestyle=authoryear,sortcites=true,natbib,sorting=nyt,style=apa]{biblatex}
\DeclareLanguageMapping{american}{american-apa}

\usepackage[authoryear]{natbib}
\bibliographystyle{imsart-nameyear}


We can just modify the header definition without replacing the commands in the main document.

The idea is to define the command in natbib+bibtex as biblatex+biber, and keep the main content unchanged. Specifically,

\newcommand{\parencite}[1]{\citep{#1}}
\newcommand{\textcite}[1]{\citet{#1}}
\newcommand{\printbibliography}{\bibliography{ref_bibtex.bib}}


### bibtex文献加颜色¶

\hypersetup{
citecolor=green,
}


\DeclareCiteCommand{\cite}
{\color{red}\usebibmacro{prenote}}%
{\usebibmacro{citeindex}%
\usebibmacro{cite}}
{\multicitedelim}
{\usebibmacro{postnote}}

\DeclareCiteCommand{\parencite}[\mkcolorbibparens]
{\usebibmacro{prenote}}%
{\usebibmacro{citeindex}%
\usebibmacro{cite}}
{\multicitedelim}
{\usebibmacro{postnote}}


### APA 带编号¶

APA bibliography style with numbers

### export to .bib from Zotero¶

Tip

The plugin Better BibTeX can keep the .bib file once new references are added. Also, it can be exported to either biblatex or bibtex.

• With biblatex, do not care about the accented characters, just use its original format
• With bibtex, it will automatically convert the accented characters with necessary backslashes.

The original abstract field is,

Karhunen–Lo{e}ve decomposition

after exporting to .bib file, it becomes

Karhunen–Lo{\textbackslash}{e}ve decomposition

and it throws an error when compiling,

! File ended while scanning use of \field.

My first solution is to check if there are some setting when exporting to .bib file to drop some field, such as abstract here, but finally I failed to find one, and here is a discussion in the Zotero community.

Then I try to replace

{e}

with

{e}

in the Zotero’s abstract field as said in How to write “ä” and other umlauts and accented letters in bibliography?, which also list several accented characters,

{"a} {\^e} {i} {.I} {\o} {'u} {\aa} {\c c} {\u g} {\l} {~n} {\H o} {\v r} {\ss} {\r u}

but it still doesnt work.

Finally, I directly correct the exported file using the above technique, and it works. But I still think it is not a good idea since I prefer to export without any further modifications. [TODO]

### display full name¶

For natbib, check the documentation of natbib by typing

texdoc natbib


longnamesfirst option will display full name for the first citation of any given reference, but all subsequent ones will be used with et al.

• without longnamesfirst, all citations would be et al.
• with \citet*, \citep*, the citation would be full author list.

For biblatex, seems that we can set the global argument maxcitenames to control the number of author in the citation, such as only display all authors, of multi-author works, the first time a citation is used with biblatex

### DeclareLanguageMapping¶

\DeclareLanguageMapping{american}{american-apa}


### printbibliography¶

\usepackage[american]{babel}
\usepackage[style=apa,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa} %Literaturverzeichnis american-apa style
\usepackage[babel,threshold=2]{csquotes}


and then

\printbibliography[heading=bibintoc,title=References]


refer to Problems with \printbibliography

## TiKZ¶

• label in place:
  \tikzset{%
in place/.style={
auto=false,
fill=white,
inner sep=2pt,
},
}


## Formula¶

### Some ways to number equations¶

• split for a single number
• multiline for a single number
• align* with tag

more details refer to A test of Equation Numbering

### align multi cases¶

Key point: enclose one of the entries in the first cases environment in a box whose width is that of the longest of the second cases environment.

or use \hphantom{}.

### 带 \hat 时的上标¶

\hat\beta^n

\hat\beta^n

\hat\beta{}^n

\hat\beta{}^n

### 二阶导数符号¶

$f''$


1. \dprime\trprime需要unicode-math
2. f^{\prime\prime}代替f''可以解决问题。

## Theorem¶

### bold for theorem title.¶

Without amsthm, the title is bold by default. Refer to Title of the theorem.

With amsthm, one solution is to add these in the preamble.

\usepackage{amsthm}
\makeatletter
\def\th@plain{%
\itshape % body font
}
\def\th@definition{%
\normalfont % body font
}
\makeatother


### proof by parts¶

\newtheorem{proofpart}{Part}
\newtheorem{theorem}{Theorem}
\makeatletter
\makeatother


here \@addtoreset{foo}{bar} is to add counter foo to the list of counters \cl@bar to be reset when counter bar is stepped.

Refer to Parts numbering in Proofs

## 缺少 .sty 文件¶

! LaTeX Error: File 'mhchem.sty' not found


# 1. 检查是否存在 mhchem.sty 文件
$locate mhchem.sty # 2. 查找需要安装的 package$ apt-cache search mhchem | grep tex
# texlive-science - TeX Live: Natural and computer sciences
# 3. 安装相应的package

\resizebox:
undefined

0 & otherwise,}


refer to Matching Content to MathJax, which originally for illustrating the typeface of mathjax.

## Make a love heart¶

• by equation:
• by pure latex
• by tikz based on filtered paths

## Change section title without altering the cleveref cross-referencing system¶

Info

Contributed by ZexiCAI on 2021-09-06.

Suppose you

1. want the section title to be “Good Title 1”, “Good Title 2”, etc., and
2. you want cross-reference two sections at the same time as “Good Titles 1 and 2” instead of “Good Title 1 and Good Title 2”. Although one can do it mannually, the author is lazy and would like the cross-referencing system to do it automatically.

The simplest way to do (1) is to change the \thesection definition:

\renewcommand{\thesection}{Good Title \arabic{section}}


However, when you do cross-referencing (“cref” or “Cref”), it will appear to be “section Good Title 1” and “section Good Title 2”. A feasible way to do it is to directly modify the definition of \section command. Its original definition is

\newcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}


We can modify it as

\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries\noindent Good Title~}}


This solves (1). To solve (2), we need to change the name of section when calling “cref” or “Cref”, by doing

\crefname{section}{Good Title}{Good Titles}
\Crefname{section}{Good Title}{Good Titles}
`

Warning

Note that however this will change the name globally.