LaTeX Document Formatting and Examples

This document provides basic examples of LaTeX documents and examples

Andrew L. Mackey

Overview

LaTeX is useful for generating various types of scientific documentation and reports.

 

General Template

The following is a starter template that students may use.

 

Aligning Mathematical Equations

Use the align* function to align mathematical equations. On each line, you can align everything around the & symbol. Be careful about adding additional line breaks. You can add vertical whitespace by adding [1em] (or any number other than 1) to the end of the line break \\.

\begin{align*}
O( n^3 ) &= 10n^2 + 100n + 50    \\
         &\geq O(10n^2) + O(100n) + O(50)    \\
         &\geq O(n^2) + O(n) + O(1)    \\
         &\geq O(n^2)    \\[1em]
         & \text{This is a true statement!}    \\
\end{align*}

Result

Latex formula example

 

 

Algorithm

The following code demonstrates how to implement the algorithm and algpseudocode packages for including algorithms in your reports.

Required Packages

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\usepackage{amsmath,amsfonts,amsthm}      % standard math libraries

Code Example

\begin{algorithm}
\caption{An example algorithm to demonstrate how to use the \texttt{algorithmic} package}   \label{alg:cap}
	\begin{algorithmic}

	\Function{Build-Crazy-String}{$n$}

		\State $s \gets \emptyset$    \Comment{$s$ will be the string we build}

		\If{$n$ is even}
			\State $s \gets \textit{``crazy"}$  \Comment{$s$ is now a \textit{crazy} string}
		\ElsIf{$n \geq 10$}
			\State $i \gets 0$
			\While{$i \leq n$}
				\State $s \gets s + \textit{``a"}$
				\State $i \gets i+1$
			\EndWhile
		\Else
			\State $s \gets $ ``\textit{uacat}''    \Comment{$s$ is assigned the string \textit{uacat}}
		\EndIf

		\State

		\For{$i \gets 0$ to $n$}
			\State $s \gets s + \textit{``b"}$
		\EndFor

		\vspace{1em}  %add a bit of vertical space

		\State \Return $s$

	\EndFunction

	\end{algorithmic}
\end{algorithm}

Result

Latex algorithm example

 

Tables

The following demonstrates how to expand the rows and columns of a table in Latex.

\bgroup

\def\arraystretch{1.2}	           % row height:  1 is default
\setlength\tabcolsep{1em}          % column width stretching

\begin{tabular}{|c|c|c|c|c|}
	\hline
	\textbf{x} & 0 & 1 & 2 & 3 \\
	\hline
	\textbf{f(x)} & 0 & 2 & 4 & 6  \\
	\hline
\end{tabular}

\egroup