LaTeX Snippets

Inserting Images


\caption{Question 1 System Model}\label{fig:q1_model}

Sometimes you'll get those really annoying images who won't stay to their designated sections. You can use the placeins package to keep them under control.



First you'll need the amsmath package:


Then you can enter math like normal, separating each line with \\. You can use the & symbol to align each line. LaTeX will try to line up all the ampersands.

\frac{D(x) - D_i}{x} &= \frac{D_o - D_i}{1} \\
D(x) &= x \times (D_o - D_i) + D_i  \\
D(x) &= x \times 0.5 + 0.25 \label{eq:q2_diameter}

If you need to embed text in your equations, use the intertext command.

  P_i + \frac{1}{2} \rho V_i^2 &= P(x) + \frac{1}{2} \rho \Big[ V(x) \Big]^2 \\
  \intertext{Let $P_i$ = 0, so P(x) is now gauge pressure (relative to inlet)} \\
  P(x) &= \frac{1}{2} \rho V_i^2 - \frac{1}{2} \rho \Big[ V(x) \Big]^2 \\

Slanted fractions can be accessed via the xfrac package.


  \sfrac{1}{2} - \sfrac{1}{3} &= \sfrac{1}{6}

When using Python and Pandas for numerical analysis, it is almost trivial to set things up so that the results are exported to a file, then included in a LaTeX report automatically.

On the Python side:

with open('data_table.tex', 'w') as f:
    table = data.to_latex(index=False, escape=False)

And in LaTeX:


  \caption{Calculated and Measured Data}\label{tbl:data_table}

To have the equations numbered according to their section (i.e. 1.1, 2.4, etc) instead of using a globally incrementing counter (i.e. 1, 2, 3 etc) use the numberswithin command:


Table from CSV

You can insert tables from a csv using the [csvsimple] package and then just ask it to do all the hard stuff for generating the table.




To start off the appendices section requires just one command:


Thereafter, any section is added as an appendix instead of a normal section.

To embed a PDF as one of your appendices, you can use the pdfpages package:


% Note, I added the front page separately so you can add a \section{} command
% to make the appendix appear in the ToC. You also scale the document down a
% tad to make it fit more nicely
\includepdf[pagecommand=\section{Numerical Analysis}, scale=0.75, pages=1]{./Numerical_Analysis.pdf}
\includepdf[scale=0.75, pages=2-]{./Numerical_Analysis.pdf}


Adding footnotes is pretty straightforward.

I'm writing something here to test \footnote{footnotes working fine}
several features.

Or if you have a particularly long footnote, you can insert a \footnotemark and then write the footnote itself at the bottom of your paragraph.

I'm writing something here to test \footnote[10]{footnotes working fine}
several features. You can write the footnote text\footnotemark in its
own line.
\footnotetext{Second footnote}


(more info)

A nomenclature works pretty much the same way as index entries.

First you need to import the package and let it do some setup stuff:


Then you'll define some symbols:

\nomenclature{$c$}{Speed of light in a vacuum inertial frame}
\nomenclature{$h$}{Planck constant}

And finally tell LaTeX to print the nomenclature:


If you are using latexmk to build your document you need to tell it to do an extra run to generate the nomenclature index. Add this to your ~/.latexmkrc:

# Custom dependency and function for nomencl package
add_cus_dep( 'nlo', 'nls', 0, 'makenlo2nls' );
sub makenlo2nls {
    system( "makeindex -s -o \"$_[0].nls\" \"$_[0].nlo\"" );

You can add units by defining this macro in your preamble:


Then just include \nomunit{} in a description whenever you want to add units.

\nomenclature{$c$}{Speed of light in a vacuum inertial system
  \nomunit{$299,792,458\, m/s$}}


Referencing in LaTeX is designed to be fairly painless and efficient to do.

To cite various sources throughout your work, just add in a \cite{foo}, where foo would be a name for some reference in your *.bib file.

The easiest way to manage your references is with a bib file that sits next to your other tex files. It's just a basic text file with references and their various bits of metadata listed;

    author =       "Ramesh Narayanaswamy",
    title =        "Flow Over Immersed Bodies: Concepts of Lift and Drag - Chapter 6",
    year =         "2016",
    URL =          ""

It's pretty much identical to what you'd normally need to enter in when using Word, except instead of filling in random boxes, it's entered into a text file that anything (not just M$ products) can read. Plus it's easy to put into version control.

Once you've written up your reference database, all you need to do is tell LaTeX to print a bibliography using your foo.bib file, and also the referencing style to use.


Check out this page for a list of the various bibliography and citation styles, and this page for the various fields and entry types for your *.bib file.


I've got a pair of templates I use for pretty much everything.

Code Listings

To insert code snippets you'll need the listings package.


Then you can use the lstlisting environment for your code.

def foo(x):
    """Do something interesting"""
    return x**x

Alternatively, if you've got a file already that you want include, you can input it. Optionally specifying line numbers and language.

\lstinputlisting[language=Octave, firstline=2, lastline=12]{BitXorMatrix.m}

With the lstlisting environment, you can also add a caption or language.

\begin{lstlisting}[language=Python, caption=Python example]


And then you can chuck a list of listings (similar to list of figures, etc) at the front of your document.


Custom Floats

By default, LaTeX comes with figure and table as the only floats available, although it's fairly trivial to add your own custom ones.

First you need to import the float package,


Then define your custom float.


And use it,


class HelloWorldApp {
  public static void main(String[] args) {
    //Display the string
    System.out.println("Hello World!");
  \caption{The Hello World! program in Java.}

From the LaTeX wikibook:

  1. Add \usepackage{float} to the preamble of your document
  2. Declare your new float using: \newfloat{type}{placement}{ext}[outer counter], where:
    • type - the new name you wish to call your float, in this instance, 'program'.
    • placement - t, b, p, or h (as previously described in Placement), where letters enumerate permitted placements.
    • ext - the file name extension of an auxiliary file for the list of figures (or whatever). Latex writes the captions to this file.
    • outer counter - the presence of this parameter indicates that the counter associated with this new float should depend on outer counter, for example 'chapter'.
  3. The default name that appears at the start of the caption is the type. If you wish to alter this, use \floatname{type}{floatname}.
  4. Changing float style can be issued with \floatstyle{style} (Works on all subsequent \newfloat commands, therefore, must be inserted before \newfloat to be effective).
    • plain - the normal style for Latex floats, but the caption is always below the content.
    • plaintop - the normal style for Latex floats, but the caption is always above the content.
    • boxed - a box is drawn that surrounds the float, and the caption is printed below.
    • ruled - the caption appears above the float, with rules immediately above and below. Then the float contents, followed by a final horizontal rule.