Last chapter introduced the basic concepts needed for a table. But we’re still stuck with a few tiny disadvantages. Like …

  • Tables can’t span more than one page
  • We’re stuck within this rigid grid system that won’t resize
  • It takes a lot of setup to get even a basic working table

There are multiple ways to solve this, each with different optimal use cases.

Tabbing

The tabbing environment provides an alternative way to align text in columns. With special, short tabbing commands, you can set tab stops anywhere you want.

What are tab stops? They are simply invisible markers. They mark a position in the table. Why? So you can easily align text against them! Define the markers; then tell the text to snap to them.

Tabbing CommandDescription
\=Set tab stop
\>Advance to next tab stop
\<Return to previous tab stop
\+Indent—move margin right
\-Unindent—move margin left
\'Pushes everything you’ve typed in the current column to the right edge of the previous column
\`Pushes all text that follows it in the same row, to the right margin of the tabbing environment.
\Starts a new row
\killIgnores preceding text. Is used to set tab stops without setting text.
1\begin{tabbing}
2    Column 1 \= Column 2 \= Column 3 \= Column 4 \
3    Lorem \> Lorem \> Lorem \> Lorem \
4    Lorem Ipsum \> \> Lorem Ipsum \
5    Column 1 \= Column 2 \= Column 3 \kill \
6    Lorem Ipsum \> \> Lorem Ipsum
7\end{tabbing}
Code left > output right
Code left > output right

There’s some conflict here from symbols that have multiple uses. The \=, \' and \` commands are normally used to produce accents. Within a tabbing environment, they won’t. If you want to produce the accents, use \a=, \a' and \a`, respectively.

Tabbing environments can be split over multiple pages! And you can easily decide on different widths for your table cells: simply add different tabs or a different number of tabs in that row.

The tabularx Environment

The tabularx package provides the tabularx environment. It has one extra type of column: X.

This column type will grow wide enough to fit its content, and then fill with white space to give your table the width you want. This simplifies the table creation process. It allows you to create tables the exact size you want, while the cells grow naturally with it.

1\usepackage{tabularx}
2
3\begin{document}
4    \begin{tabularx}{\textwidth}{l | X | r}
5        Column 1 & Column 2 & Column 3 \
6        Column 1 & Column 2 & Column 3
7    \end{tabularx}
8\end{document}
Code above > output below
Code above > output below

Longtables

The automatically supported longtable environment allows a table to span multiple pages. It, however, doesn’t support the stretching X columns. To solve this, use the package longtabu.

The syntax for creating tables this way is

1\begin{tabu} to width columns

The width can be any length. The columns are the same as the default tabular environment.

1\usepackage{longtable}
2\usepackage{tabu}
3
4\begin{document}
5    \begin{longtabu} to \textwidth {l | X | r}
6        Column 1 & Column 2 & Column 3 \
7        Column 1 & Column 2 & Column 3
8    \end{longtabu}
9\end{document}
Code above > output below
Code above > output below

The table Environment

Wait, there was a table environment all this time? Why are we using a tabular environment?!

In most markup languages, features come in pairs. One tag holds the actual content, another is a container that can store this content with metadata.

Example

In HTML (website code), you can show an image with the simple <img src="url" /> tag. But what if you want to add a caption to the image?

Then it’s recommended to wrap the image inside a <figure> tag, and use the <figcaption> tag right after the image.

Yes, I keep referencing HTML/website code, because that is also a markup language and probably the most famous one. Converting this idea to LaTeX, we get …

  • The tabular environment is only the content
  • The table environment is a container that allows adding information about the content. Like captions, labels, footnotes, and more.

The syntax is as you’d expect.

1\begin{table}[position]
2    % ... table body ...
3    \caption{table title} 
4\end{table}
1\begin{table}[h]
2    \centering
3    \begin{tabular}{l | l | l}
4        Cell 1 & Cell 2 & Cell 3 \
5        Cell 1 & Cell 2 & Cell 3
6    \end{tabular}
7    \caption{What a nice table this is.}
8\end{table}
Code left > output right
Code left > output right

Here, the position can take the same arguments as the tabular environment.

Remark

There is a star variation, which will put the table in single-column mode if your document is in multi-column mode.

Continue with this course
Support me and this website!

Want to support me?

Buy one of my projects. You get something nice, I get something nice.

Donate through a popular platform using the link below.

Simply giving feedback or spreading the word is also worth a lot.