Post

LaTeX Tips

Newbie's roadmap: getting started with resources to the ultimate setup!

History

While working on his magnum opus, ‘The Art of Computer Programming’, Donald Knuth was disappointed with the quality of typesetting, which led him to develop \(\TeX\), a typesetting system intended for creating of beautiful books that contain a lot of mathematics1. With Leslie Lamport’s modifications, \(\LaTeX\) was born.

Features

Unlike a WYSIWYG software, here, an individual writes code, and the document is generated automatically. This allows users to focus on writing actual content while the software takes care of text formatting and document layout.

Here are some of its advantages

  • Support for complex math expressions and environments
  • Automatic numbering and generation of table of contents, citations and other lists
  • Active open-source community has developed packages2 for pretty much everything

You really don’t need to know anything else about it; you will pick up things as you dive deeper. Now, let’s begin.

Getting Started

You can use Overleaf to start your \(\LaTeX\) journey. The Learn LaTeX in 30 minutes blog will explain the basics. And that’s it! You can now use Overleaf to work on any document.

Resources

Also, do check out this tutorial by Aryaman Maithani, especially the “things to keep in mind” section, to learn it the correct way. He’s the guy who inspired me to delve into \(\LaTeX\).

These resources are handy, but if you ever get stuck, just Google it and you’ll get what you want.
Trust me, the \(\TeX\) - \(\LaTeX\) Stack Exchange is a lifesaver for literally any \(\TeX\)nical doubt.

Online Tools

  • Detexify - Recognises hand-drawn symbols, and provides the corresponding \(\LaTeX\) command and packages to import, if any.

Just to make it clear, if coding something gets too complex, you can always turn to online GUI tools such as

And many more!

Offline Setup

Once you get a good grasp of \(\LaTeX\), go for an offline setup.

I use Sublime Text with LaTeXTools, LaTeXYZ and LaTeX-cwl packages to speed up writing. sublime-text-latex-math-render Gaussian function of a normally distributed random variable with expected value \(=\mu\) and standard deviation \(=\sigma\)

Benefits

  • Keyboard Shortcuts
  • Auto-completions and Auto-matching
  • Equation and Image Preview
  • Forward Search and Inverse Search
  • Spell Checking
  • Custom Builders and many more

To do this, you need to follow the steps mentioned in this blog: Set up Sublime Text as Your Ultimate LaTeX Editor. I have summarised the essential points below.

Installation

Configuration Steps

Interested in taking live notes with LaTeX? Check out Evan’s blog, but brace yourself – the setup gets crazier and involves a fair bit of a learning curve.

And that’s it! Thanks for reading this blog; I hope this will help you in your future \(\TeX\)nical adventures. I will wrap this up by showcasing some cool \(\LaTeX\) applications with source code.

Math

Givens Rotation

\[{ G(i,j,\theta )={\begin{bmatrix}1&\cdots &0&\cdots &0&\cdots &0\\\vdots &\ddots &\vdots &&\vdots &&\vdots \\0&\cdots &\cos{\theta}&\cdots &-\sin{\theta}&\cdots &0\\\vdots &&\vdots &\ddots &\vdots &&\vdots \\0&\cdots &\sin{\theta}&\cdots &\cos{\theta}&\cdots &0\\\vdots &&\vdots &&\vdots &\ddots &\vdots \\0&\cdots &0&\cdots &0&\cdots &1\end{bmatrix}} }\]

Apply \( G(i,j,\theta )\) to rotate a vector \( \mathbf{x} \) in the \((i,j)\) plane by \(\theta\) radians (counter-clockwise)

1
2
3
4
5
6
7
8
9
10
11
12
13
\begin{equation}
  G(i,j,\theta )={
    \begin{bmatrix}
    1&\cdots &0&\cdots &0&\cdots &0\\
    \vdots &\ddots &\vdots &&\vdots &&\vdots \\
    0&\cdots &\cos{\theta}&\cdots &-\sin{\theta}&\cdots &0\\
    \vdots &&\vdots &\ddots &\vdots &&\vdots \\
    0&\cdots &\sin{\theta}&\cdots &\cos{\theta}&\cdots &0\\
    \vdots &&\vdots &&\vdots &\ddots &\vdots \\
    0&\cdots &0&\cdots &0&\cdots &1
    \end{bmatrix}
    }
\end{equation}

A remarkable formula of Ramanujan

\[\sqrt{\frac{\pi e}{2}}= \cfrac{1}{1+{\cfrac{1}{1+{\cfrac{2}{1+{\cfrac{3}{1+{ {\cfrac{4}{1+{_{\ddots }}} }}}}}}}}} + \left\{1 + \dfrac{1}{1\cdot3}+\dfrac{1}{1\cdot3\cdot5}+\dfrac{1}{1\cdot3\cdot5\cdot7}+\dfrac{1}{1\cdot3\cdot5\cdot7\cdot9}+\cdots\right\}\]
1
2
3
4
5
6
\begin{equation}
  \sqrt{\frac{\pi e}{2}}= \cfrac{1}{1+{\cfrac{1}{1+{\cfrac{2}{1+{\cfrac{3}{1+{ {\cfrac{4}{1+{_{\ddots }}} }}}}}}}}}
  + \left\{
  1 + \dfrac{1}{1\cdot3}+\dfrac{1}{1\cdot3\cdot5}+\dfrac{1}{1\cdot3\cdot5\cdot7}+\dfrac{1}{1\cdot3\cdot5\cdot7\cdot9}+\cdots
  \right\}
\end{equation}

Pseudocode using algorithm2e

Random k-SAT problem generation Random k-SAT problem generation Pseudocode to generate a random \(k\)-SAT problem using SageMath

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
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\begin{algorithm}[H]
\SetArgSty{textrm}
\SetCommentSty{emph}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\underline{Function \texttt{random\_k\_SAT}} ($k$, $n$, $m$ (\texttt{NUMBER\_OF\_CLAUSES}))\;
\Input{$n=$ number of variables and $m=$ number of clauses, $n\leq m$}
\Output{$n$ variables, $m$ clauses in the generated $n$ variables}
\caption{Generate random $k$-SAT}
\Begin{
Create $n$ variables $x_1, x_2,\ldots, x_n$ representing each as a string\;
Create $m$ clauses $C_1, C_2, \ldots, C_{m}$ as empty strings\;
\For{each clause $C_i$}{ %$i\in\{0,1,\ldots,m-1\}$
    Sample $k$ random variables from $x_1,\ldots, x_{n}$ (and sort them)\;
    These are called \texttt{chosen\_variables} $= [x_{i_1}, \ldots, x_{i_k}]$\ ($i_1 < \ldots < i_k$)\;
    \While{True}{
        Randomly generate $k$ \texttt{coefficients} = [$c_1,\ldots, c_k$] where each $c_i\in\{-1,0,1\}$\;
        \tcp{where $c_i$ is the coefficient of $x_{i_k}$}
        \tcp{Exclude the case when all coefficients are zero; i.e., the clause is empty}
        \If{$\exists  i$ such that $c_i\neq 0$}{
            break\;
        }
    }
    \For{each non-empty coefficient $c_i$ in \texttt{coefficients}}{
        \If{$c_i = -1$}{
            Append `$\sim x_{i_k}$' to $C_i$\;
        }
        \If{$c_i = 1$}{
            Append `$x_{i_k}$' to $C_i$\;
        }
        \If{there are more non-empty coefficients}{
            Append `$\mid$' to $C_i$\;
        }
    }
    \tcp{Finally, convert the clause into propositional formula using SageMath}
    $C_i= $\texttt{ sage.logic.propcalc.formula}$(C_i)$\;
}
\Return [$x_1, x_2,\ldots, x_n$], [$C_1, C_2, \ldots, C_{m}$]
}
\end{algorithm}

Code Highlighting using minted

Principal Component Analysis Principal Component Analysis Dimensionality Reduction using Principal Component Analysis in python

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

\begin{minted}[frame = single, breaklines, linenos]{python}
import numpy as np

def PCA(datapoints, PCA_THRESHOLD):
    mean = np.mean(datapoints, axis = 1)
    datapoints = datapoints - mean.reshape((datapoints.shape[0], 1))
    L = datapoints.T @ datapoints
    eigenvalues, eigenvectors = np.linalg.eigh(L)
    eigenvalues = eigenvalues[::-1]
    eigenvectors = eigenvectors[:, ::-1]
    num_components = np.searchsorted(np.cumsum(eigenvalues) / np.sum(eigenvalues), PCA_THRESHOLD, side = "left") + 1
    eigenvectors = datapoints @ eigenvectors[:,:num_components]
    eigenvectors = eigenvectors / np.linalg.norm(eigenvectors, axis = 0)
    eigenvalues = eigenvalues[:num_components]
    return eigenvalues, eigenvectors, mean
\end{minted}

TikZ applications

Molecular Orbital Diagrams using modiagram

Molecular Orbital Diagram Molecular Orbital Diagram Molecular Orbital Diagram for Nitric Oxide

1
2
3
4
5
6
7
8
9
\usepackage{modiagram}

\begin{modiagram}[labels,names]
  \atom[N]{left}{ 2p = {0;up,up,up} }
  \atom[O]{right}{ 2p = {2;pair,up,up} }
  \molecule[NO]{
    2pMO = {1.8,.4;pair,pair,pair,up},
  }
\end{modiagram}

Parse Trees using tikz-qtree

Parse Tree Parse Tree Parse Tree for \(\varphi = (r \rightarrow \neg s) \lor \neg(p \rightarrow (\neg q \lor (r \land (p \rightarrow (s \lor r)))))\)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\usepackage{tikz-qtree}
\tikzset{edge from parent/.style={draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}

\begin{tikzpicture}[every node/.style={draw,circle,minimum width={1em}},level distance=3.5em,sibling distance=3em]
    \Tree
     [.$\lor$
        [.$\rightarrow$  $r$
            [.$\neg$ $s$ ]] 
        [.$\neg$ [.$\rightarrow$ $p$ [.$\lor$ [.$\neg$ $q$
        ]
        [.$\land$  $r$
            [.$\rightarrow$ $p$ [.$\lor$ $s$ $r$ ]]] 
         ] ]
        ]]
\end{tikzpicture}

Electrical Circuits using circuitikz

npn transistor used as an amplifier npn transistor used as an amplifier npn transistor used as an amplifier

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
\usepackage{circuitikz}

\begin{circuitikz}[american voltages]
\draw 
(0,0) to [short,o-,C,l=$C_1$] (2,0)
to [R, l_=$R_1$] (2,3)
to [short,-o] (9,3)
to [open,v^>=$V_{CC}$] (9,-3);
\draw 
(0,-3) to [short,o-*] (2,-3)
to [short,-*] (4,-3)
to [short,-*] (6,-3)
to [short] (8,-3)
to [short,o-o] (9,-3);
\draw 
(6,-3) to[short] node[ground] {} (6,-3.5);
\draw 
(2,0) to [R, l_=$R_2$] [v^>=$V_B$] (2,-3);
\draw
(0,0) to [open,v^>=$V_0\sin(\omega t)$] (0,-3);
\draw 
(4,0) node[npn] (npn) {}
(npn.base) node[anchor=north,xshift=0.5em] {B}
(npn.collector) node[anchor=north,xshift=-0.5em,yshift=0.3em] {C}
(npn.emitter) node[anchor=south,xshift=-0.5em,yshift=-0.4em] {E};
\draw 
(2,0) to [short,i=$i_B$] (3.2,0);
\draw
(4,3) to [R,l_=$R_L$,i=$i_C$,*-*] (4,0.8)
to [short,-o] (8,0.8)
to [open,v^>=$V_{out}$] (8,-3);
\draw 
(4,-0.75) to [short,i=$i_E$] (4,-1.5)
to [R,l_=$R_E$] [v^>=$V_E$] (4,-3);
\draw 
(4,-1.5) to [short] (6,-1.5)
to [C,l_=$C_2$] (6,-3);
\draw 
(4.4,0.8) to [open,v^>=$V_{CE}$] (4.4,-0.8);
\end{circuitikz}

Fitch Proofs using logicproof

Fitch Proofs for Propositional Logic Fitch Proofs for Propositional Logic Fitch Proof of \(\vdash p \rightarrow (q \rightarrow p)\)

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
\usepackage{logicproof}

\begin{logicproof}{4}
p \lor \neg p & LEM on $p$\\
\begin{subproof}
    p & assumption\\
    q \lor \neg q & LEM on $q$\\
    \begin{subproof}
        q & assumption\\
        \begin{subproof}
            p & copy 2\\
            \begin{subproof}
                q & copy 4\\
                p & copy 2
            \end{subproof}
            q \rightarrow p & $\rightarrow$i 6--7
        \end{subproof}
        p \rightarrow (q \rightarrow p) & $\rightarrow$i 5--8
    \end{subproof}
    \begin{subproof}
        \neg q & assumption\\
        \begin{subproof}
            p & copy 2\\
            \begin{subproof}
                q & assumption\\
                p & copy 2
            \end{subproof}
            q \rightarrow p & $\rightarrow$i 12--13
        \end{subproof}
        p \rightarrow (q \rightarrow p) & $\rightarrow$i 11--14
    \end{subproof}
    p \rightarrow (q \rightarrow p) & $\lor$e 3, 4--9, 8--15
\end{subproof}
\begin{subproof}
    \neg p & assumption\\
    q \lor \neg q & LEM on $q$\\
    \begin{subproof}
        q & assumption\\
        \begin{subproof}
            p & assumption\\
            \neg p & copy 17\\
            \bot & $\bot$i 20,21\\
            q \rightarrow p & $\bot$e 22
        \end{subproof}
        p \rightarrow (q \rightarrow p) & $\rightarrow$i 20--23
    \end{subproof}
    \begin{subproof}
        \neg q & assumption\\
        \begin{subproof}
            p & assumption\\
            \neg p & copy 17\\
            \bot & $\bot$i 26,27\\
            q \rightarrow p & $\bot$e 28
        \end{subproof}
        p \rightarrow (q \rightarrow p) & $\rightarrow$i 26--29
    \end{subproof}
    p \rightarrow (q \rightarrow p) & $\lor$e 18, 19--24, 25--30
\end{subproof}
p \rightarrow (q \rightarrow p) & $\lor$e 1, 2--16, 17--31
\end{logicproof}

Templates

For the time being, here’s my stuff. Feel free to use and tweak them however you like. Maybe I will convert them into templates someday.

Report

source code, title page3

Slides using Beamer

source code

Resume

IIT Bombay theme, Generic theme using moderncv

References

This post is licensed under CC BY 4.0 by the author.