Getting to Grips with Latex - Tables - Latex Tutorials by Andrew Roberts @ School of Computing, University of Leeds

来源:百度文库 编辑:神马文学网 时间:2024/04/29 10:18:45

Getting to Grips with Latex - Tables

by Andrew Roberts67diggsdigg

In academic writing, tables are a common feature, often forsummarising results from research. It is therefore a skill thatneeds mastering in order to produce good quality papers.

However, if there is one area about Latex that I feel is theleast intuitive, then I am afraid that this is it. Basic tablesare not too taxing, but you will quickly notice that anythingmore advanced can take a fair bit of construction. So, we willstart slowly and build up from there.

The Tabular environment

To begin, we shall first get familiar with some Latexterminology. An environment in Latex is a specialdeclaration for formatting specific types of text. For example,in tutorial3 theabstract was introduced, so that the abstract of thepaper could be formatted differently that the main body of text.All environments begin and end in the same fashion:

\begin{environment-name}......\end{environment-name}

The tabular is another such environment, designed forformatting your data into nicely arranged tables. Arguments arerequired after the environment declaration to describe thealignment of each column. The number of columns does not need tobe specified as it is inferred by looking at the number ofarguments provided. It is also possible to add vertical linesbetween the columns here. The following symbols are availableto describe the table columns:

l left-justified column c centered column r right-justified column p{width} paragraph column with text vertically aligned at the top m{width} paragraph column with text vertically aligned in the middle b{width} paragraph column with text vertically aligned at the bottom | vertical line || double vertical line

Once in the environment,

& column separator \\ start new row \hline horizontal line

Note, any white space inserted between these commands ispurely down to ones' preferences. I personally add spacesbetween to make it easier to read.

Very basic table

This example shows how to create a simple table in Latex. It isa three-by-three table, but without any lines.

\begin{tabular}{ l c r }            1 & 2 & 3 \            4 & 5 & 6 \            7 & 8 & 9 \            \end{tabular}

Expanding upon that by including some vertical lines:

\begin{tabular}{ l | c || r | }            1 & 2 & 3 \            4 & 5 & 6 \            7 & 8 & 9 \            \end{tabular}

To add horizontal lines to the very top and bottom edges of thetable:

\begin{tabular}{ l | c || r | }            \hline            1 & 2 & 3 \            4 & 5 & 6 \            7 & 8 & 9 \            \hline            \end{tabular}

And finally, to add lines between all rows, as well as centring(notice the use of the center environment - of course, the result ofthis is not obvious from the preview on this web page, so look at thepdfoutput):

\begin{center}            \begin{tabular}{ l | c || r | }            \hline            1 & 2 & 3 \\ \hline            4 & 5 & 6 \\ \hline            7 & 8 & 9 \            \hline            \end{tabular}            \end{center}

Text wrapping in tables

Latex's algorithms for formatting tables have a fewshortcomings. One is that it will not automatically wrap text incells, even if it has overrun the width of the page. Forcolumns that you know will contain a certain amount of text,then it is recommended that you use the p attribute andspecifythe desired width of the column (although it may take sometrail-and-error to get the result you want).

Before we can proceed, we must introduce the Latex system ofusing measurements. This is quite flexible, as you can choosefrom a variety of length units.

  • pt - a point is 1/72 inch.
  • mm - millimetre.
  • cm - centimetre.
  • in - inch.
  • ex - roughly the height of an 'x' in the current font.
  • em - roughly the width of an 'M' (note the uppercase) of the current font.

There are also things known as command lengths, whichare not fixed values as they depend on the configuration of thecurrent document class and/or preamble. Useful ones include:

  • \parindent - the size of the paragraph indent
  • \baselineskip - vertical distance between lines.
  • \parskip - the extra space between paragraphs.
  • \textwidth - the width of a line of text in the local environment (e.g., the lines are commonly narrower in the abstract than in the normal text).
  • \textheight - the height of the text on the page.

The examples prepared are quite long because I was illustratingwhat happens when there is a fair bit of text in table cells.So instead of reproducing it within the page, go directly to theexample Latex file, wrapped.texand then view the output.

The Tabular* environment - controlling table width

This is basically a slight extension on the original tabularversion, although it requires an extra argument (before the columndescriptions)to specify the preferred width of the table.

\begin{tabular*}{0.75\textwidth}{ | c | c | c | r | }\hlinelabel 1 & label 2 & label 3 & label 4 \\hlineitem 1  & item 2  & item 3  & item 4  \\hline\end{tabular*}

However, that doesn't look quite as intended. The columns are still attheir natural width (just wide enough to fit their contents whilst therows are as wide as the table width specified. This looks very ugly.The reason for the mess is that you must also explicitly insert extracolumn space. Fortunately, Latex has rubber lengths,which unlike others, are not fixed, and Latex can dynamicallydecide how long they should be. So, the solution the currentproblem is:

\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill}} | c | c | c | r | }\hlinelabel 1 & label 2 & label 3 & label 4 \\hlineitem 1  & item 2  & item 3  & item 4  \\hline\end{tabular*}

You will notice the @{...} construct added at the beginning ofthe column description. More details of this will be comingshortly. Within it is the \extracolsepcommand, which requiresa width. A fixed width could have been used, however, by usinga rubber length, such as \fill, thecolumns are automatically spaced evenly.

@-expressions

It is probably worth addressing the @ specifier now that it hasbeen introduced, even though there aren't very many obviousapplications for it.

It typically takes some text as its argument, and when appendedto a column, it will automatically insert that text into eachcell in that column before the actual data for that cell. It isworth noting that once used, inter-column space between theaffected columns is suppressed. To add space, use@{\hspace{width}}.

Admittedly, this is not that clear, and so will require a fewexamples to clarify. Sometimes, it is desirable in scientifictables to have the numbers aligned on the decimal point. Thiscan be achieved by doing the following:

\begin{tabular}{r@{.}l}            3&14159\            16&2\            123&456\            \end{tabular}

Its space suppressing qualities actually make it quite usefulfor manipulating the horizontal spacing between columns. Givena basic table, and varying the column descriptions:

\begin{tabular}{|l|l|}\hlinestuff & stuff \\ \hlinestuff & stuff \\hline\end{tabular}
{|l|l|} {|@{}l|l@{}|} {|@{}l@{}|l@{}|} {|@{}l@{}|@{}l@{}|}

Spanning

To complete this tutorial, a quick at how to generate slightlymore complex tables. Unsurprisingly, the commands necessaryhave to be embedded within the table data itself.

Rows spanning multiple columns

The the command for this looks like this: \multicolumn{num_cols}{alignment}{contents}.num_cols is the numberof subsequent columns to merge; alignment is prettyobvious, either l, c, or r. And contents is simply theactual data you want to be contained within that cell. A simpleexample:

\begin{tabular}{|l|l|}            \hline            \multicolumn{2}{|c|}{Team sheet} \            \hline            GK & Paul Robinson \            LB & Lucus Radebe \            DC & Michael Duberry \            DC & Dominic Matteo \            RB & Didier Domi \            MC & David Batty \            MC & Eirik Bakke \            MC & Jody Morris \            FW & Jamie McMaster \            ST & Alan Smith \            ST & Mark Viduka \            \hline            \end{tabular}

Columns spanning multiple rows

The first thing you need to do is add \usepackage{multirow}to thepreamble. This then provides the command needed for spanning rows: \multirow{num_rows}{width}{contents}.The arguments arepretty simple to deduce. With the width parameter, youcan specify a fixed with if you wish, or, if you want thenatural width (i.e., just wide enough to fit the contents of thecolumn) then simply input an asterisk (*). This approach wasused for the following example:

\begin{tabular}{|l|l|l|}            \hline            \multicolumn{3}{|c|}{Team sheet} \            \hline            Goalkeeper & GK & Paul Robinson \\ \hline            \multirow{4}{*}{Defenders} & LB & Lucus Radebe \            & DC & Michael Duberry \            & DC & Dominic Matteo \            & RB & Didier Domi \\ \hline            \multirow{3}{*}{Midfielders} & MC & David Batty \            & MC & Eirik Bakke \            & MC & Jody Morris \\ \hline            Forward & FW & Jamie McMaster \\ \hline            \multirow{2}{*}{Strikers} & ST & Alan Smith \            & ST & Mark Viduka \            \hline            \end{tabular}            

The main thing to note when using \multirowisthat for the subsequent rows that are to be spanned, a blank entry forthe appropriate cells have to be inserted.

Summary

That's about it for basic tables in my opinion. After youexperiment, you do quickly get up to scratch. I must admit, thetable syntax in Latex can look rather messy, and so seeingnew examples can look confusing. But hopefully, enough has beencovered here so that you can create any table you are likely toneed for your papers. Unsurprisingly, Latex has plenty moreup its sleeve, so expect a follow up tutorial covering moreadvanced features in the near future.


Files:basic.tex|basic.pdf|wrapped.tex|wrapped.pdf|tabular_star.tex|tabular_star.pdf|specifier.tex|specifier.pdf|spanning.tex|spanning.pdf

67diggsdigg

Comments? Please leave feedback.

< Backto Latex tutorials.
<< Back tomisc.


Read more: Gettingto Grips with Latex - Tables - Latex Tutorials by Andrew Roberts @School of Computing, University of Leeds http://www.andy-roberts.net/misc/latex/latextutorial4.html#ixzz0mMDKaOOr