Reader small image

You're reading from  LaTeX Beginner's Guide - Second Edition

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801078658
Edition2nd Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Chapter 6: Creating Tables

Scientific documents and others do not just contain plain text; they also present information and data in tables. We shall become familiar with that in this chapter.

In this chapter, we will learn how to perform the following:

  • Using tab stops to write in columns
  • Typesetting tables
  • Adding captions to tables
  • Using packages for further customization

Let's tackle these tasks. We will start by simply arranging text in columns.

Technical requirements

You need to have a LaTeX installation, or you can compile the example code online at https://latexguide.org/chapter-06.

The code is also available on GitHub: https://github.com/PacktPublishing/LaTeX-Beginner-s-Guide-2nd-Edition-/tree/main/Chapter_06_-_Creating_Tables.

In this chapter, we will use the following LaTeX packages: array, booktabs, caption, and multirow.

We will also briefly talk about these packages: color, colortbl, dcolumn, longtable, ltablex, ltxtable, microtype, ragged2e, rccol, rotating, siunitx, stabular, supertabular, tabularx, tabulary, xcolor, and xtab.

Using tab stops to write in columns

Remember the time of the typewriter and early word processing software? When we needed to line up some text in columns, we could use tab stops. LaTeX provides a similar way to easily align text in columns, namely, the tabbing environment.

We want to present a quick overview of LaTeX. We shall present one point on each line, aligned at words and colons, with the help of the following steps:

  1. Begin a new document and open a tabbing environment:
    \documentclass{article}
    \begin{document}
    \begin{tabbing}
  2. Write the text, set tab stops with \=, and end the line with \\:
      \emph{Info:} \= Software \= : \= \LaTeX \\
  3. Add further lines, move to the next tab stop with \>, and again, end lines with \\:
      \> Author \> : \> Leslie Lamport \\
      \> Website \> : \> www.latex-project.org
  4. Close the tabbing environment and end the document:
    \end{tabbing}
    \end{document}
  5. Click the Typeset button to...

Typesetting tables

We might need more complicated structures and formatting, such as centering in columns, dividing lines, or even nested structures. LaTeX provides the tabular environment for typesetting straightforward and complex tables.

We will now create a table of font family commands as in the previous example, but this time, we would like to make all entries in a column horizontally centered to each other. We will also add some horizontal lines to mark the border and the header of the table by following these steps:

  1. Create a new document. Define a command for setting the font for the head row:
    \documentclass{article} 
    \newcommand{\head}[1]{\textnormal{\textbf{#1}}} 
    \begin{document}
  2. Begin a tabular environment. As a mandatory argument, provide ccc, standing for three centered columns:
    \begin{tabular}{ccc}
  3. Write the table head row, add & to separate column entries, and add \\ to end rows. Use \hline to insert horizontal lines:
      \hline 
     &...

Adding captions to tables

Especially with longer text, we would like to add captions and numbers to our tables. Numbering the tables allows easy referencing, whereas captions add information and tell the reader what the table is about. LaTeX has built-in features to achieve that.

Now it's time to complete our table. We shall list the remaining font commands. We'll use the first column to describe the category of the font commands: family, weight, shape, and so on. Then, we will add another column to show the effect of combining font commands.

To finish, we shall center the table and provide a number and a caption. To do that, we will put a table environment around our example table, use \centering inside it, and insert a \caption command at the end of the table environment. We will add more font commands and add another column at the right, containing more examples. Let's break it down into the following steps:

  1. Start with the document's article class...

Using packages for further customizations

When typesetting tables, we may encounter further challenges. For example, we may need column width adjustment, page breaks within a table, color, to rotate a table, and get a specific alignment. In the following sections, we will have a look at additional packages for such purposes.

You can find example tables and links to the documentation at https://latexguide.org/tables for each of the following sections.

Auto-fitting columns to the table width

l, c, and r columns have the width of their content. For p columns, you specify the width. This way, it's hard to find out the actual width of the table. Wouldn't it be a good idea to specify the table width and let LaTeX decide how wide the columns may be? The tabularx package allows that. Using it looks as follows:

\usepackage{tabularx}
...
\begin{tabularx}{width}{column specifiers}
  ...
\end{tabularx}

The new tabularx environment requires an additional argument...

Summary

In this chapter, we have learned how to create tables. Specifically, we dealt with putting text into columns, adding captions to tables, spanning columns and rows, using packages to auto-fit columns, and creating colored, landscape, and even multi-page tables.

We can open the documentation of every mentioned package by running texdoc packagename at the command line or by visiting https://texdoc.org/pkg/packagename.

LaTeX can generate a list of tables like a table of contents. We will deal with such lists in Chapter 8, Listing Contents and References.

Similar to figures, LaTeX numbers our tables automatically. We can use these numbers to reference the tables. Chapter 7, Using Cross-References, is dedicated to referencing, so we will turn to it now.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801078658
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz