Chapter 06 – Creating Tables

Here are the Code examples of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I sometimes use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

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}

figure

Figure 6.1



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}

figure

Figure 6.2



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}

figure

Figure 6.3



Typesetting tables

\documentclass[border=10pt]{standalone}
\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}

figure

Figure 6.4



Understanding formatting arguments

\documentclass[border=10pt]{standalone} 
\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}

figure

Figure 6.5



A table with different vertical alignments

\documentclass[border=10pt]{standalone} 
\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}

figure

Figure 6.6



Increasing the row height

\documentclass[border=10pt]{standalone}
\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}

figure

Figure 6.7



Beautifying tables

\documentclass[border=10pt]{standalone} 
\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}

figure

Figure 6.8



Spanning entries over multiple columns

\documentclass[border=10pt]{standalone} 
\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}

figure

Figure 6.9



Inserting code column-wise

\documentclass[border=10pt]{standalone}
\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}

figure

Figure 6.10



Spanning entries over multiple rows

\documentclass[border=10pt]{standalone}
\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}

figure

Figure 6.11



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}

figure

Figure 6.12



This code is available on Github. It is licensed under the MIT License, a short and simple permissive license with conditions only requiring preservation of copyright and license notices.

Go to next chapter.