(click anywhere to close)
OPEN MENU

[LaTeX] Tables II

category: Writing | course: LaTeX | difficulty:

Last chapter was great, and introduced some useful concepts, but we’re still stuck with a few tiny disadvantages: tables can’t span more than one page, and we’re stuck within this rigid, not easily resizable grid system.

There are multiple ways to solve this, each with different especially convenient 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 any way and at any moment you like. Then, you can use these to align text against one tab stop or the other. These tabbing commands are:

Tabbing Command

Description

\=

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

\kill

Ignores preceding text. Is used to set tab stops without setting text.

\begin{tabbing}
    Column 1 \= Column 2 \= Column 3 \= Column 4 \\
    Lorem \> Lorem \> Lorem \> Lorem \\
    Lorem Ipsum \> \> Lorem Ipsum \\
    Column 1 \= Column 2 \= Column 3 \kill \\
    Lorem Ipsum \> \> Lorem Ipsum
\end{tabbing}
LatexTabbingEnvironment

Because the \=, \’ and \` commands are normally used to produce accents, within a tabbing environment this can be done with \a=, \a’ and \a`, respectively.

Tabbing environments can be split among multiple pages, and you can easily decide on different widths for your table cells.

The tabularx Environment

The tabularx package provides the tabularx environment, which 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 greatly simplifies the table creation process, as it allows you to create tables the exact size you want, while the cells grow naturally with it.

\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\textwidth}{l | X | r}
    Column 1 & Column 2 & Column 3 \\
    Column 1 & Column 2 & Column 3
\end{tabularx}
\end{document}
LatexTabularX

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

\begin{tabu} to dimensions

\usepackage{longtable}
\usepackage{tabu}

\begin{document}
\begin{longtabu} to \textwidth {l | X | r}
    Column 1 & Column 2 & Column 3 \\
    Column 1 & Column 2 & Column 3
\end{longtabu}
\end{document}
LatexTabularX

The table Environment

You might be wondering: why isn’t the default environment called table, instead of tabular?

Well, that’s because the tabular environment is only the actual table contents, while the table environment is a container that allows you to add things to the table that have to do with the document. For example, think of captions, labels, footnotes, setting a different alignment or different position. The syntax is

\begin{table}[position] table body \caption{table title} \end{table}

\begin{table}[h]
    \centering
    \begin{tabular}{l | l | l}
        Cell 1 & Cell 2 & Cell 3 \\
        Cell 1 & Cell 2 & Cell 3
    \end{tabular}
    \caption{What a nice table this is.}
\end{table}
LatexTableEnvironment

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

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
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)