Gice

Technology and General Blog

LaTeX, pronounced as Lay-Tek or Lah-tek, is a documentation language for creating professional documents. Its most common use is technical and scientific documentation because it provides a What You See Is What You Mean approach. This method allows you to focus only on the contents of your document without worrying about the formatting.

This tutorial aims to teach you how to use LaTeX to create various table types and populate them with data.

NOTE: This tutorial assumes you are not new to LaTeX; it does not serve as an introduction to LaTeX.

How to Create a Simple Table With LaTeX

Tables are standard when working with scientific documents. LaTeX offers an extensive collection of tools you can use to create and customize various table elements.

To create a simple table in LaTeX, use the tabular environment.

To separate columns, use the ampersand symbol &. To separate rows, use the new line symbol

The following LaTeX code creates a simple table.

documentclassarticle
usepackage[utf8]inputenc

titleLinuxHint – LaTeX tables
authorLinuxHint
dateJune 2021

begindocument
begincenter
begintabularc
    1 & 2 & 3 & 4
    5 & 6 & 7 & 8
    9 & 10 & 11 & 12
endtabular
endcenter
maketitle
enddocument

Use the tabular environment to tell the LaTeX compiler that you wish to create a table.

Inside the tabular environment, you must specify the parameters defining the number of columns to insert. For example, four (c) values indicate four centered columns.

Once you compile the code below, you should get an output as:

How to Add A Horizontal Line

You can use the hline command to add a horizontal line at the top and bottom of the table.

The code for that is:

documentclassarticle
usepackage[utf8]inputenc

titleLinuxHint – LaTeX tables
authorLinuxHint
dateJune 2021

begindocument
begincenter
begintabularc
hline
    1 & 2 & 3 & 4
    5 & 6 & 7 & 8
    9 & 10 & 11 & 12
    hline
endtabular
endcenter
maketitle
enddocument

Once you compile the code, you should get a table with a horizontal line at the top and bottom as:

To create an enclosed table with vertical lines on both sides, you can specify two pipes at the beginning of the column definition as:

begintabular c

A full example code for this is:

documentclassarticle
usepackage[utf8]inputenc

titleLinuxHint – LaTeX tables
authorLinuxHint
dateJune 2021

begindocument
begincenter
begintabular
hline
    1 & 2 & 3 & 4
    5 & 6 & 7 & 8
    9 & 10 & 11 & 12
    hline
endtabular
endcenter
maketitle
enddocument

Once you compile the code above, you should get an output similar to the one shown below:

How to Align Column Text

LaTeX allows us to allow column text to the right, left, and center. By default, LaTeX uses c to align the text center.

To set the text to right or left, use r and l respectively.

For example, the following blocks show how to create tables with the right text-align.

documentclassarticle
usepackage[utf8]inputenc

titleLinuxHint – LaTeX tables
authorLinuxHint
dateJune 2021

begindocument
begincenter
begintabular r
hline
    1 & 2 & 3 & 4
    5 & 6 & 7 & 8
    9 & 10 & 11 & 12
    hline
endtabular
endcenter
maketitle
enddocument

How to Create a Multi-Page Table With LaTeX

To create a table that takes up two or more pages, you need to use the longtable package. To do this, enter: the line

Specifying the longtable package allows the tables to be broken down and combined using LaTeX page break tools.

To create a long table, you need to add four commands.

  • endfirsthead – The content that precedes this command is allocated at the beginning of the table on the first page.
  • endhead – The content between this command and endfirsthead is allocated at the top of the table on every page except the first one.
  • endfoot – The content is allocated at the bottom of every page except the last one.
  • endlastfoot – Displayed at the bottom on the last page where the table ends.

The following creates a simple multi-page table.

documentclassarticle
usepackage[utf8]inputenc
usepackagelongtable

begindocument
beginlongtable [c] c
labellong

hline
multicolumn2 Start Table
hline
Hello & World
hline
endfirsthead

hline
multicolumn2 c Continue table to pages
hline
Hello & World
endfirsthead

hline
multicolumn2 c Start Another Tablereflong
hline
endhead
hline
endfoot
hline
multicolumn2 This Ends the Table
hline
endlastfoot
[REPEAT multi-column]
endlongtable
enddocument

How to Combine Rows and Columns In LaTeX

You can use the command multirow and multi-column to combine rows and columns.

Multi-columns

The general syntax to combine multiple columns is:

multicolumnNumber_of_columnsaligncontent

For example, consider the code below:

documentclassarticle
usepackage[utf8]inputenc
begindocument
begintabular p 3cm
hline
multicolumn4 Trek List
hline
Name & Release Date & Director & Story By
hline
Star Trek: The Motion Picture & December 7, 1979, & Robert Wise & Alan Dean Foster
Star Trek II: The Wrath of Khan & June 4, 1982, & Nicholas Meyer & Harve Bennett
Star Trek V: The Final Frontier &   June 9, 1989, & William Shatner & William Shatner
hline
endtabular
% Data Source -> “https://en.wikipedia.org/wiki/List_of_Star_Trek_films
enddocument

NOTE: It is good to ensure the columns are spaced evenly by specifying the dimensions.

In the the command multicolumn4 Trek List

The 4 defines the number of columns to combine.

The next part c defines the delimiters and the alignments for the columns.

Trek List – The name for the combined columns.

Once you compile the LaTeX code above, you should get an output as:

Multi-Rows

To combine rows using the multirow command, you need to import the multirow package.

The following example code shows how to combine rows.

documentclassarticle
usepackage[utf8]inputenc
usepackagemultirow
begindocument
begincenter
begintabularc
hline
Column1 & Column2 & Column3 & Column4
hline
multirow36cmCombined rows (cells) & cell1 & cell2
& cell3 & cell4
& cell5 & cell6
hline
endtabular
endcenter
enddocument

Examining the command: multirow36cmCombined rows (cells) & cell1 & cell2

You will get three parameters:

The first one is the number of rows to combine. So in this example, 3 rows.

Next, the second parameter defines the width of the column. In this example, 6cm.

Finally, the last parameter defines the content inside the cell.

Compiling the code above should give a table similar to

About Table Captions, Label, and References

You can create table captions and labels, which you can use to display information about the table or reference it.

To add a caption to a table, use the caption command. You can place the table caption below or above the table.

For example:

documentclassarticle
usepackage[utf8]inputenc
begindocument
begintable[h!]
centering
captionInformation about Star Trek Films
begintabular p 3cm
hline
multicolumn3 c Trek List
hline
Name & Release Date & Director
hline
Star Trek: The Motion Picture & December 7, 1979, & Robert Wise
Star Trek II: The Wrath of Khan & June 4, 1982, & Nicholas Meyer
Star Trek V: The Final Frontier &   June 9, 1989, & William Shatner
hline
endtabular
labeltreks
endtable
% Data Source -> “https://en.wikipedia.org/wiki/List_of_Star_Trek_films
enddocument

Once you compile the code, you should get a table with a caption at the top, as shown in the image below:

Conclusion

This tutorial has discussed the basics of creating and working with Tables in LaTeX.

As you know, LaTeX is a powerful tool, and this tutorial does not scratch the surface of how to work with LaTex tables.

The LaTex documentation is a great reference guide. Please refer to it as needed.

Leave a Reply

Your email address will not be published. Required fields are marked *