LaTeX Snippets

This is a collection of useful $\rm\LaTeX$ snippets for my personal use. I’m sharing it in case someone finds it useful.

LaTeX homework template

You need the fancyhdr package for the page header/footer and the geometry package for the margins.

\documentclass[letterpaper,11pt]{article}
\usepackage{fancyhdr}
\usepackage[left=0.7in, top=0.8in, right=0.7in, bottom=1.05in, footskip=0.3in, headsep=0.15in]{geometry}
\usepackage[utf8]{inputenc}
\pagestyle{fancy}

\usepackage{amsmath, amsthm, amssymb, amsfonts, mathtools, graphicx, underscore}
\usepackage[shortlabels]{enumitem}
\usepackage{amsmath,amssymb,url,multirow,array}
\usepackage{marginnote}
\usepackage{jeffe}
\usepackage[compact]{titlesec}

\titlespacing*{\section} {0pt}{2.5ex plus 1ex minus .2ex}{1.3ex plus .2ex}
\setlist{leftmargin=5mm}
\reversemarginpar

\lhead{CS 473 - HW 1 \\ Daeyun Shin (netid)}
\rhead{\today}
\fancyfoot[C]{\thepage}
\setlength{\headheight}{30pt}

\begin{document}
\begin{description}

\item[Problem 1] \hfill
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
\item Answer
\begin{enumerate}
\item 42
\end{enumerate}
\end{enumerate}

\end{description}
\end{document}

Prof. Erickson’s macros (jeffe package, available here) are very useful when presenting figures and algorithms.

Frequently Used

Table

\begin{tabular}{ l | l | l }
   &   &   \\ \hline
   &   &   \\
\end{tabular}

Matrix

\begin{equation*}
V_{i,j} =
 \begin{pmatrix}
    &  &  &  \\
    &  &  &  \\
    &  &  &  \\
 \end{pmatrix}
\end{equation*}

Image

\includegraphics[scale=1]{../images/plot1.png}

1x4 Image Figure

\begin{figure}[H]
\centering
\begin{tabular}{cccc}
    \includegraphics[width=40mm, clip=true, trim=l b r t]{results/1.png} &
    \includegraphics[width=40mm, clip=true, trim=l b r t]{results/2.png} &
    \includegraphics[width=40mm, clip=true, trim=l b r t]{results/3.png} &
    \includegraphics[width=40mm, clip=true, trim=l b r t]{results/4.png}
\end{tabular}
\caption{Caption goes here.}
\label{figure1}
\end{figure}

Lorem ipsum dolor sit amet (Figure~\ref{figure1}).

Algorithm

Using jeffe.sty

\begin{algorithm}
\textul{$\textsc{Partition}(A[1\,..\,n], p)$:}\+
\\      $\textsl{pivotValue} \gets A[p]$
\>\>\>\>\>\> \Comment{Comment here}
\\      $\textsc{Swap}(A[p], A[n])$
\\      $s \gets 1$
\\[-2ex]
\\      for $i \gets 1$ to $n-1$\+
\\          if $A[i] \le \textsl{pivotValue}$\+
\\              $\textsc{Swap}(A[i], A[s])$
\\              $s \gets s + 1$\-\-
\\[-2ex]
\\      $\textsc{Swap}(A[s], A[n])$
\end{algorithm}

Syntax Highlighting in LaTeX

You can use the listings package to include code with syntax highlighting. For instructions on how to use the listings package, see the manual (PDF).

\documentclass[letterpaper]{article}
\usepackage{fancyhdr}

\usepackage{listings, color, textcomp, setspace}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
        language={[x86masm]Assembler}, % change this
        backgroundcolor=\color{white},
        tabsize=4,
        numberstyle=\tiny,numbers=left,
        upquote=true,
        aboveskip={0.7\baselineskip},
        columns=fixed,
        showstringspaces=false,
        extendedchars=true,
        breaklines=true,
        frame=single,
        rulecolor=,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        identifierstyle=\ttfamily,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
        morecomment=[l]{\#},
        basicstyle=\scriptsize\footnotesize\ttfamily\singlespacing\setstretch{1},
        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        xleftmargin=0.2cm, xrightmargin=0.4cm,
}

\usepackage[left=0.7in, top=1in, right=0.7in, bottom=1in]{geometry}
\pagestyle{fancy}

\lhead{CS 123 - Homework 1 \\ Daeyun Shin }
\rhead{\today}
\fancyfoot[C]{\thepage}

\begin{document}
\begin{description}

\item[Problem 1 - filename.s] \hfill
% include filename.s
\lstinputlisting{filename.s}

\end{description}
\end{document}