• 검색 결과가 없습니다.

Basic Tables

문서에서 The Not So Short Introduction to LATEX (페이지 71-77)

2.18 Tables

2.18.1 Basic Tables

column specifier, where 〈width〉 is the width of the column.

\begin{tabular}{lp{3cm}}

left & Very long paragraph that gets broken into multiple lines. \\

1 & Another one, but shorter. \\

\end{tabular}

left Very long para-graph that gets broken into multi-ple lines.

1 Another one, but shorter.

The optional 〈pos〉 argument specifies the vertical position of the table with respect to the baseline of the text. There are three possible alignments: c centres the table (the default), t matches the baseline of the top row, and b matches the baseline of the bottom row.

text\begin{tabular}{ll} 1 & 2 \\ 3 & 4 \\ \end{tabular}

text\begin{tabular}[t]{ll} 1 & 2 \\ 3 & 4 \\ \end{tabular}

text\begin{tabular}[b]{ll} 1 & 2 \\ 3 & 4 \\ \end{tabular}

text

text 1 23 4 text 1 23 4 text 1 2 3 4 text

What we have seen so far, allows aligning some items in rows and columns, but real tables need visible headings. To insert them, use the commands \toprule, \midrule and \bottomrule from the booktabs package. All of these accept an optional argument that specifies their thickness, but usually the default settings are just fine. SeeListing 2.2 for an example of using these commands.

With these commands you are already able to produce simple, yet nicely looking tables. You may be are wondering why we have not mentioned how to add vertical lines between the columns. Well, we did not mention them because the first rule of producing professional-looking tables is that you must not use vertical lines.

The second rule is to never use double lines such as these shown in Listing 2.3. The default lines are already of different weight in order to signify their meaning. Using more lines than is necessary means cluttering the space without adding information. If you stick to these two rules, your tables will look quite good already.

To make the tables look even more sleek, you may want to remove the padding in the first and last column. To control the space between

2.18 Tables 55

\begin{tabular}{lcl}

\toprule

Alignment & Letter & Niceness \\

\midrule

Left & l & Very nice \\

Centre & c & Very nice \\

Right & r & Very nice \\

\bottomrule

\end{tabular}

Alignment Letter Niceness

Left l Very nice

Centre c Very nice

Right r Very nice

Listing 2.2: A simple table using the booktabs commands.

\begin{tabular}{lll}

\toprule[0.1cm]

\toprule

Person & Face & Table \\

\midrule

\midrule

Me & :( & Not nice \\

You & :[ & Awful \\

Your reader & :< & Terrible \\

\bottomrule

\bottomrule[0.1cm]

\end{tabular}

Person Face Table

Me :( Not nice

You :[ Awful

Your reader :< Terrible

Listing 2.3: An anti-example of using double rules inside a table.

the columns use the @{〈sep〉} column specifier, where 〈sep〉 is either text or space. Space of arbitrary length can be inserted using the

\hspace{〈width〉}command. The contents of the 〈sep〉 arguments will be put between cells in the relevant column.

\begin{tabular}{

@{a} c @{\hspace{1cm}} c @{|} c @{ b}

} 1 & 2 & 3 \\

4 & 5 & 6\\

7 & 8 & 9\\

\end{tabular}

a1 2|3 b a4 5|6 b a7 8|9 b

If you leave the 〈sep〉 empty it will suppress the padding between the columns. SeeListing 2.4for an example.

Sometimes it may make sense to group columns under one heading.

To achieve this, we need two things: a way to group multiple heading columns, and to have a line covering the same columns (sitting underneath the merged heading cell). Use the

\multicolumn{〈ncols〉}{〈colspec〉}{〈text〉}

command to merge the cells. The 〈ncols〉 argument indicates how many columns should the 〈text〉 span, while the 〈colspec〉 is the column spec-ification for the new content—the same as when starting a tabular environment. SeeListing 2.5for an example.

To get a horizontal line to span multiple columns, use the command

\cmidrule[〈dim〉](〈trim〉){〈a-b〉}

While the command uses nonstandard syntax for 〈trim〉, this is just an optional argument with different pair of delimiters.9 The 〈dim〉 allows us to specify the thickness of the line. The 〈trim〉 argument accepts any combination of r, r{〈dim〉}, l or l{〈dim〉}. This allows you trim the rule from right or left, either by the package default or the specified 〈dim〉. It is usually recommended to trim the rules from the side where they touch other columns. The command’s only required argument is the span of columns to draw the line over (〈a-b〉). SeeListing 2.6 for an example.

If you need a cell to span multiple rows instead of columns, then you have to use package multirow. Its main command is a little more complicated

\multirow[〈vpos〉]{〈nrows〉}{〈width〉}[〈vmove〉]{〈text〉}

The arguments are

9This syntax allows specification of the second optional argument without the first, which would be impossible if square brackets were used for both.

2.18 Tables 57

\begin{tabular}{@{}lll@{}}

\toprule

Person & Face & Table \\

\midrule

Me & :) & Nice \\

You & :] & Sleek \\

Your reader & :> & Informative \\

\bottomrule

\end{tabular}

Person Face Table

Me :) Nice

You :] Sleek

Your reader :> Informative

Listing 2.4: An example of using @{} column specifier to suppress outer separators.

\begin{tabular}{@{}lll@{}}

\toprule

Person & Face & Table \\

\midrule

Me & :) & Nice \\

You & :] & Sleek \\

Your reader & \multicolumn{2}{c}{Not available} \\

\bottomrule

\end{tabular}

Person Face Table

Me :) Nice

You :] Sleek

Your reader Not available

Listing 2.5: An example of using the\multicolumncommand in a table.

\begin{tabular}{@{}lll@{}}

\toprule

& \multicolumn{2}{c}{Reaction} \\

\cmidrule(l){2-3}

Person & Face & Exclamation \\

\midrule

Me & :) & Nice \\

You & :] & Sleek \\

Your reader & \multicolumn{2}{c}{Not available} \\

\bottomrule

\end{tabular}

Reaction Person Face Exclamation

Me :) Nice

You :] Sleek

Your reader Not available

Listing 2.6: An example of using the\cmidrule command inside a table.

〈vpos〉 is the vertical position of the text within the cell. Can be either c for centre (the default), t for top or b for bottom.

〈nrows〉 is the number of rows for the new cell to span.

〈width〉 is the width of the cell. Apart from a regular length, you can also pass two special arguments here: * for the natural length of the text and = for the same width as the column (this only makes sense if the column width was specified, for example via p{3cm}).

〈vmove〉 allows adjusting the position of the text if it sits too low or too high.

〈text〉 is the text to be put in the cell.

Unlike with\multicolumnyou still have to write all the cells in remaining rows, but they should be empty. SeeListing 2.7for an example.

In general it is not a good idea to use the \multirow command for showing values common to multiple rows. Usually repeating the values in question makes the table more readable.

When typesetting numerical data in the table, you may want to align it by decimal point. A way to do this (and more) is described in subsection 3.5.4. The section also contains some guidelines about typesetting numerical data in tables.

2.18 Tables 59

\begin{tabular}{@{}lll@{}}

\toprule

& \multicolumn{2}{c}{Reaction} \\

\cmidrule(l){2-3}

Person & Face & Exclamation \\

\midrule

\multirow[t]{2}{*}{VIPs} & :) & Nice \\

& :] & Sleek \\

Others & \multicolumn{2}{c}{Not available} \\

\bottomrule

\end{tabular}

Reaction Person Face Exclamation

VIPs :) Nice

:] Sleek Others Not available

Listing 2.7: An example of using\multirow inside a table.

문서에서 The Not So Short Introduction to LATEX (페이지 71-77)