Skip to content
Search
Generic filters
Exact matches only

How to Generate Table in LaTeX?

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

How to Generate Table in LaTeX by using table and tabular environments, this type of questions. We also learn to format table, entering data, merging in table and importing the table from MS-Excel. First we’ll create a basic table which contains no data.

Generate Table in LaTeX

In LaTeX, we are often create
tables through a mix of table setting and therefore the tabular environment.
The table settings part correspondence to the beginning of a table, caption of
a table, and also defines the float of the table such as display the table in
center, display the table text in center, left and right etc.

Tabular environment

The Tabular environment uses different command and operators to perform different actions on table and on text contains in the table. For example, newline symbols ||as row separators and ampersands & as column separators.

Now just look at the code given in the following table. This code is written in LaTex (Generate Table in LaTeX) for creating an empty table without containing any contents in the cells. begin{table}[] command initiate the start of table, centering align the table into center and caption is used for table heading as shown below:

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

Some Table Attributes

Now, we are adding some data into the table rows and columns. Vertical lines are used for separating the columns of a table. This vertical line (|) is passed as an associate argument for the tabular settings (e.g.begin{tabular}{l|c|r} ). Letters between these vertical lines tells us that where we align the content on the left part of the cell (l), to the center of the cell (c) or to the right of the cell (r) for all columns. Only one letter and a vertical line in front of them or in the middle of the letters for multiple columns should be used to show the vertical line in the table. If we want to separate Rows, Rows separators could be added with the hline command. As we discussed above, we add the caption of table using ‘Caption’ command and passing the caption of table as an argument.

 Following code generated the given table:

documentclass{article}
begin{document}
begin{table}
begin{center}
caption{Table for Example}
label{tab:Figure1}
 begin{tabular}{|l|c|r|} % here we make alignment of contents of the table columns      
hline text{Col-1} & text{Col-2} & text{Col-3}\ 
hline  
  ali & yellow & Pakistan\
  nadir & black & India\
yasir & white & America\      
Amir  & blue  & Sri-Lanka\
hline
    end{tabular}
  end{center}
end{table}
end{document}

Table Formatting:

table may contain a caption, number of rows, and a number of columns. Rows are exists in row group and columns are exists in column group. The intersection of Rows and columns are called cells. Data is entered in the cells of a table. 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.3linewidth}>{raggedrightarraybackslash}p{0.3linewidth}}
toprule
&Treatment A&Treatment B\
renewcommandarraystretch{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:

Of course it is also attainable to mix the 2 options, to make a cell spanning multiple rows and multiple columns. Multicolumn command used to merge the columns with one another not the content of columns, as the content we use a multirow command to merge multiple rows. (Generate Table in LaTeX). After that another multicolumn command also added to specify the number of rows which we want to use to merge. Following table shows the results of ‘Multirow’ and ‘Multicolumn’:

documentclass{article}
begin{document}
begin{table}
  begin{center}    
caption{Results of Multirow, Multicolumn}    
label{tab:table1}  
   begin{tabular}{l|c|r}      
textbf{Num-1} & textbf{Num-2} & textbf{Num-3}\ hline       multicolumn{2}{c|}{multirow{5}{+}{6787}} & w\     
  multicolumn{2}{c|}{} & x\ hline     
  5 & 67.345623 & y\      
6 & 56.234312 & z\   
  end{tabular}
  end{center}
end{table}
end{document}

Import Table from Excel

When writing research papers or documents, sometimes we need to represent a lot amount of data in the form of tables. If we write this data into table by hand in LaTeX by hand then it will take a lot of time and may also prompt to some kind of errors. To prevent such errors and to save time, .csv (comma separated value) files imported in LaTeX directly without any difficulty. Different programs such as openOffice-Calc, emacs org-mode or Excel can export datasheets in the form of .csv files.

documentclass{article}
usepackage{booktabs}
usepackage{siunitx}
usepackage{pgfplotstable} % package which uses .csv to create table
sisetup{ round-mode = places, round-precision = 2,}
begin{document}
begin{table}[h!]
begin{center}
caption{List of Numbers}
label{table1}
 pgfplotstabletypeset[ multicolumn names, % it allows for names of multicolumns     
  col sep=comma, % giving column separator
 display columns/0/.style={ column name=$Num-10$,
% gave the name of first column                               
  column type={Int},string type},
display columns/1/.style={ column name=$Num 12$,
%giving the name of second column                                  column type={Int},string type},      
every head row/.style={ before row={ toprule},
% define rule for top                                
after row={si{} & si{}\  midrule} % define rule for mid },  
every last row/.style={after row=bottomrule}, % define rule for bottom ]{names.csv} % give a complete path to the file
  end{center}
end{table}
end{document}
  • CSV files not used directly.
  • In the LaTeX but to use the data those CSV files contains we can write up the above given code which will import the data from .csv file named ‘names.csv’ easily without taking a lot of time and any error. 
  • And it will generate the table which will look like this:

Working with Tables in LaTeX

Finally, in this blog we learn that how can we work with tables in LaTeX. First we create a simple table in the LaTeX and then we enter some data into it. After that we apply some attributes of table like borders, aligning contents, bold etc. Sometimes we need to merge the two or multiple columns or multiple rows. We also cover this topic in this blog.

It is time consuming and error prone task.

Lastly as we know that sometimes we represent a lot of data in the form of tables, but we cannot directly write it into LaTeX. As it is time consuming and error prone task. The solution of this problem is to just write the data in other programs like Excel and open-Office etc. Then just export their datasheets as .csv files, then you can easily import those .csv files into LaTeX. These are some operation which we can done in LaTeX on tables, a lot of others can also be done in LaTeX. We also write a blog that tells that how to make CV using LaTeX.

Click on the link to see How to do Typography in LaTex?

Click on the link to see How to write notation in LaTex?

error: Content is protected !!