LaTeX for Computing Students

Created: 21st July 2018

About a month ago, I finished my first year at Imperial, reading Computing. Throughout the year, I’ve used LaTeX to write up my notes, solutions to maths assignments and group project reports. Here are a few things I’ve found useful as a Computing student to get the most out of LaTeX.

Typesetting Code

To typeset code in LaTeX, I use the package minted which provides syntax highlighting. This package uses a program called Pygments, which is written in Python. Having installed Python, Pygments can be installed using pip:

pip install Pygments

After that, you can use it as follows:

print("Hello World")

The argument to minted specifies the language. You can see the full list of supported languages on Pygments’ website. It is quite extensive. To compile the LaTeX document with pdflatex, you have to use the flag --shell-escape which allows the LaTeX source code to call shell commands.

To typeset code inline i.e. within a block of text, I use the \texttt command e.g. \texttt{print("Hi")}.

Typesetting Trees

There were a number of instances, where I needed to draw a tree. This can be done quite easily with the qtree package. To draw a tree with a root node “A”, and child nodes “B” and “C”, write the following:

\Tree [.A B C ]

The square brackets and dot indicate that A is a node with children. To give B a child D:

\Tree [.A [.B D] C ]

There is also another package called tikz-qtree that I use to typeset trees. This has the same basic syntax as qtree, but lets you give the arcs labels. For instance going back to our example with the tree with a root node “A” and two children “B” and “C”, to give the arcs between “A” and its children labels:

\Tree [.A \edge node[auto=right]{Edge to B}; B \edge node[auto=left]{Edge to C}; C ]

The auto property controls whether the arc is left or right of the label.

Typesetting Algorithm Pseudocode

Normally, I write up algorithms using pseudocode, rather than use a particular programming language, so I can abstract away the irrelevant details. To do this I use the clrscode3e package, which was made for the book Introduction to Algorithms often known as CLRS. A simple function might be written as follows:

\li \While $\id{size} \neq 0$ \Do
    \li $\proc{Swap}(A, 0, \id{size})$
    \li $\id{size} = \id{size} - 1$
    \li $\proc{Fix-Max-Heap}(A)$

The pseudocode is contained within a codebox. \proc is a nice way of typesetting function names, and \id is a nice way of typesetting identifiers. Both can only be used in maths mode. This is why when defining the function name using \Procname, I had to put the dollars. \li indicates a line of code. \Do causes the level of indentation to increase. \End causes the level of indentation to decrease. The actual lines and indentation in the LaTeX source code do not affect the presentation.

Other things to note:

  • There are special commands defined for keywords such as \If and \While.
  • There is a command \const for constants, e.g. \const{true}.
  • There is a command for attributes e.g. \attrib{s}{length}, which means you don’t need \id.
  • You can make your own keywords e.g. \kw{break}.

The package automatically adds line numbers. The great thing is that since you have access to maths mode, you can incorporate the necessary mathematical notation in your pseudocode.

Typesetting Proofs

When doing courses such as Logic, it was necessary to write proofs using a formal proof system such as natural deduction. For these proofs I used the logicproof package. Here is a simple example:

        A & ass \\
        B & somehow
    A \to B & $\to I(1, 2)$

The numeric argument indicates how deep the nested proofs go. In this case, only 1 level of depth. Nested proofs have a box drawn around them, and are created using the subproof environment. A new proof line is created using \\. This isn’t needed after the last line. The & is used to separate the formula and the description of how the formula was reached. The part for the formula before the ampersand is already in maths mode.

The logicproof package is for Fitch-style proofs, which use boxes. There is an alternative package bussproofs for tree proofs, but I found logicproof far easier to use.

Other Things

Some other randon things, I’ve found useful:

  • The physics package has an \abs command, for the absolute value.
  • The hyperref package means the items in table of contents link to the corresponding pages in the outputted PDF. It also lets you link to websites.
  • The fancyhdr package lets you control the headers and footers.
  • The multirow package can be used when making tables with the tabular environment. It lets you have a cell that spans multiple rows or multiple columns.
  • I find it very useful to have macros for the floor and the ceiling function, so I can do \floor{x}:
\newcommand\ceil[1]{\ensuremath{\lceil #1 \rceil}}
\newcommand\floor[1]{\ensuremath{\lfloor #1 \rfloor}}