A quick guide to help you write professional LaTeX docs.
This is a quick guide to help you write itemize
, enumerate
environments, etc.
I'm not writing this guide to tell you how to write
You may notice that lots of commands I tend to use are rather long (usually means it's in plain
Also, this is the best way to see what's the difference compared to other
I often see things like
This is one paragraph.\\
This is another.
The command \\
will produce a line break, i.e., will end the current line and start a new one. This is different from a paragraph, as the start of paragraphs is usually indented. So if we write
This is one paragraph.\\ This is still in the paragraph!
This is another.
the output will be
This is one paragraph.
This is still in one paragraph!
This is another.
Notice the indention at the start of the two paragraphs, while there is no indention on that new line since we only introduce a line break, which will not start a new paragraph. So basically, \n
(i.e., newline symbol) as a sign for starting a new paragraph. So, with the following input:
This is
one paragraph
This is another.
the output will be like this:
This is one paragraph.
This is another.
Hence, while \\
will force a new line and if you already have a blank line between paragraphs, \\
is redundant. And I don't recommend you manually insert a line break in your paragraph to do auto-adjusting.
Another confusing command is \par
. I have seen something like
\par This is one paragraph.
\par This is another.
Although this has the same output as above, this is indeed wrong. The \par
command is used to end a paragraph, not to start one. So \par
is the same thing as leaving a blank line as we mentioned. If you really want to use \par
, this is something you should write
This is one paragraph.\par This is another.
and the output will be
This is one paragraph.
This is another.
The reason why you can theoretically use
\par
to start a paragraph is that, this command will not be counted multiple times: If you are already in a new paragraph,\par
will do nothing. For example,This is one paragraph.\par\par\par\par This is another.
produces
This is one paragraph. This is another.And if you put
\par
in front,\par This is one paragraph.\par This is another.Since at the beginning of the document, we're already in a new paragraph, so the first
\par
will do nothing, result in the equivalent input:This is one paragraph.\par This is another.
Hence this will produce the desired output. But again, I suggest you to use blank line to start new paragraph.
As you know, $...$
and $$...$$
are two commonly used commands to write a mathematical expression. For example, $e^{i\pi} + 1 = 0$
produces $$e^{i\pi} + 1 = 0$$
produces
Both $...$
and $$...$$
are fine but this is why you should avoid them: they are \(...\)
and \[...\]
, which is supported by modern
While
equation*
environment is also an good option for unnumbered equations, but I tend to keep things simple and unified. For numbered equations,equation
andalign
environment should be used.
When you need to have some texts in your math equation, please use \text{...}
to wrap the texts around. For example,
\[
x_i \geq 0 \text{ for all } i,
\]
which produces
is a good example. Notice that you should always indent both sides of your text inside \text{}
, otherwise you'll have something like
which is not desirable.
This is because
$\LaTeX$ will neglect any spacing in math mode. In other words, if you write something like$x y$
, you'll just get$x y$ instead of$x\; y$ . More about spacing in Math mode later.
Also, you should always think twice when choosing between \text{}
or \mathrm{}
. They render the same output, but it's always a good habit to keep your source code clean and semantically correct. A quick guide is that when writing text, use \text{}
, when writing math shorthand, use \mathrm{}
instead. For example, if a variable up
, you should write u_{\text{up}}
instead of u_{\mathrm{up}}
since up
is a text. But if you're doing an integral, say
you should write \int x \,\mathrm{d}x
instead of \int x \,\text{d}x
.
Most of the time, when you're writing a math symbol with your direct keyboard input, there's a corresponding command in
-
$\colon$ (\colon
): Instead of writingf: X \to Y
for$f:X\to Y$ , writef\colon X \to Y
instead. -
$\ldots$ (\ldots
): This is a straightforward one. Instead of writinga_1, a_2, ...
for$a_1, a_2, \ldots$ , writea_2, a_2, \ldots
instead.There's something called
\cdots
also, which is my personal preferred one. If you really want to know about...
, there are actually five types of them:Code Output Comment A_1, A_2, \dotsc
$A_1, A_2, \dotsc$ for dots with commas A_1 + \dotsb + A_N
$A_1 + \dotsb + A_N$ for dots with binary operators/relations A_1 \dotsm A_N
$A_1 \dotsm A_N$ for multipication dots \int_{a}^{b} \dotsi
$\int_{a}^{b} \dotsi$ for dots with integrals A_1\dotso A_N
$A_1\dotso A_N$ for other dots The above is the conventions suggested by American Mathematical Society. If you don't want to follow them strictly, just choose one of them and stick with it.
-
$\coloneqq$ ,$\eqqcolon$ (\coloneqq
,\eqqcolon
): Another direct one. When you define a new symbol such as let$y\coloneqq x_1-x_2$ , writey \coloneqq x_1 - x_2
instead ofy := x_1 - x_2
.You need to put
\usepackage{mathtools}
in your header, namely you need themathtools
package. -
$\gg$ ,$\ll$ (\gg
,\ll
): Use\gg
for much greater than and\ll
for much less than instead of directly using>>
and<<
. The former ones produce$\gg$ ,$\ll$ , while the latter ones produce$>>$ and$<<$ .
Also, some symbols are by default equivalent to your direct keyboard output.
-
$\prime$ (\prime
):x'
is equivalent tox^\prime
.There exists some pathological examples like
${x^\prime}^\prime$ v.s.${x'}^{'}$ . But for usual cases,$x'$ is equivalent to$x^\prime$ , and indeed if you writex''
,$\LaTeX$ renders this asx^{\prime\prime}
as$x''$ and so on. I personally prefer to use^\prime
since in this way I get a full control of my output. -
$\ast$ (\ast
):*
is equivalent to\ast
.There are also something called
\star
, which produces$\star$ . In some cases,$x^\star$ may be desired. For me, I prefer to use\ast
whenever I want to render$x^\ast$ . This is because I generally regard*
as an operator, which can mean convolution for example. And to mark a variable, I think^\ast
is a better touch.
There are also commands which produce very similar output (or even exactly the same), but with different semantics. To keep the source code clean, we mention some of them.
-
\rightarrow
v.s.\to
($\rightarrow$ ): I use\to
for mapping, e.g.$f\colon X\to Y$ , while\rightarrow
for all other cases. -
\leftarrow
v.s.\gets
($\leftarrow$ ): I use\gets
when writing pseudocode, while\leftarrow
for all other cases. -
\Rightarrow
v.s.\implies
($\Rightarrow$ v.s.$\implies$ ): I use\implies
when writing proofs, while\Rightarrow
for all other cases. -
\Leftarrow
v.s.\impliedby
($\Leftarrow$ v.s.$\impliedby$ ): I use\impliedby
when writing proofs, while\Leftarrow
for all other cases. -
\Leftrightarrow
v.s.\iff
($\Leftrightarrow$ v.s.$\iff$ ): Similarly, I use\iff
when writing proofs, while\Leftrightarrow
for all other cases.Since
\implies
,\impliedby
, and also\iff
are quite long, so I redefined them into their corresponding ones for a more compact look. But in the code, I still type\implies
when writing proof. To redefine them, put\let\implies\Rightarrow \let\impliedby\Leftarrow \let\iff\Leftrightarrow
into your preamble. This makes
\implies
,\impliedby
and\iff
shorter and in my opinion has a better look.
I sometimes see things like \cup
is a binary operator, so it should only be used when you want to write something like \bigcup
instead. Below are some examples.
Operation | Standard form code | Output | Big form code | Output |
---|---|---|---|---|
Intersection | \cap |
\bigcap |
||
Disjoint union | \sqcup |
\bigsqcup |
||
Tensor product | \otimes |
\bigotimes |
||
Disjunction | \vee |
\bigvee |
||
Conjunction | \wedge |
\bigwedge |
There are lots of functions that have their own commands, e.g., lg(x)
, which produces \lg(x)
for \
log(x),
\sin(x),
\cos(x)` and so on.
There are two symbols I want to mention, the empty set symbol and l (ell).
- You can either write
\emptyset
or\varnothing
to denote an empty set, which produces$\emptyset$ and$\varnothing$ . I prefer the latter one, but choose whatever you want. - On the other hand, it seems like not everyone knows the command
\ell
for producing a nice looking$\ell$ , and instead, they simply typel
, which produces$l$ . Not sure whether they're intended, but this is something worth mentioning.
The most painful thing when I read a
with the source code being
\[
N \coloneqq \vert \sum\limits_{j=1}^\infty ( \sum\limits_{i=1}^\infty X_{ij} ) \vert.
\]
The size of the absolute value and the parenthesis are still in the default size, while the formula being wrapped is much higher than the default size. Before we talk about how to resolve this sizing issue, we should first see the common commands which will cause this kind of problem.
-
$\lvert \ldots \rvert$ (\lvert ... \rvert
): This is a fun one since we often use this to denote the absolute value like$\lvert x \rvert$ . In this case, write\lvert x \rvert
instead of|x|
.Indeed, there are something called
\vert
, which is synonym to|
, and amsmath recommends to use\lvert ...\rvert
for absolute value. -
$\lVert \ldots \rVert$ (\lVert ... \rVert
or\| ... \|
): For norm, write\lVert x\rVert
instead of||x||
for$\lVert x\rVert$ .Notice that you can also write
\Vert x \Vert
or\| x |\
for$\Vert x\Vert$ , though I still prefer\lVert x \rVert
from the very same reason with\lvert ... \rvert
. -
$\lbrack \ldots \rbrack$ (\lbrack ... \rbrack
or[ ... ]
): They are indeed equivalent, so I prefer[ ... ]
for simplicity. -
$\langle\ldots\rangle$ (\langle ... \rangle
): Please don't use<x, y>
for things like inner product, use\langle x, y \rangle
instead to produce$\langle x, y\rangle$ . -
$\{\ldots\}$ (\{ ... \}
): The usual set notation.
Let's see how we can fix this.
To automatically resize the brackets, and parentheses, we use \left...\right...
to do this. For the above example, the resized formula should be
\[
N \coloneqq \left\vert \sum\limits_{j=1}^\infty \left( \sum\limits_{i=1}^\infty X_{ij} \right) \right\vert
\]
which produces
Interestingly enough, though this is already powerful, there are more commands that can be utilized. They are \left.
/\right.
and \middle
. A typical usage for \left.
or \right.
is when you want to automatically resize an operator which only appears on one side. For example:
\left. \frac{x^2}{2} \right|_0^1
And for \middle
, you might have encountered the following situation:
We see that \middle
before |
and get
as we desired.
Sometimes the automatic sizing may tend to be too big since it's trying to wrap everything inside, for example, $\vert \hat{x}^{(n)}i\vert$ is way better than $\left\vert \hat{x}^{(n)}{i}\right\vert$ while the latter uses \left\vert ... \right\vert
and the former simply uses \vert ... \vert
. In this case, uses \big
, \Big
, \bigg
, and \Bigg
instead of \left
and \right
as modifiers. For example,
\[
( \big( \Big( \bigg( \Bigg(
\]
produces
A particularly important use case is that, when you use \underbrace
, the automatic resizing will be much larger than expected. For example,
where I use \left[ ... \right]
on the left and \bigg[ ... \bigg]
on the right. Notice that we even haven't written anything under the braces, and the left one is already ugly.
I have seen someone tried
$$\mathbb{E}\underbrace{\left\lbrack \prod_{i=0}^\infty X_i\right\rbrack}$$ to avoid the auto-resizing issue. Please don't do that, just don't.
Another use case is that \left( k g(x) \right)
produces \left
and \right
produce the same size delimiters as those nested within it. In this case, we can use \big( k g(x) \big)
, which produces
As we have seen before, when I type an indefinite integral, we have something like
with the source code being \int x\,\mathrm{d}x
. Notice that there's a \,
before \,
, we will end up with
where
Command | Description | Size |
---|---|---|
\, |
small space |
\quad
|
\: |
medium space |
\quad
|
\; |
large space |
\quad
|
\! |
negative space |
\quad
|
While
\quad
and\qquad
are commonly used for spacing in every scenario, the above only works in math environments.
When using inline math environment, you'll often see \sum_{i=1}^\infty x_i
. But if you type the same thing in display math mode, you'll get
instead. You can indeed put the subscript and superscript below/on the summation symbol in inline math mode like \limits
followed by \sum
, i.e., \sum\limits_{i=1}^\infty x_i
. This \limits
command can be used in various scenarios, for examples,
This indeed works for all big commands like
$\int$ ,$\iint$ ,$\iiint$ ,$\iiiint$ ,$\oint$ ,$\idotsint$ ,$\bigodot$ ,$\bigoplus$ ,$\bigotimes$ ,$\bigvee$ ,$\bigwedge$ ,$\bigsqcup$ ,$\biguplus$ . For example,$\bigoplus\nolimits_{i=1}^\infty G_i$ v.s.$\bigoplus\limits_{i=1}^\infty G_i.$ But notice that
\limits
works differently with integral signs: It'll put the upper-bound directly on top of the integral sign, and the same is done for the lower-bound. For example:
$$\int_{a}^{b} x\,\mathrm{d}x\text{ v.s. }\int\limits_{a}^{b} x\,\mathrm{d}x.$$ Notice that if you want to apply this to all your integral, please use
\usepackage[intlimits]{amsmath}
when loadingamsmath
package. This will only be applied when using integrals, since as mentioned,\limits
with integral is treated differently.
One important thing that relates to sizing but is neglected by lots of people is the way of handling continued fractions. If we use \frac{}{}
throughout, we'll have something like
with the code being
\[
x = a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \frac{1}{a_3 + \frac{1}{a_4} } } }
\]
which is ugly. Instead, we use \cfrac{}{}
, where that extra c
stands for continued
. In this case, we have
with the code being
\[
x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }.
\]
We see that with \cfrac{}{}
, the equation is spaced more equally in the vertical direction, hence it's clearer.
Sometimes you may want to define your operators. For example, while there is a default \ker
for producing the kernel of a function like \im
for the image of a function. To do this, we should use
\DeclareMathOperator{\im}{Im}
instead of
\newcommand{\im}{\mathrm{Im}}
since we'll have some spacing issues if we go with the latter one.
Unlike spacing in math mode, when writing text, there are just a few things to note. The .
, and see whether the character before it is a lowercase alphabet. So for example, when you write
This is common, e.g. A, B, and C.
Then the compiler will think you're going to end the sentence right after e.g.
, so it will create an extra spacing for you. Other common situations are w.r.t.
(with respect to), w.p.
(with probability), and w.h.p.
(with high probability). The way to fix this is to add \
after .
, i.e., you may write
This is common, e.g.\ A, B, and C.
In this way, you force the compiler to create a normal spacing after the period, fixing the problem.
It is typical that when you write a quote in
"this is a quote"
in
``this is a quote''
which renders as