Skip to content

AI Objectives

All AI Articles will be published here

How to Generate Table in LaTeX

Posted on January 11, 2022January 20, 2022 By ai-admin No Comments on How to Generate Table in LaTeX
  1. Creating Table in LaTeX
  2. Some Table Attributes
  3. Table Formatting
  4. Combining Multiple Tables
  5. Import Table from Excel

In this article we’re going to learn how to use the table and tabular environments to Generate Table in LaTeX. At first we’re going to create a simple table which will look like this:

Creating Table in LaTeX

Tables in LaTeX can be created through a combination of the table environment and the tabular environment. The table environment part contains the caption and defines the float for our table such as display the table in center.

The Tabular environment used ampersands & as column separators and newline symbols || as row separators.

Now let’s take a look at some actual code for a basic table:

\begin{table}[]
\centering
\caption{}
\label{tab:my-table}
\begin{tabular}{lll}
&  &  \\
&  &  \\
&  &
\end{tabular}
\end{table}

Output :

Some Table Attributes

Now, we are adding some data into the table rows and columns. The vertical lines separating the columns of our table (|) are passed as an argument to the tabular environment (e.g.\begin{tabular}{l|c|r} ) and the letters tell whether we want to align the content to the left (l), to the center (c) or to the right (r) for each column. There should be one letter for every column and a vertical line in between them or in front of them, if we want a vertical line to be shown in the table. Row separators can be added with the \hline command. We added the caption of table using ‘Caption’ command. Following code generated the given table next to it:

\documentclass{article}
\begin{document}
\begin{table}[h!]
  \begin{center}  
  \caption{Your first table.} 
   \label{tab:table1} 
   \begin{tabular}{l|c|r} % <– Alignments: 1st column left, 2nd middle and 3rd right, with vertical lines in between    
  \textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
   $\alpha$ & $\beta$ & $\gamma$ \\   
   \hline  
    1 & 1110.1 & a\\  
    2 & 10.1 & b\\ 
     3 & 23.113231 & c\\
    \end{tabular} 
\end{center}
\end{table}
 \end{document}

Table Formatting

A table may contain a caption, row groups, and column groups. A row group contains rows, while a column group contains columns. Rows and columns contain cells. In the following code we format the table by putting the bolded horizontal lines to clarify the table contents, add extra space between rows and follow the professional template for this table:

\usepackage{array}
\begin{table}[ht]
\centering
\caption{Fixed-width columns with “stretched” rows.}
\begin{tabular}[t]{l>{\raggedright}p{0.3\linewidth}>{\raggedright\arraybackslash}p{0.3\linewidth}}
\toprule &Treatment A&Treatment B\\
\renewcommand\arraystretch{1.5}
\midrule
John Smith&Good response, no side-effects&No response\\
Jane Doe&–&Good response, no side-effects\\
Mary Johnson&No response&Good response with side-effects\\ \bottomrule
\end{tabular}
\end{table}%

Combining Multiple Rows & Columns to Generate Table in LaTeX

Of course it’s also possible to combine the two features, to make a cell spanning multiple rows and columns. To do this, we simply use the multicolumn command and instead of specifying content, we add a multirow command as the content. We then have to add another multicolumn statement for as many rows as we’re combining. Following table shows the results of ‘Multirow’ and ‘Multicolumn’

\begin{table}[h!]
  \begin{center}
    \caption{Multirow and -column table.}
    \label{tab:table1}
    \begin{tabular}{l|S|r}
      \textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
      $\alpha$ & $\beta$ & $\gamma$ \\
      \hline
      \multicolumn{2}{c|}{\multirow{2}{*}{1234}} & a\\ % <– Multicolumn spanning 2 columns, content multirow spanning two rows
      \multicolumn{2}{c|}{} & b\\ % <– Multicolumn spanning 2 columns with empty content as placeholder
      \hline
      3 & 23.113231 & c\\
      4 & 25.113231 & d\\
    \end{tabular}
  \end{center}
\end{table}

Import Table from Excel

When writing papers, it is sometimes necessary to present a large amount of data in tables. Writing such tables in LaTeX by hand is a very time-consuming and error-prone task. To avoid this, we can simply import the data directly from .csv (comma-separated value) files. Programs such as Excel, OpenOffice Calc or even emacs org-mode can export data sheets as .csv files. LaTeX can not work with them directly, but we can use the following code, to generate tables from .csv files:

documentclass{article}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage{siunitx} % Formats the units and values
\usepackage{pgfplotstable} % Generates table from .csv
\sisetup{ round-mode = places, % Rounds numbers round-precision = 2, % to 2 places}
\begin{document}
\begin{table}[h!]
\begin{center}
\caption{Autogenerated table from .csv file.}
\label{table1}
\pgfplotstabletypeset[ multicolumn names, % allows to have multicolumn names
col sep=comma, % the seperator in our .csv file
display columns/0/.style={column name=$Value 1$,
% name of first column
column type={S},string type},  % use siunitx for formatting
display columns/1/.style={
column name=$ Value 2$, column type={S}, string type}, every head row/.style={before row={\toprule}, % have a rule at top
after row={\si{\ampere} & \si{\volt}\\ % the units seperated by &
\midrule} % rule under units},
 every last row/.style={after row=\bottomrule}, % rule at bottom
 ]{table.csv} % filename/path to file
\end{center}
\end{table}
\end{document}

Generate Table in LaTeX, LaTex Tags:Generate table in latex, LaTeX, Table Formatting

Post navigation

Previous Post: K-Means Clustering
Next Post: Importance and Functions of Kernel in Machine Learning

Related Posts

How to Generate Table in LaTeX? LaTex
How to do Typography in LaTex LaTex
How to Generate Table in LaTeX? Generate Table in LaTeX
How to do Typography in LaTex LaTex
How to Generate Table in LaTeX LaTex
How to Generate Table in LaTeX LaTex

Leave a Reply Cancel reply

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

Copyright © 2023 AI Objectives.

Powered by PressBook Grid Dark theme