Tips for Usage of Latex Table, Figure and Algorithm

  • Author: Qing-Yuan Jiang

1. Tables

  • Links: Table [1], [2], [3], [4]

1.1. Specify Table Cell Width

\documentclass{article}

\usepackage[paperheight=2cm,paperwidth=12cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}

\usepackage{array}
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}

\begin{document}
\begin{table}\centering
\caption{Example 1: specifying cell width.}
\begin{tabular}{|C{1cm}|C{2cm}|R{3cm}|L{3cm}|}
\hline
A & B & Country & Destination\\\hline
A & B & Country & Destination\\
\hline
\end{tabular}
\end{table}
\end{document}
  • Illustration:

1.2. Fill Color in Table Cell

\documentclass{article}

\usepackage[paperheight=1.5cm,paperwidth=12cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}
\usepackage[table]{xcolor}

\begin{document}
\begin{table}\centering
\caption{Example 2: filling color in table cell.}
\begin{tabular}{l|c|r}
  \hline
  Some & \cellcolor{blue!25}coloured & contents \\
  \hline
\end{tabular}
\end{table}
\end{document}
  • Illustration:

1.3. Insert Image into Table Cell

\documentclass{article}

\usepackage[paperheight=3.5cm,paperwidth=8cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}

\usepackage[export]{adjustbox}

\begin{document}
\begin{table}[t]
\centering
\caption{Example 3: Insert image into table.}
\begin{tabular}{|p{1.5cm}|c|}
\hline
Number &   Image \\ 
\hline
A   &   \includegraphics[width=0.3\textwidth,margin=1ex 1ex 1ex 1ex,valign=m]{example-image}  \\
\hline
\end{tabular}
\end{table}
\end{document}
  • Illustration:

1.4. Insert Image into Table Cell

\documentclass{article}

\usepackage[paperheight=5cm,paperwidth=8cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}

\usepackage{enumitem}

\begin{document}

\begin{table}[h!]
\centering
\caption{Example 4: Insert list into table cell.}
\begin{tabular}{|c|p{5cm}|}
\hline
A & List:
\begin{itemize}[topsep=0pt]
\item B.1
\item B.2
\end{itemize}\\ \hline
\end{tabular}
\end{table}

\end{document}
  • Illustration:

2. Figures

  • Links: Figure [1], [2].

2.1. Two Figures Side by Side.

\documentclass{article}
\usepackage[paperheight=6cm,paperwidth=12cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}
\usepackage{graphicx}

\begin{document}

\begin{figure}[t]
\centering
\begin{minipage}{.48\textwidth}
\centering
\includegraphics[width=0.9\linewidth]{example-image}
\caption{A}
\label{fig:A}
\end{minipage} \hspace{5pt}
\begin{minipage}{.48\textwidth}
\centering
\includegraphics[width=0.9\linewidth]{example-image}
\caption{B}
\label{fig:B}
\end{minipage}
\end{figure}
\end{document}
  • Illustration:

2.2. Figures and Tables Side by Side.

\documentclass{article}
\usepackage[paperheight=4cm,paperwidth=12cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage{graphicx}
\usepackage{booktabs}

\begin{document}
\begin{table}
\begin{minipage}[b]{0.49\textwidth}
\centering
\captionof{table}{Example 2: Table}
\label{tab:tab1}
\begin{tabular}[b]{llcc}
\toprule    
Item1 & Item2 & Item3 & Item4 \\
\midrule    
C & 1  & 1  & 1  \\
B & 2  & 2  & 2  \\
C & 3  & 3  & 3  \\
\bottomrule 
\end{tabular}
\end{minipage}
\hfill
\begin{minipage}[b]{0.49\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\captionof{figure}{Example 3: Figure}
\label{fig:fig1}
\end{minipage}
\end{table}
\end{document}
  • Illustration:

3. Algorithm

  • Links: Algorithm [1].

3.1. Algorithm with Comments

\documentclass{article}
\usepackage[paperheight=7cm,paperwidth=7cm,left=0cm, right=0cm, top=0cm, bottom=0cm]{geometry}

\usepackage{xcolor}
\usepackage{amsmath}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]
{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

\begin{document}
\begin{algorithm}[t]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\DontPrintSemicolon
\SetAlgoLined
\SetNoFillComment
\LinesNotNumbered 
\Input{~Training set $X$, labels $L$.}
\Output{~The parameters $\theta$.}
\textbf{INIT} initialize variables.\;
\For{$t=1\rightarrow \text{Epochs}$}{
    \tcc{one epoch.}
    \For{$i=1\rightarrow m$}{
        Sample a batch.\;
        Forward.\;
        Calculate Loss.\;
        Backward.\;
    }
    \tcc{update learning rate.}
    Updating learning rate.\;
}
\caption{Algorithm Example 1}
\end{algorithm}
\end{document}
  • Illustration: