Tabular environments

6.6 Tabular environments

A tabular environment creates a table that L A TEX treats as a “large symbol”. In par- ticular, a table cannot be broken across pages.

1 2 3 Here is a simple table, Peter

Name

2.45 34.12 1.00 , typeset inline. This

John

0.00 12.89 3.71 David 2.00 1.85 0.71

looks awful, but it does make the point that the table is just a “large symbol”. The table is typed as

\begin{tabular}{ | l | r | r | r | } \hline Name

\end{tabular} with no blank line before or after the environment.

134 Chapter 6 Text environments

This table can be horizontally centered with a center environment (see Sec- tion 6.3). It can also be placed within a table environment (see Section 10.4.3). This sets the table off from the surrounding text with vertical space and you can also use the float controls b, t, h, p to specify where the table should appear (see Sec- tion 10.4.3). This also allows you to define a caption, which can be placed before or after the table:

\begin{table} \begin{center} \begin{tabular}{ | l | r | r | r | } \hline Name

\\ \hline Peter

2.45 & 34.12 & 1.00\\ \hline John

0.00 & 12.89 & 3.71\\ \hline David

2.00 & 1.85 & 0.71\\ \hline \end{tabular} \caption{Tabular table.}\label{Ta:first}

\end{center} \end{table}

This table is displayed as Table 6.1.

Table 6.1: Tabular table.

It can be listed in a list of tables (see Section 10.4.3) and the table number may be referenced using the command \ref{Ta:first}. Note that the label must be typed between the caption and the \end{table} command.

For another example, look at the two tables in the fonttbl.tex file in your samples folder. The first is typed as

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

0& \symbol{0} &\symbol{1}&\symbol{2}&\symbol{3}& \symbol{4}&\symbol{5}&\symbol{6}&\symbol{7}& \symbol{8}&\symbol{9}\\ \hline

6.6 Tabular environments 135

120& \symbol{120} &\symbol{121}&\symbol{123}& \symbol{123}&\symbol{124}&\symbol{125}&\symbol{126} &\symbol{127} && \\ \hline \end{tabular}

The second table is the same except that the numbers run from 128 to 255. The typeset table is shown in Section 5.4.4.

Rule

tabular environments

1. \begin{tabular} requires an argument consisting of a character l, r, or c, mean- ing left, right, or center alignment, for each column, and optionally, the | symbols. Each | indicates a vertical line in the typeset table. Spaces in the argument are ignored but can be used for readability.

2. Columns are separated by ampersands (&) and rows are separated by \\.

3. & absorbs spaces on either side.

4. The \hline command creates a horizontal rule in the typeset table. It is placed either at the beginning of the table (after the \begin line) or it must follow a \\ command.

5. If you use a horizontal line to finish the table, you must separate the last row of the table from the \hline command with the \\ command.

6. \begin{tabular} takes an optional argument, b or t, to specify the bottom or the top vertical alignment of the table with the baseline. The default is center alignment.

Remember to put the optional argument b or t in square brackets, as in \begin{tabular}[b]{ | l | r | r | r | } If you forget to place an \hline command right after \\ in the last row, you get

an error message such as ! Misplaced \noalign.

\hline ->\noalign {\ifnum 0=‘}\fi \hrule \@height \arrayrulew...

l.9 ....00 & 1.85 & 0.71 \hline

136 Chapter 6 Text environments

More column-formatting commands The required argument of the tabular environment may contain column-formatting

commands of various types. An @-expression, for instance, @{.}, replaces the space L A TEX normally inserts between two columns with its argument. For example,

\begin{tabular}{r @{.} l} 3&78\\ 4&261\\

4 \end{tabular}

creates a table with two columns separated by a decimal point. In effect, you get a single, decimal-aligned column:

This example is an illustration. You should use David Carlisle’s dcolumn package if you need a decimal-aligned column (see Section 12.3.1).

The width of a column depends on the entries in the column by default. You can specify a width by using the p column specifier:

p{width } For instance, if you want the first column of Table 6.1 to be 1 inch wide, then type \begin{tabular}{ | p{1in} | r | r | r | }\hline

\end{tabular} which typesets as

Name

Peter

John

David

6.6 Tabular environments 137

To center the items in the first column, precede each item with a \centering com- mand (see Section 6.3). Note that the first column is actually somewhat over 1 inch wide, because of the extra space provided around the column boundaries.

The p column specifier can also be used for multiline entries. Refinements \hline draws a horizontal line the whole width of the table. \cline{a-b } draws a

horizontal line from column a to column b. For instance, \cline{1-3} or \cline{4-4} Another useful command is \multicolumn, which is used to span more than one

column, for example, \multicolumn{3}{c}{\emph{absent}} The first argument is the number of columns spanned by the entry, the second is the

alignment (an optional vertical line designator | for this row only can also be included), and the third argument is the entry. Note that the entry for the spanned columns is in braces. An example is shown in Table 6.2, typed as follows:

\begin{table}[h!]

Table 6.2: Table with \multicolumn.

Name Month Week Amount

Grand Total

Table 6.3: Table with \multicolumn and \cline.

138 Chapter 6 Text environments

\begin{center}

\begin{tabular}{ | l | r | r | r | } \hline

Peter & 2.45 & 34.12 & 1.00\\ \hline John

& \multicolumn{3}{c |}{\emph{absent}}\\ \hline David

& 2.00 & 1.85 & 0.71\\ \hline \end{tabular} \caption{Table with \bsl\texttt{multicolumn}.} \label{Ta:mc}

\end{center} \end{table}

The next example, shown in Table 6.3, uses the \multicolumn and \cline com- mands together:

\begin{table}[t] \begin{center} \begin{tabular}{ | c c | c | r | } \hline

Name & Month & Week & Amount\\ \hline Peter & Jan. &1

& 1.00\\ \cline{3-4}

& 12.78\\ \cline{3-4}

& 0.71\\ \cline{3-4}

& 15.00\\ \cline{2-4} & \multicolumn{2}{| l}{Total} & 29.49\\ \hline

John & Jan. &1 & 12.01\\ \cline{3-4}

& 3.10\\ \cline{3-4}

& 10.10\\ \cline{3-4}

& 0.00\\ \cline{2-4} & \multicolumn{2}{| l}{Total} & 25.21\\ \hline

\multicolumn{3}{|l}{Grand Total} & 54.70\\ \hline

\end{tabular} \caption{Table with \bsl\texttt{multicolumn} and \bsl\texttt{cline}.}\label{Ta:multicol+cline}

\end{center} \end{table}

The \parbox command (see Section 5.9.3) can be used to produce a single mul- tiline entry. Recall that the first argument of \parbox is the width of the box. A p{} width designator creates a column in which all entries can be multiline. As an example, to replace Grand Total by Grand Total for Peter and John, type the last line as

6.6 Tabular environments 139

\multicolumn{3}{l}{ \parbox[b]{10em}{Grand Total\\ for Peter and John} } & 54.70\\ \hline

Note the use of the bottom alignment option (see Section 5.9.3). The last row of the modified table prints

Grand Total for Peter and John

The spacing above Grand Total is not quite right. It can be adjusted with a strut (see Section 5.9.5),

\parbox[b]{10em}{\strut Grand Total\\

for Peter and John:}

Finally, vertical spacing can be adjusted by redefining \arraystretch. For in- stance, in the table

Area

Students

5th Grade :

63.4 m 2 22

6th Grade :

typed as \begin{center}

\begin{tabular}{|r|c|c|}\hline & \textbf{Area} & \textbf{Students}\\ \hline \textbf{5th Grade}: & 63.4 m\textsuperscript{2} &22\\ \hline \textbf{6th Grade}: & 62.0 m\textsuperscript{2} &19\\ \hline \textbf{Overall}: & 62.6 m\textsuperscript{2} &20\\ \hline

\end{tabular} \end{center}

you may find that the rows are too crowded. The vertical spacing may be adjusted by adding the line

\renewcommand{\arraystretch}{1.25} to the tabular environment. To limit its scope, add it after \begin{center}

140 Chapter 6 Text environments

The adjusted table is typeset as

Area

Students

5th Grade :

63.4 m 2 22

6th Grade :

In some tables, horizontal and vertical lines do not always intersect as desired. Fine control over these intersections is provided by the hhline package (see Sec- tion 12.3.1).

Chapter 5 of The L A TEX Companion, 2nd edition [46] deals with tabular mate- rial, discussing many extensions, including multipage tables, decimal-point alignment, footnotes in tables, tables within tables, and so on.

6.6.1 Table styles

L A TEX can draw double horizontal and vertical lines in tables with ease. As a result, there are far too many double lines in L A TEX tables, resulting in cluttered and confusing tables. The Chicago Manual of Style, 15th edition [11] has almost 80 pages on tables. For simple tables it advocates a simple style, as shown in Table 6.4. Notice

the generous space above and below the column heads, which has been achieved with the \rule[-8pt]{0pt}{22pt} command,

some extra space above the first line of data, which has been achieved with the \rule{0pt}{14pt} command,

the columns of equal width, which has been achieved with p{70pt} commands, and no vertical lines.

Most tables in this book have been designed according to this style using Simon Fear’s booktabs package (see Section E.1).

Table 6.4: Smokers and Nonsmokers, by Sex.

Total Males

Smoke

Don’t Smoke

3,362 Females

3.946 Total

6.7 Tabbing environments 141

Dokumen yang terkait

c. Bridging Teknik memanjat pada celah vertikal yang lebih besar (gullies). Caranya dengan menggunakan kedua tangan dan kaki sebagai pegangan pada kedua celah tersebut. Posisi badan mengangkang kaki sebagai tumpuan dibantu juga tangan sebagai penjaga kese

0 1 10

2. Marah: adalah kunci setiap kejahatan. Nabi saw. telah berpesan pada seseorang untuk menjauhi sikap marah dengan sabda beliau: "Janganlah engkau marah". Beliau mengulanginya berkali-kali. (H.R Bukhari). - Kebersihan Hati

2 5 14

Object Oriented Programming dengan Delphi (

1 4 75

E volusi dan aplikasi sistem informasi berbasis komputer

1 8 18

b. Data Manipulation Language (DML) : Digunakan untuk memanipulasi data dengan menggunakan perintah : select, - SISTEM BASIS DATA 1.rar (7,386Kb)

1 3 15

Hubungan Pola Asuh Orang Tua dan Kesiapan Psikologis Anak dengan Kebersihan Toilet Traning pada Anak Usia Prasekolah di Paud Ab-Arisalah Kota Lubuklinggau

0 1 8

Hubungan Pengetahuan dsn Sikap dengan Tindakan Hygiene Penjual makanan Jajanan di Lingkungan Sekolah Dasar Kaamatan Baturaja Timur Kabupaten Ogan Komering UIU Tahun 2013 A. Gani

1 3 14

Faktor faktor yang berhubungan dengan manfaatan Posyandu lansia oleh kelompok usia lanjut didesa Kemalara dalam wilayah kerja Puskesmas Kemalaraja OKU Tahun 2013. Saprianto, M.Kes

3 3 21

Korelasi umur, pekerjaan, dan keberadaan kontainer dengan kejadian penyakit malaria di desa simpang Martapura wilayah Kabupaten Ogan Komering Ulu Selatan tahun 2014 – A. Gani

0 0 18

Pengaruh Ekstrak Daun Jambu Biji (Psidiu Guajava Linn) dan Ekstrak daun Teh Hijau (Camelia Sinensis) terhadap Pertumbuhan Escherichia Coli In Vitro dan Perbandingannya dengan Kotrimoksazol

2 16 20