Chapter 06 – Creating Tables

Using tab stops to write in columns

\documentclass{article}
\begin{document}
\begin{tabbing}
  \emph{Info:} \= Software \= : \= \LaTeX \\
  \> Author    \> : \> Leslie Lamport \\
  \> Website   \> : \> www.latex-project.org
\end{tabbing}
\end{document}

Undesired overlapping aligned text

\documentclass{article} 
\newcommand{\head}[1]{\textbf{#1}} 
\begin{document} 
\begin{tabbing}
  \= \head{Command} \= \head{Declaration} \= \head{Example}\\
  \> \verb|\textrm{...}| \> \verb|\rmfamily| \> \rmfamily text\\
  \> \verb|\textsf{...}| \> \verb|\sffamily| \> \sffamily text\\
  \> \verb|\texttt{...}| \> \verb|\ttfamily| \> \ttfamily text
\end{tabbing}
\end{document}

Corrected aligned text

\documentclass{article} 
\newcommand{\head}[1]{\textbf{#1}} 
\begin{document} 
\begin{tabbing}
  \= \verb|\textrm{...}| \= \head{Declaration} \=
     \head{Example}\kill
\> \head{Command} \> \head{Declaration} \> \head{Example}\\
\> \verb|\textrm{...}| \> \verb|\rmfamily| \> \rmfamily text\\
\> \verb|\textsf{...}| \> \verb|\sffamily| \> \sffamily text\\
\> \verb|\texttt{...}| \> \verb|\ttfamily| \> \ttfamily text
\end{tabbing}
\end{document}

Typesetting tables

\documentclass{article} 
\newcommand{\head}[1]{\textnormal{\textbf{#1}}} 
\begin{document}
\begin{tabular}{ccc}  \hline 
  \head{Command} & \head{Declaration} & \head{Output}\\ 
  \hline
  \verb|\textrm| & \verb|\rmfamily| & \rmfamily Example text\\ 
  \verb|\textsf| & \verb|\sffamily| & \sffamily Example text\\ 
  \verb|\texttt| & \verb|\ttfamily| & \ttfamily Example text\\ 
  \hline 
\end{tabular} 
\end{document}

Understanding formatting arguments

\documentclass{article} 
\newcommand{\head}[1]{\textnormal{\textbf{#1}}} 
\begin{document}
\begin{tabular}{|l|c|r|p{1.7cm}|}
  \hline
  left & centered & right & a fully justified paragraph cell\\
  \hline
  l & c & r & p\\
  \hline
\end{tabular}
\end{document}

A table with different vertical alignments

\documentclass{article} 
\usepackage{array}
\begin{document}
\begin{tabular}{@{}lp{1.2cm}m{1.2cm}b{1.2cm}@{}}
  \hline
  baseline & aligned at the top & aligned at the middle
  & aligned at the bottom\\
  \hline
\end{tabular}
\end{document}

Increasing the row height

\documentclass{article}
\usepackage{array}
\setlength{\extrarowheight}{4pt}
\begin{document}
\begin{tabular}{@{}>{\itshape}ll!{:}l<{.}@{}}
  \hline
  Info:     & Software & \LaTeX\\
            & Author   & Leslie Lamport\\
            & Website  & www.latex-project.org\\
  \hline
\end{tabular}
\end{document}

Beautifying tables

\documentclass{article} 
\usepackage{booktabs}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}} 
\begin{document}
\begin{tabular}{ccc}
  \toprule[1.5pt]
  \head{Command} & \head{Declaration} & \head{Output}\\
  \midrule
  \verb|\textrm| & \verb|\rmfamily| & \rmfamily Example text\\
  \verb|\textsf| & \verb|\sffamily| & \sffamily Example text\\
  \verb|\texttt| & \verb|\ttfamily| & \ttfamily Example text\\
  \bottomrule[1.5pt]
\end{tabular}
\end{document}

Spanning entries over multiple columns

\documentclass{article} 
\usepackage{booktabs}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}} 
\begin{document}
\begin{tabular}{@{}*{3}l@{}}
  \toprule[1.5pt]
  \multicolumn{2}{c}{\head{Input}} &
  \multicolumn{1}{c}{\head{Output}}\\
  \head{Command} & \head{Declaration} & \\
  \cmidrule(r){1-2}\cmidrule(l){3-3}
  \verb|\textrm| & \verb|\rmfamily| & \rmfamily Example text\\
  \verb|\textsf| & \verb|\sffamily| & \sffamily Example text\\
  \verb|\texttt| & \verb|\ttfamily| & \ttfamily Example text\\
  \bottomrule[1.5pt]
\end{tabular}
\end{document}

Inserting code column-wise

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\begin{document}
\begin{tabular}{@{}l*{2}{>{\ttfamily\textbackslash}l}l%
  <{Example text}@{}}
  \toprule[1.5pt]
  & \multicolumn{2}{c}{\head{Input}} &
  \multicolumn{1}{c}{\head{Output}}\\
  & \normal{\head{Command}} & \normal{\head{Declaration}}
  & \normal{}\\
  \cmidrule(lr){2-3}\cmidrule(l){4-4}
  Family & textrm & rmfamily & \rmfamily\\
         & textsf & sffamily & \sffamily\\
         & texttt & ttfamily & \ttfamily\\
  \bottomrule[1.5pt]
\end{tabular}
\end{document}

Spanning entries over multiple rows

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\begin{document}
\begin{tabular}{@{}l*{2}{>{\textbackslash\ttfamily}l}l%
  <{Example text}@{}}
  \toprule[1.5pt]
  & \multicolumn{2}{c}{\head{Input}} &
  \multicolumn{1}{c}{\head{Output}}\\
  & \normal{\head{Command}} & \normal{\head{Declaration}}
  & \normal{}\\
  \cmidrule(lr){2-3}\cmidrule(l){4-4}
  \multirow{3}{*}{Family} & textrm & rmfamily & \rmfamily\\
                          & textsf & sffamily & \sffamily\\
                          & texttt & ttfamily & \ttfamily\\
  \bottomrule[1.5pt]
\end{tabular}
\end{document}

Adding captions to tables

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\begin{document}
\begin{table}
  \centering
  \begin{tabular}{@{}l*{2}{>{\textbackslash\ttfamily}l}%
    l<{Example text}l@{}}
    \toprule[1.5pt]
    & \multicolumn{2}{c}{\head{Input}}
    & \multicolumn{2}{c}{\head{Output}}\\
    & \normal{\head{Command}}    & \normal{\head{Declaration}}
    & \normal{\head{Single use}} & \head{Combined}\\
    \cmidrule(lr){2-3}\cmidrule(l){4-5}
    \multirow{3}{*}{Family} & textrm & rmfamily & \rmfamily & \\
                            & textsf & sffamily & \sffamily & \\
                            & texttt & ttfamily & \ttfamily & \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-4}
    \multirow{2}{1.1cm}{Weight} & textbf & bfseries & \bfseries
    & \multirow{2}{1.8cm}{\sffamily\bfseries Bold and
      sans-serif}\\
                            & textmd & mdseries & \mdseries & \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-4}
    \multirow{4}{*}{Shape}  & textit & itshape  & \itshape  & \\
                            & textsl & slshape  & \slshape  &
    \multirow{2}{1.8cm}{\sffamily\slshape Slanted and
      sans-serif}\\
                            & textsc & scshape  & \scshape  & \\
                            & textup & upshape  & \upshape  & \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-4}
    Default & textnormal & normalfont & \normalfont & \\
    \bottomrule[1.5pt]
  \end{tabular}
  \caption{\LaTeX\ font selection}
\end{table}
\end{document}