There are three ways you can insert tables in Overleaf:
If you’re new to LaTeX, using the toolbar in Visual Editor (option 1) is a great way to get started. And you can switch between Visual Editor and Code Editor to see the code behind the table. This page focuses on option 3.
We can start with one of the simplest examples of a table:
\begincenter> \begintabular> c c c > cell1 & cell2 & cell3 \\ cell4 & cell5 & cell6 \\ cell7 & cell8 & cell9 \endtabular> \endcenter>
The tabular environment is the default L a T e X method to create tables. You must specify a parameter to this environment; here we use which tells LaTeX there are three columns and the text inside each one of them must be centred.
The tabular environment provides additional flexibility; for example, you can put separator lines in between each column:
\begincenter> \begintabular> |c|c|c| > \hline cell1 & cell2 & cell3 \\ cell4 & cell5 & cell6 \\ cell7 & cell8 & cell9 \\ \hline \endtabular> \endcenter>
Here is a description of the constructions used in the example above:
< |c|c|c| >This declares that three columns, separated by a vertical line, are going to be used in the table. Each c means that the contents of the column will be centred. You can also use r to align the text to the right and l for left alignment. \hline This will insert a horizontal line on top of the table and at the bottom too. There is no restriction on the number of times you can use \hline . cell1 & cell2 & cell3 \\ Each & is a cell separator and the double-backslash \\ sets the end of this row.
Below you can see a second example which uses various vertical and horizontal lines (often called "rules"):
\begincenter> \begintabular>||c c c c||> \hline Col1 & Col2 & Col2 & Col3 \\ [0.5ex] \hline\hline 1 & 6 & 87837 & 787 \\ \hline 2 & 7 & 78 & 5415 \\ \hline 3 & 545 & 778 & 7507 \\ \hline 4 & 545 & 18744 & 7560 \\ \hline 5 & 88 & 788 & 6344 \\ [1ex] \hline \endtabular> \endcenter>
When formatting a table you might require a fixed width, either for each column or for the entire table. The following example adds the array package to document preamble:
\usepackagearray>
and uses it to set fixed column widths:
\documentclassarticle> \usepackagearray> \begindocument> \begincenter> \begintabular> | m5em> | m1cm>| m1cm> | > \hline cell1 dummy text dummy text dummy text& cell2 & cell3 \\ \hline cell1 dummy text dummy text dummy text & cell5 & cell6 \\ \hline cell7 & cell8 & cell9 \\ \hline \endtabular> \endcenter> \enddocument>
In the tabular environment, the parameter m sets a width of 5em for the first column ( 1cm for the other two) and centres the text in the middle of the cell. The aligning options are m for middle, p for top and b for bottom. When using these parameters the text is automatically formatted to fit inside each cell.
If you don't need to control the width of each cell, but of the entire table and then evenly distribute the space within, use the tabularx package. See the example below:
\documentclassarticle> \usepackagetabularx> \begindocument> \begintabularx>0.8\textwidth> | >\raggedright\arraybackslash>X | >\centering\arraybackslash>X | >\raggedleft\arraybackslash>X | > \hline item 11 & item 12 & item 13 \\ \hline item 21 & item 22 & item 23 \\ \hline \endtabularx> \enddocument>
The environment tabularx is similar to tabular but more flexible. To use it, add the line \usepackage to the document preamble. Notice that the environment opening statement is different, in the example the table width is set to 0.8\textwidth , which is 80% of the document's text width. You can use any of the L a T e X units to set that value.
The prefix inside braces | >X | >X | >X | sets the alignment of each column: the first one to left, the second one to center and the third one to right.
Rows and columns can be merged to create larger table cells. The following example uses the \multicolumn command to merge several columns:
\documentclassarticle> \usepackagemultirow> \begindocument> \begintabular> |p3cm>||p3cm>|p3cm>|p3cm>| > \hline \multicolumn4>|c|>Country List> \\ \hline Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 Code&ISO numeric Code\\ \hline Afghanistan & AF &AFG& 004\\ Aland Islands& AX & ALA &248\\ Albania &AL & ALB& 008\\ Algeria &DZ & DZA& 012\\ American Samoa& AS & ASM&016\\ Andorra& AD & AND &020\\ Angola& AO & AGO&024\\ \hline \endtabular> \enddocument>
Let's review each component of the command \multicolumn<|c|> \\ :
The number of columns to be combined: 4 in this case. <|c|>Delimiters and alignment of the resulting cell: in this case the text will be centred and a vertical line will be drawn at each side of the cell. Text to be displayed inside the cell.
To combine rows you need to add the multirow package to your document preamble:
\usepackagemultirow>
You can then use the \multirow command in your document:
\documentclassarticle> \usepackagemultirow> \begindocument> \begincenter> \begintabular> |c|c|c| > \hline col1 & col2 & col3 \\ \hline \multirow3>4em>Multiple row> & cell2 & cell3 \\ & cell5 & cell6 \\ & cell8 & cell9 \\ \hline \endtabular> \endcenter> \enddocument>
The multirow command takes three parameters; our example uses the following settings:
If you have to insert a very long table, which takes up two or more pages in your document, use the longtable package. First, add the following line to your document preamble:
\usepackagelongtable>
You can then use the longtable environment, as shown in the following example:
\documentclassarticle> \usepackagelongtable> \begindocument> \beginlongtable>[c]| c | c |> \captionLong table caption.\labellong>>\\ \hline \multicolumn2>| c |>Begin of Table>\\ \hline Something & something else\\ \hline \endfirsthead \hline \multicolumn2>|c|>Continuation of Table \reflong>>\\ \hline Something & something else\\ \hline \endhead \hline \endfoot \hline \multicolumn2>| c |>End of Table>\\ \hline\hline \endlastfoot Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ Lots of lines & like this\\ . Lots of lines & like this\\ \endlongtable>
The behaviour of longtable is similar to the default tabular , but generates tables that can be broken by the standard L a T e X page-breaking algorithm. There are four longtable -specific elements:
\endfirsthead Everything above this command will appear at the beginning of the table, in the first page. \endhead Whatever you put before this command and below \endfirsthead will be displayed at the top of the table in every page except the first one. \endfoot Similar to \endhead , what you put after \endhead and before this command will appear at the bottom of the table in every page except the last one. \endlastfoot Similar to \endfirsthead . The elements after \endfoot and before this command will be displayed at the bottom of the table but only in the last page where the table appears.
Positioning a table is easy if they're inside a float table environment.
\documentclassarticle> \begindocument> Below is a table positioned exactly here: \begintable>[h!] \centering \begintabular>||c c c c||> \hline Col1 & Col2 & Col2 & Col3 \\ [0.5ex] \hline\hline 1 & 6 & 87837 & 787 \\ 2 & 7 & 78 & 5415 \\ 3 & 545 & 778 & 7507 \\ 4 & 545 & 18744 & 7560 \\ 5 & 88 & 788 & 6344 \\ [1ex] \hline \endtabular> \endtable> \enddocument>
The parameter h! passed to the table environment declaration establishes that this table must be placed here, and override L a T e X defaults. The positioning parameters that can be passed-in include:
h Will place the table here approximately. t Position the table at the top of the page. b Position the table at the bottom of the page. p Put the table in a special page, for tables only. ! Override internal L a T e X parameters. H Place the table at this precise location, pretty much like h!.
For further examples on table positioning see the Positioning images and tables article.
In this example there are a few more commands:
\centering Centres the table relative to the float container element. \[1ex] This adds extra space to the cell.
Tables can be captioned, labelled and referenced by means of the table environment.
\documentclassarticle> \begindocument> Table \reftable:1> is an example of a referenced \LaTeX<> element. \begintable>[h!] \centering \begintabular>||c c c c||> \hline Col1 & Col2 & Col2 & Col3 \\ [0.5ex] \hline\hline 1 & 6 & 87837 & 787 \\ 2 & 7 & 78 & 5415 \\ 3 & 545 & 778 & 7507 \\ 4 & 545 & 18744 & 7560 \\ 5 & 88 & 788 & 6344 \\ [1ex] \hline \endtabular> \captionTable to test captions and labels.> \labeltable:1> \endtable> \enddocument>
There are three important commands in the example:
\caption As you may expect this command sets the caption for the table. If you create a list of tables this caption will be used there. You can place it above or below the table. \label If you need to reference the table within your document, set a label with this command. The label will number the table and, when combined with the \ref command, will allow you to reference it. \ref This code will be substituted by the number corresponding to the referenced table.
To create a list of tables use the \listoftables command. The caption of each table will be used to generate this list. For languages supported by the babel package, the title "List of tables" will be translated accordingly. See the article about International language support for more info.
\documentclassarticle> \begindocument> \listoftables \vspace5pt> The table \reftable:1> is an example of referenced \LaTeX<> elements. \begintable>[h!] \centering \begintabular>||c c c c||> \hline Col1 & Col2 & Col2 & Col3 \\ [0.5ex] \hline\hline 1 & 6 & 87837 & 787 \\ 2 & 7 & 78 & 5415 \\ 3 & 545 & 778 & 7507 \\ 4 & 545 & 18744 & 7560 \\ 5 & 88 & 788 & 6344 \\ [1ex] \hline \endtabular> \captionThis is the caption for the first table.> \labeltable:1> \endtable> \begintable>[h!] \centering \begintabular>||c c c c||> \hline Col1 & Col2 & Col2 & Col3 \\ [0.5ex] \hline\hline 4 & 545 & 18744 & 7560 \\ 5 & 88 & 788 & 6344 \\ [1ex] \hline \endtabular> \captionThis is the caption for the second table.> \labeltable:2> \endtable> \enddocument>
Several table elements can be modified to suit the needs of your document. Below you will learn how to modify the line thickness, the line colour and the background colour of the cells in your table.
The legibility of a table can sometimes be improved by incrementing the column spacing and row stretch.
\documentclassarticle> \setlength\arrayrulewidth>0.5mm> \setlength\tabcolsep>18pt> \renewcommand\arraystretch>1.5> \begindocument> \begintabular> |p3cm>|p3cm>|p3cm>| > \hline \multicolumn3>|c|>Country List> \\ \hline Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\ \hline Afghanistan & AF &AFG \\ Aland Islands & AX & ALA \\ Albania &AL & ALB \\ Algeria &DZ & DZA \\ American Samoa & AS & ASM \\ Andorra & AD & AND \\ Angola & AO & AGO \\ \hline \endtabular> \enddocument>
A description of the commands is provided below:
\setlength This sets the thickness of the borders of the table. In the example is 0.5mm but you can use other units—see the article Lengths in LaTeX for a complete list. \setlength The space between the text and the left/right border of its containing cell is set to 18pt with this command. Again, you may use other units if needed. \renewcommand The height of each row is set to 1.5 relative to its default height.
You can apply alternating colours to the rows of your table by using the xcolor package with the table option, as demonstrated in the following example:
\documentclassarticle> \usepackage[table]xcolor> \setlength\arrayrulewidth>0.5mm> \setlength\tabcolsep>18pt> \renewcommand\arraystretch>2.5> \begindocument> \rowcolors3>green!80!yellow!50>green!70!yellow!40> \begintabular> |p3cm>|p3cm>|p3cm>| > \hline \multicolumn3>|c|>Country List> \\ \hline Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\ \hline Afghanistan & AF &AFG \\ Aland Islands & AX & ALA \\ Albania &AL & ALB \\ Algeria &DZ & DZA \\ American Samoa & AS & ASM \\ Andorra & AD & AND \\ Angola & AO & AGO \\ \hline \endtabular>> \enddocument>
Notice the braces right before the command
\rowcolors3>green!80!yellow!50>green!70!yellow!40>
and after the tabular environment. The command \rowcolors takes three parameters each passed inside braces:
See the xcolor package documentation (at the further reading section) for a list of available colours and how to create your own. In the example the colours green and yellow are mixed in different proportions.
For the command to work make sure to add
\usepackage[table]xcolor>
to the preamble of your L a T e X file.
All elements in a table can be customized to use a specific colour. Again, this functionality is provided by xcolor so you must add
\usepackage[table]xcolor>
to the preamble. Below you can see an example.
\documentclassarticle> \usepackage[table]xcolor> \setlength\arrayrulewidth>1mm> \setlength\tabcolsep>18pt> \renewcommand\arraystretch>2.5> \newcolumntypes>>\columncolor[HTML]AAACED>> p3cm>> \arrayrulecolor[HTML]DB5800> \begindocument> \begintabular> |s|p3cm>|p3cm>| > \hline \rowcolorlightgray> \multicolumn3>|c|>Country List> \\ \hline Country Name or Area Name& ISO ALPHA 2 Code &ISO ALPHA 3 \\ \hline Afghanistan & AF &AFG \\ \rowcolorgray> Aland Islands & AX & ALA \\ Albania &AL & ALB \\ Algeria &DZ & DZA \\ American Samoa & AS & ASM \\ Andorra & AD & \cellcolor[HTML]AA0044> AND \\ Angola & AO & AGO \\ \hline \endtabular> \enddocument>
Below is a description about how to change the colour of each element in the table:
\newcolumntypes>>\columncolor[HTML]AAACED>> p3cm>>
A brief description of parameters in the tabular environment.
Tables can be created using tabular environment.
\begin[pos] table content \endwhere options can be:
t | the line at the top is aligned with the text baseline |
b | the line at the bottom is aligned with the text baseline |
c or none | the table is centred to the text baseline |
l | left-justified column |
c | centred column |
r | right-justified column |
p | paragraph column with text vertically aligned at the top |
m | paragraph column with text vertically aligned in the middle (requires array package) |
b | paragraph column with text vertically aligned at the bottom (requires array package) |
| | vertical line |
|| | double vertical line |
* | the format form is repeated num times; for example *<|l>| is equal to |l|l|l| |
To separate between cells and introducing new lines use the following commands:
& | column separator |
\\ | start new row (additional space may be specified after \\ using square brackets, such as \\[6pt] ) |
\hline | horizontal line between rows |
\newline | start a new line within a cell (in a paragraph column) |
\cline | partial horizontal line beginning in column i and ending in column j |
For more information see