I was asked to give an introduction to tables in LaTeX. Even though there are plenty of tutorials and pieces of LaTeX-code out there in the internet, it might still be useful to somebody who just started using Latex or has never used tables before…
Table
Let me start with the table-stub and later on explain how to fill in the actual table content. Most of the Latex editors provide a macro, which is usually helpful as it directly inserts the basic stub usually including a few rows and columns of the table. The macro provided by LED is the following code:
\begin{table}[t] \centering \begin{tabular}{} %table content \end{tabular} \caption{} \label{tab:label} \end{table}
There is an optional argument which determines where LaTeX places the table. Available placements are:
h (here), t (top), b (bottom) and p (page of floats).
Caption and label are the same commands used when including figures. Caption is the explanation displayed below or above the table, depending on where you insert it, before or after the tabular. The label is used for reference in the text. It is a Latex convention to start table references by “tab:”, which no only lets you use the same name for a figure (starting with fig:) and a table, but makes it easier to distinguish between figures and tables. Use the command \ref{tab:} to reference a table in the text.
Tabular
The tabular environment produces a box consisting of a sequence of rows aligned vertically in columns. The complete tabular environment takes 3 arguments:
\begin{tabular}{width}[pos]{cols}
The first argument fixes the width of the whole tabular environment.Filling the content is not difficult, once you got the idea. Columns are separated by “&” and rows by “\\”.
Example 1:
entry 1 & entry 2 & entry 3 & entry4 \\
You have to tell Latex in the beginning how many columns you will be using. This is done by adding l’s (align left), c’s (align center) and r’s (align right) in addition to |’s (vertical bar for cell separation) as the argument of the command tabular.
Example 2:
In order to generate a table with four horizontal cells as in example 1, one would use
\begin{tabular}{|c|c|c|c|}
c can be substituted by l and r.
It is also possible to use different alignments for different cells. Just make sure that are as many c’s, l’s and r’s as there are column entries, otherwise LaTeX will complain. For leaving a cell empty, just use “& &”. The number of &’s has to be equal to the number of c’s/r’s/l’s -1.
There are a certain number of other options available, which I will not discuss here, as they are quite specific. You can find the complete command reference for tabular here.
In order to horizontally separate the cells, just use \hline
, which inserts a line as wide as the table. The size of the table is determined automatically, depending on the number of cells as well as the content.
Example 3:
Here is the code for a complete table, which can be used as a template for your tables, just copy-paste the code into your tex-file:
\begin{table}[t] \centering \begin{tabular}{|l|c|c|c|c|c|} \hline Product & 1 & 2 & 3 & 4 & 5\\ \hline Price & 124.- & 136.- & 85.- & 156.- & 23.-\\ Guarantee [years] & 1 & 2 & - & 3 & 1\\ Rating & 89\% & 84\% & 51\% & & 45\%\\ \hline \hline Recommended & yes & yes & no & no & no\\ \hline \end{tabular} \caption{This is a table template} \label{tab:template} \end{table}
Table \ref{tab:template}
gives an example of how to write a table in Latex.Generating the PDF should produce thefollowing table.
Problem with label for tables having small text-size « Blog on Latex Matters
[…] and MacTex… A table ususally has the following structure (for an introduction to tables click here) with the “small”- environment added to slightly decrease its size: begin{table}[htdp] […]
Floating text around figure/table « Blog on Latex Matters
[…] font size. E.g. begin{footnotesize} begin{floatingtable} … end{footnotsize} Check this post for an introduction to […]
Hi,
I am trying to change the “title” of a table in Latex. You know that first part of the tile: “Table 1: —–“. I want this “Table” to disappear or to replace it with something else. Is there any way that I can do that?
Thanks,
Lavinia
Hi Lavinia,
Try the caption package:
and within your document use:
for “Tab.” instead of “Table” and similarly for figures:
Source: Caption documentaton
Cheers,
Tom
Reblogged this on russjohnson09.
I am using LaTeX to make some reports, one problem I am having is with Tables, the headings to the tables are quite long. Right now I use the tabularx package, when I compile I get underfull hbox badness 10000, I know it will still work but I want LaTeX to compile with no warnings. A minimal example is given below.
Hi!
Thanks for your question and the minimal working example. As you already pointed out, the problem lies with the very long names of the column headings. I suggest you try the microtype package. Loading that packages possibly resolves most of the warnings. Otherwise, you can enforce how LaTeX splits long names using:
Hope it helps,
Tom.
Hi Tom,
Thanks, it fixed it up, however I have decided to make the shift to the booktabs table, it looks much more professional. However another problem with the long names in booktabs is Overfull hbox badness 10000, sometimes I get this when placing figures side by side, any assistance?
Hey!
These warnings are relatively easy to fix. Use the documentclass option
draft
to highlight any “overfull hbox”. I suspect you are using0.5\linewidth
or0.5\textwidth
for two figures side-by-side. Just slightly reduce the size to e.g.0.47\textwidth
for both figures, since LaTeX add some space in between.Please provide a minimal working example again in case this does not solve your problem.
Thanks, Tom.
Hello tom
please help me in my problem
I’m using lyx and I want to make word table bold and put caption below word table, also i want both word table and caption to be most left justified with left border of table , like this
Table1
This is table1 caption
what will be code to modify or steps in lyx
Hi mero,
I’m sure you can do it in LyX. But since I never use LyX, here is how I would go about it.
Insert TeX code before and after your table:
Once that’s done, you’ll also need to align the caption which turns out a little more challenging, since you’ll need to load the caption package. Navigate to “Document –> Settings” and choose “LaTeX preamble”. Then paste the following line in the textbox:
That did the trick for me. Hope it works for you!
Cheers, Tom.
Hi, I have a problem
Result1.tex:
I always get the error:
missplaced\noaligne.
\bottomrule-> noaligne
I can solve the problem by changing \input{result1.tex} to \input{result1.tex} \\
But then I have a empty line before the bottomrule and thats not what I want.
Anybody an idea?
Thanks in advance
Hi Markus,
I recommend to place the entire table, not only the table content, into the file and try again. It works for me. Also, next time, please provide a minimal working example. I first had to google before I was able to run your code. It only works when using quite a substantial amount of additional code.
Cheers, Tom.
Thanks for your help Tom. It was the first time that I posted a problem in a forum like this one. Thats why I had no idea how I should really do it. Next time I will do it as you recommend. Thanks again. The problem is that the file result1.tex is provided from Stata.It’s important for me that it stays like this. But thats exactly the point. Isn’t it strange that it works when one puts everything in the file. I mean what else does the the input command do as just putting the code at it’s place. But that’s not working.
Hi Markus,
Thanks for following up on your question. Not sure, why you get the error. Here is what works for me with your Stata output (result1.tex):
I copied most of the code from this blog.
Your code (commented with %) also works. Give it a try!
Hope it helps. Tom.
Hello Tom,
I have a query. I want to make a table which is very long in terms of column. So I divided the whole thing into 2 parts. Now these are titled as Table 1 and Table 2. But how can I make them Table 1.(A) and Table 1.(B)…. Is there any way?
Thanks in advance
Hi Arindam,
The longtable package does what you are looking for. You can find an example here.
Best, Tom.
It’s so kind of you, I think I have got my answer……:) So quickly….
Great! And good luck with the table… Tom.
hi tom
In my pdf, why it is showing Tab. in place of Table.?
please answer.
himanshu
Hi Himanshu,
This might be due to the document class or package you’re useing. Try adding this line to your preamble:
Hi Tom
I am sending my preamble. Please check and advise.
With your suggestion also, it is not showing TABLE.
Please do me a favour. I am about to submit my Ph.D. thesis.
PREAMBLE:
** code removed by Tom **
Hi Himanshu,
Thanks for sending your preamble. It’s the thesis document class that defines
\tableshortname
to be “Tab.” and\figureshortname
to be “Fig.” (on line 1092/3).So, simply redefine these in your preamble, e.g.:
Hi Tom
thanks a lot for your sweet solution. it worked.
Actually, I am a learner of LaTeX. I will need your help at times, if permitted….
Regards
himanshu
Sure, no problem! Feel free to drop me a comment, should you have other questions. However, please find an relevant article using the search field on the right. Also, I can help more efficiently when you provide a minimal example :).
Tom
Hi, does anyone now how to write words ‘figure’ and ‘table’ which looks like in amsart?
I think that all letters are capital letters, but only the first is large and the other are small.
They are using small capitals, provided by the
\sc
command.Here’s a minimal working example where the list headings look similar to what amsart produces.
Hope this helps,
Tom
Thanks!
I found that I can use
\textsc{•}
command, but example was helpful.Great, thanks for your reply.
Tom
I use a spreadtab package in tables. I try to enter a text column in it on which no calculation done, but it doesn’t work. Please any help!
Hi there,
Thanks for this question. You’ll have to use ‘@’ or ‘\STtextcell’ for text cells. Next time, please consider the package documentation before asking a question and/or provide a minimal example (see code below).
Best, Tom
Hi Tom i got this table… But… When I am going to use
\ref{tab:1}
the result on my page says: Table II-A and I got no another table, I got a Figure but it seems that is recognizing it as same type of label.Hi Santiago,
Thanks for your question. I’m not sure what the problem is, since you didn’t include the rest of the code. Your table looks fine. Perhaps
II
is the chapter/section andA
is the first table. You can redefine the way the table counter is printed if necessary.HTH, Tom