How to Create LaTeX tables

LaTeX tables Creating is a versatile document creation tool that allows you to present information in many different ways. 

If you plan to include large quantities of data or information in your document, you may wish to include it in a table.  The good news is that LaTeX lets you create customized tables using a few key commands. Let’s examine how to create LaTeX tables and customize them according to your needs.

Table of Contents

Creating LaTeX Tables Using Table and Tabular Environments

To create LaTeX tables, you must first create a table environment followed by a tabular environment. The table environment lets you create captions while the tabular environment enables you to insert values and margins for your table.

You can create a tabular environment using the commands \begin{tabular}{….} and \end{tabular}.

An example of a simple table is shown below:

\begin{center}
\begin{tabular}{ |c|c|c| } 
 \hline
 cellnumber1 & cellnumber2 & cellnumber3 \\ 
 cellnumber4 & cellnumber5 & cellnumber6 \\ 
 cellnumber7 & cellnumber8 & cellnumber9 \\ 
 \hline
\end{tabular}
\end{center}

The above code creates three-column latex tables, each with three rows not separated by a line. We used the command { |c|c|c| } to first declare three columns separated by vertical lines. The “c” in the above command informs LaTeX to center the values in each column. Alternatively, we could use the parameter “r” to align the column text towards the right, or “l” to align it to the left.

We used the command \hline twice to add horizontal borders to the top and bottom of the table. You can use this command as many times as needed to add horizontal borders to your tables.

We then inserted our cell values “cellnumber1”, “cellnumber2”, and so forth. Each value in the code cellnumber1 & cellnumber2 & cellnumber3 \\ is separated by an “&”. This “&” informs LaTeX to separate the values individually in columns, while the “\\” indicates the end of a row.

If we wanted to set up latex tables with horizontal lines between each row and no lines between columns, the code would resemble:

\begin{center}
\begin{tabular}{||c c c c||} 
 \hline
 Column1 & Column2 & Column3 & Column4 \\ [0.5ex] 
 \hline\hline
 1 & 232 & 486 & 524 \\ 
 \hline
2 & 38 & 39 & 413 \\
 \hline
 3 & 747 & 831 & 5201 \\
 \hline
 4 & 604 & 13211 & 8184 \\
 \hline
 5 & 96 & 233 & 1681 \\ [1ex] 
 \hline
\end{tabular}
\end{center}

In the above code, we used the command  \hline\hline. This creates a gap between the first row containing our table headers and the second row that contains the first set of values. 

We also used the command \begin{tabular}{||c c c c||} to describe the table format. We wanted four values separated but without vertical column lines. The “||” in this code inserts a double line at each side of the latex tables.


Related content for LaTex:


Creating Tables With a Fixed Length

In some cases, you may wish to create LaTeX tables with a fixed length. LaTeX allows you to do this, and also create tables where the length of each column is fixed. This is made possible by using the array package with the command \usepackage{array}. We demonstrate this in the following example:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{center}
\begin{tabular}{ | m{5em} | m{1cm}| m{1cm} | } 
  \hline
  cell1 example text example text example text & cell2 & cell3 \\ 
  \hline
  cell1 example text example text example text & cell5 & cell6 \\ 
  \hline
  cell7 & cell8 & cell9 \\ 
  \hline
\end{tabular}
\end{center}
\end{document}

The above code produces latex tables where the width of the first column is much greater than the width of the second and third columns. We achieved this using the command m{5em} to set the first column’s length as 5em. We then set the length of the other two columns to 1cm using the code m{1cm}

The parameter “m” informs LaTeX to place the text in the middle of the column. Alternatively, we could have used the parameter “p” to place the text at the top of the column and “b” to place it at the bottom.

You should note that you do not need to set the width of each cell, as LaTeX tables can do this automatically. It is possible to make each cell equal width with the spaces distributed equally using the tabularx package. This package can be loaded using the command \usepackage{tabularx}.

We show an example of a 3 x 2 table where each cell is equal in width below.

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{0.7\textwidth} { 
  | >{\raggedright\arraybackslash}X 
  | >{\centering\arraybackslash}X 
  | >{\raggedleft\arraybackslash}X | }
 \hline
 cell1 & cell2 & cell3 \\
 \hline
 cell4  & cell5  & cell6  \\
\hline
\end{tabularx}
\end{document}

In the above LaTeX Tables, the text in the first column is aligned left, the second column text is aligned to the middle, while the third column text is aligned to the right. We achieved this using the code 

| >{\raggedright\arraybackslash}X 

| >{\centering\arraybackslash}X 

 | >{\raggedleft\arraybackslash}X | }

We also set the table width to be 70% of the document text width using the command 0.8\textwidth.


Related Content


How to Merge Rows and Columns

LaTeX tables can be set to merge columns or rows to make larger cells. This can be done using the command \multicolumn. We can demonstrate this with the following example:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
 \hline
 \multicolumn{4}{|c|}{Dog Listings} \\
 \hline
 Dog Name& Breed&Weight&Height\\
 \hline
 Alfie  & Rottweiler    &33&   24\\
 Romeo&   Dachshund  &14  &8\\
 Charles &Golden Retriever & 28&  20\\
 \hline
\end{tabular}
\end{document}

In the above code, we used the command \multicolumn{4}{|c|}{Dog Listings} \\ to produce latex tables where the first row consists of a single cell made from merging four columns. 

The {4} in this code describes the number of columns we wish to combine. The {|c|} command informs LaTeX to center the text and place a vertical line on each side. The code {Dog Listings} places the text “Dog Listing” inside the merged cell.

If we wanted to create a table where the second, third, and fourth rows are merged, the code would be:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{center}
\begin{tabular}{ |c|c|c|c| } 
\hline
column1 & column2 & column3 \\
\hline
\multirow{3}{4em}{Three Rows Combined} & number2 & number3 \\ 
& number5 & number6 \\ 
& number8 & number9 \\ 
\hline
\end{tabular}
\end{center}
\end{document}

The above code includes the command \multirow{3}{4em}{Three Rows Combined} & number2 & number3 \\ where “3” specifies that three rows should be combined, and 4em indicates the column width.

Creating Latex Tables That Span Multiple Pages

You may need to create a very long table to include a large amount of information at some point. If such a table is expected to span across multiple pages, you will need to use the longtable package in LaTeX. This package can be added using the command \usepackage{longtable} in the document preamble.

We demonstrate how to create multi-page LaTeX tables using the following code:

\documentclass{article}
\usepackage{longtable}
 
\begin{document}
 
 \begin{longtable}[c]{| c | c |}
 \caption{Caption for Table.\label{long}}\\
 
 \hline
 \multicolumn{2}{| c |}{First Row in the Table}\\
 \hline
 Value1 & Value2\\
 \hline
 \endfirsthead
 
 \hline
 \multicolumn{2}{|c|}{Continuation of same Table \ref{long}}\\
 \hline
 Value1 & Value2\\
 \hline
 \endhead
 
 \hline
 \endfoot
 
 \hline
 \multicolumn{2}{| c |}{End of this Table}\\
 \hline\hline
 \endlastfoot
 
Content for the table\\
Content for the table\\
Content for the table\\
Content for the table\\
Content for the table\\
Content for the table\\
Content for the table\\
 ...
Content for the table\\
 \end{longtable}

The longtable environment works similarly to the tabular environment. However, it allows latex tables to span across multiple pages without letting LaTeX’s page-breaking algorithm ruin the format. Using the longtable package also requires you to use certain commands in your code. The ones we used are shown below.

\endfirsthead: The text displayed above this command will show at the start of your table on the first page.

\endhead: The text placed before this command and after \endfirsthead will be shown at the start of the table on each page with the exception of the first page.

\endfoot: The text placed before this command and after \endhead will be shown at the bottom of your table on each page with the exception of the first page.

\endlastfoot: The text placed before this command and after \endfoot will be shown at the bottom of your table on the last page.

Positioning the Latex Tables Text

In the above examples, we spoke briefly about aligning text in their respective columns. However, it is possible to perform more complex positioning using a float table environment in LaTeX tables.

We demonstrate this in the following example:

\documentclass{article}
\begin{document}
The following table is positioned here:
\begin{table}[h!]
\centering
 \begin{tabular}{||c c c c||} 
 \hline
 Column1 & Column2 & Column2 & Column3 \\ [0.5ex] 
 \hline\hline
 4 & 47 & 5434 & 732\\ 
 7 & 58 & 41 & 9414\\
 3 & 63 & 38 & 6801\\
11 & 89 & 2300 & 4313\\
 23 & 101 & 813 & 7488\\ [1ex] 
 \hline
 \end{tabular}
\end{table}
\end{document}

In the above code, the “h!” informs LaTeX to place the table underneath instead of its default position. Some of these parameters are explained below:

  • h places the table “here” in approximate terms.
  • H places the table exactly at this location.
  • t places the table at the top of your page.
  • b places the table at the bottom of your page.
  • p places the table in a specified special page.
  • ! informs LaTeX to override its default parameters for table placement.

Underneath the code mentioned above, we used the command \centering to center our table relative to the float’s container element. The command \[1ex] tells LaTeX to add extra space to this cell.

Captioning, Labelling, and Referencing Latex Tables

LaTeX allows you to caption, label, and reference your table with the help of the table environment. We demonstrate how to do each of these in the following example:

\documentclass{article}
\begin{document}
Table \ref{table:1} is a referenced element in this document.
\begin{table}[h!]
\centering
 \begin{tabular}{||c c c c||} 
 \hline
 Column1 & Column2 & Column2 & Column3 \\ [0.5ex] 
 \hline\hline
 4 & 47 & 5434 & 732\\ 
 7 & 58 & 41 & 9414\\
 3 & 63 & 38 & 6801\\
11 & 89 & 2300 & 4313\\
 23 & 101 & 813 & 7488\\ [1ex] 
 \hline
 \end{tabular}
\caption{This is the caption for this table.}
\label{table:1}
\end{table}
\end{document}

In the above code, the command \caption{This is the caption for this table.} creates the caption “This is the caption for this table”. We placed this command underneath \end{table} so the caption was placed under the table.

We included the command \label{table:1} to label the table. After setting this label, we can reference it using the \ref{table:1} command.

Creating a List of Latex Tables

LaTeX allows you to create a list of all the tables in your document using the \listoftables command. This list will feature the caption used for each table. These demonstrate how to create a list of tables in the following example:

\documentclass{article}
\begin{document}
\listoftables
\vspace{5pt}
Table \ref{table:1} is a referenced element in this document.
\begin{table}[h!]
\centering
 \begin{tabular}{||c c c c||} 
 \hline
 Column1 & Column2 & Column2 & Column3 \\ [0.5ex] 
 \hline\hline
 4 & 47 & 5434 & 732\\ 
 7 & 58 & 41 & 9414\\
 3 & 63 & 38 & 6801\\
11 & 89 & 2300 & 4313\\
 23 & 101 & 813 & 7488\\ [1ex] 
 \hline
 \end{tabular}
\caption{This is the caption for this table.}
\label{table:1}
\end{table}
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||} 
 \hline
 Column1 & Column2 & umnColumn2 & Col3 \\ [0.5ex] 
 \hline\hline
  7 & 613 & 2718 & 8500 \\
 8 & 79 & 632 & 7210 \\ [1ex] 
 \hline
\end{tabular}
\caption{This caption is for the second table.}
\label{table:2}
\end{table}
\end{document}

After compiling the above code, we will see each table mentioned in a List of Tables section. These tables will be numbered and their corresponding pages will also be shown in this section.

Adding Colors to Tables

If you want to add some color to your tables, you can do this with the help of the xcolor package and using the table options. We demonstrate how to do this in the following example:

\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{2.5}
\begin{document}
{\rowcolors{3}{green!80!yellow!50}{green!70!yellow!40}
\begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|  }
\hline
\multicolumn{3}{|c|}{Dog Listings} \\
\hline
 Dog Name& Breed&Weight\\
\hline
 Alfie  & Rottweiler    &33\\
 Romeo&   Dachshund  &14\\
 Charles &Golden Retriever & 28\\
\hline
\end{tabular}
\end{document}

In the above code we used the command \rowcolors{3}{green!80!yellow!50}{green!70!yellow!40}. The “3” indicates which row to start using the colors from. {green!80!yellow!50} specifies the row color for odd rows while {green!70!yellow!40} specifies the color for even rows.

Changing the Color of Different Table Elements

\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{2.5}
\newcolumntype{s}{>{\columncolor[HTML]{AAACED}} p{3cm}}
\arrayrulecolor[HTML]{DB5800}
\begin{document}
\begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|  }
\hline
\rowcolor{lightgray} \multicolumn{3}{|c|}{Dog Listings} \\
\hline
 Dog Name& Breed&Weight\\
\hline
 Alfie  & Rottweiler    &33\\
\rowcolor{gray}
 Romeo&   Dachshund  &14\\
 Charles &Golden Retriever & \cellcolor[HTML]{AA0044} 28\\
\hline
\end{tabular}
\end{document}

In the above code, we set the table line color using the command \arrayrulecolor. We then set the background color of a single cell using the command \cellcolor

We set the background color of an entire row using the command \rowcolor. 

We also set the background color for an entire column using the command \newcolumntype{s}{>{\columncolor[HTML]{AAACED}} p{3cm}}. This code defines the column type as “s” and the column text alignment as “p”. The column color is set using HTML format as AAACED.

Showing Tables in Landscape

In some cases you may need to rotate a table to help it fit onto a single page. This is helpful if the table contains too many columns. It is possible to rotate LaTeX tables in many different ways.

One of the easiest methods is using the package rotating. You can load this package using the command \usepackage{rotating} in the preamble. You will also need to replace the table environment with a sidewaystable environment provided by the rotating package.

We demonstrate this using the following code:

\begin{sidewaystable}[h!]
  \begin{center}
  \caption{Rotated table.}
  \label{tab:table1}
  \begin{tabular}{l|S|r}
      \toprule
      \textbf{Column1} & \textbf{Column2} & \textbf{Column3}\\
      11 & 22 & 33 \\
    \midrule
    1 & 58 & a\\
    2 & 62 & b\\
    3 & 64 & c\\
    \bottomrule
  \end{tabular}
  \end{center}
\end{sidewaystable}

Final Thoughts

As you can see, LaTeX allows you to insert tables relatively easily. You have plenty of options for customizing your tables. so try experimenting with the different commands discussed above and start producing professional-looking tables in your next LaTeX document. You can also learn how to create more complex LaTeX tables by watching the video below.

Frequently Asked Questions

Some frequently asked questions related to LaTeX tables are shown below.

Q1. What is a LaTeX table?

A LaTeX table is simply a table in LaTeX. These tables can have multiple rows and columns that can be customized to your liking.

Q2. How do I make a table in LaTeX?

You can make a simple table in LaTeX using the commands \begin{tabular}{….} and \end{tabular}, and by specifying your table text/values in between them.

Q3. How do I make multiple lines in a table in LaTeX?

You can create horizontal lines in LaTeX tables using the command \hline. You can create vertical lines using \begin{tabular}{||c c c c||}, in which “|” denotes a vertical line and its position relative to the cell text.

Q4. What is the difference between table and tabular in LaTeX?

Table and tabular are two important environments in LaTeX tables. The tabular environment allows you to typeset content in your rows and columns. The table environment functions as the container for your floating material that contains the tabular environment.

Leave a Comment