1% ============================================================================
2% tables_demo.tex - Tables in LaTeX
3% ============================================================================
4% This document demonstrates comprehensive table creation in LaTeX, including
5% basic tabular environments, professional formatting with booktabs, multi-row
6% and multi-column cells, colored rows, and numerical data with siunitx.
7%
8% Compilation:
9% pdflatex tables_demo.tex (run twice for cross-references)
10% ============================================================================
11
12\documentclass[11pt]{article}
13
14\usepackage[margin=1in]{geometry}
15\usepackage[T1]{fontenc}
16\usepackage{lmodem}
17
18% Table packages
19\usepackage{array} % Extended column specifications
20\usepackage{tabularx} % Auto-width columns
21\usepackage{booktabs} % Professional table formatting
22\usepackage{multirow} % Multi-row cells
23\usepackage{makecell} % Line breaks in cells
24\usepackage{longtable} % Tables spanning multiple pages
25
26% Colors for table rows
27\usepackage[table]{xcolor}
28
29% Number formatting
30\usepackage{siunitx} % Align numbers on decimal point
31
32% For better captions
33\usepackage{caption}
34
35% Hyperlinks
36\usepackage{hyperref}
37\hypersetup{colorlinks=true, linkcolor=blue}
38
39\title{Tables in \LaTeX}
40\author{Typesetting Department}
41\date{\today}
42
43\begin{document}
44
45\maketitle
46
47\tableofcontents
48
49\listoftables
50
51\section{Introduction}
52
53Tables are essential for presenting structured data. This document demonstrates
54various techniques for creating professional-looking tables in \LaTeX, from
55basic \texttt{tabular} environments to advanced formatting.
56
57% ============================================================================
58\section{Basic Tables}
59% ============================================================================
60
61\subsection{The \texttt{tabular} Environment}
62
63The basic table structure uses the \texttt{tabular} environment:
64
65\begin{verbatim}
66\begin{tabular}{column specifications}
67 cell 1 & cell 2 & cell 3 \\
68 cell 4 & cell 5 & cell 6 \\
69\end{tabular}
70\end{verbatim}
71
72\subsection{Simple Table Example}
73
74\begin{table}[h]
75\centering
76\caption{A basic table}
77\label{tab:basic}
78\begin{tabular}{lll}
79\hline
80Name & Age & City \\
81\hline
82Alice & 25 & New York \\
83Bob & 30 & London \\
84Charlie & 35 & Tokyo \\
85\hline
86\end{tabular}
87\end{table}
88
89Table~\ref{tab:basic} shows a simple three-column table with horizontal lines.
90
91\subsection{Column Specifications}
92
93\begin{table}[h]
94\centering
95\caption{Column specification examples}
96\label{tab:columns}
97\begin{tabular}{|l|c|r|}
98\hline
99Left & Center & Right \\
100\hline
101Text & Text & Text \\
102Aligned & Aligned & Aligned \\
103Left & Center & Right \\
104\hline
105\end{tabular}
106\end{table}
107
108Common column types:
109\begin{itemize}
110 \item \texttt{l} -- left-aligned
111 \item \texttt{c} -- centered
112 \item \texttt{r} -- right-aligned
113 \item \texttt{p\{width\}} -- paragraph column with specified width
114 \item \texttt{|} -- vertical line
115\end{itemize}
116
117% ============================================================================
118\section{Professional Tables with \texttt{booktabs}}
119% ============================================================================
120
121\subsection{Why Use \texttt{booktabs}?}
122
123The \texttt{booktabs} package provides professional-looking horizontal rules
124without vertical lines, which is considered better typographic practice.
125
126\begin{table}[h]
127\centering
128\caption{Professional table with booktabs}
129\label{tab:booktabs}
130\begin{tabular}{lcc}
131\toprule
132Algorithm & Accuracy (\%) & Time (ms) \\
133\midrule
134Method A & 85.3 & 120 \\
135Method B & 92.1 & 95 \\
136Method C & 88.7 & 110 \\
137\bottomrule
138\end{tabular}
139\end{table}
140
141Key \texttt{booktabs} commands:
142\begin{itemize}
143 \item \verb|\toprule| -- top rule
144 \item \verb|\midrule| -- middle rule (separates header from data)
145 \item \verb|\bottomrule| -- bottom rule
146 \item \verb|\cmidrule{i-j}| -- partial rule from column i to j
147\end{itemize}
148
149\subsection{Table with Partial Rules}
150
151\begin{table}[h]
152\centering
153\caption{Using partial rules with cmidrule}
154\label{tab:cmidrule}
155\begin{tabular}{lrrrr}
156\toprule
157& \multicolumn{2}{c}{Method 1} & \multicolumn{2}{c}{Method 2} \\
158\cmidrule(lr){2-3} \cmidrule(lr){4-5}
159Dataset & Acc & Time & Acc & Time \\
160\midrule
161Set A & 85.3 & 120 & 87.2 & 115 \\
162Set B & 90.1 & 95 & 91.5 & 90 \\
163Set C & 88.7 & 110 & 89.3 & 105 \\
164\bottomrule
165\end{tabular}
166\end{table}
167
168% ============================================================================
169\section{Multi-row and Multi-column Cells}
170% ============================================================================
171
172\subsection{Multi-column Cells}
173
174Use \verb|\multicolumn{n}{alignment}{content}| to span multiple columns:
175
176\begin{table}[h]
177\centering
178\caption{Table with multicolumn cells}
179\label{tab:multicolumn}
180\begin{tabular}{lccc}
181\toprule
182& \multicolumn{3}{c}{Measurements} \\
183\cmidrule{2-4}
184Sample & Trial 1 & Trial 2 & Trial 3 \\
185\midrule
186A & 12.5 & 12.7 & 12.3 \\
187B & 15.2 & 15.4 & 15.1 \\
188C & 18.9 & 18.7 & 19.0 \\
189\midrule
190\multicolumn{4}{c}{Average: 15.4} \\
191\bottomrule
192\end{tabular}
193\end{table}
194
195\subsection{Multi-row Cells}
196
197Use \verb|\multirow{n}{width}{content}| to span multiple rows:
198
199\begin{table}[h]
200\centering
201\caption{Table with multirow cells}
202\label{tab:multirow}
203\begin{tabular}{llrr}
204\toprule
205Category & Subcategory & Value 1 & Value 2 \\
206\midrule
207\multirow{3}{*}{Group A} & Item 1 & 10 & 20 \\
208 & Item 2 & 15 & 25 \\
209 & Item 3 & 12 & 22 \\
210\midrule
211\multirow{2}{*}{Group B} & Item 4 & 18 & 28 \\
212 & Item 5 & 20 & 30 \\
213\bottomrule
214\end{tabular}
215\end{table}
216
217\subsection{Combining Multi-row and Multi-column}
218
219\begin{table}[h]
220\centering
221\caption{Complex table with multirow and multicolumn}
222\label{tab:complex}
223\begin{tabular}{llcccc}
224\toprule
225& & \multicolumn{2}{c}{Condition 1} & \multicolumn{2}{c}{Condition 2} \\
226\cmidrule(lr){3-4} \cmidrule(lr){5-6}
227Type & Subtype & Mean & SD & Mean & SD \\
228\midrule
229\multirow{3}{*}{Type A} & Var 1 & 85.3 & 2.1 & 87.2 & 1.8 \\
230 & Var 2 & 90.1 & 1.5 & 91.5 & 1.3 \\
231 & Var 3 & 88.7 & 1.9 & 89.3 & 1.7 \\
232\midrule
233\multirow{2}{*}{Type B} & Var 1 & 92.5 & 1.2 & 94.1 & 1.0 \\
234 & Var 2 & 95.3 & 0.9 & 96.2 & 0.8 \\
235\bottomrule
236\end{tabular}
237\end{table}
238
239% ============================================================================
240\section{Colored Tables}
241% ============================================================================
242
243\subsection{Row Colors}
244
245The \texttt{xcolor} package with \texttt{[table]} option enables row coloring:
246
247\begin{table}[h]
248\centering
249\caption{Table with alternating row colors}
250\label{tab:rowcolors}
251\rowcolors{2}{gray!10}{white}
252\begin{tabular}{lrrr}
253\toprule
254Product & Q1 Sales & Q2 Sales & Q3 Sales \\
255\midrule
256Product A & 1250 & 1380 & 1420 \\
257Product B & 980 & 1050 & 1120 \\
258Product C & 1560 & 1620 & 1590 \\
259Product D & 2100 & 2250 & 2340 \\
260Product E & 1840 & 1920 & 1980 \\
261\bottomrule
262\end{tabular}
263\end{table}
264
265\subsection{Column and Cell Colors}
266
267\begin{table}[h]
268\centering
269\caption{Table with colored columns and cells}
270\label{tab:cellcolors}
271\begin{tabular}{l>{\columncolor{blue!10}}c>{\columncolor{green!10}}c}
272\toprule
273Item & Score 1 & Score 2 \\
274\midrule
275A & 85 & 90 \\
276B & 78 & \cellcolor{red!20}65 \\
277C & 92 & 88 \\
278D & \cellcolor{yellow!30}95 & 93 \\
279\bottomrule
280\end{tabular}
281\end{table}
282
283% ============================================================================
284\section{Numerical Data with \texttt{siunitx}}
285% ============================================================================
286
287\subsection{Decimal Alignment}
288
289The \texttt{siunitx} package provides the \texttt{S} column type for aligning
290numbers on the decimal point:
291
292\begin{table}[h]
293\centering
294\caption{Numbers aligned with siunitx}
295\label{tab:siunitx}
296\begin{tabular}{lSSS}
297\toprule
298{Measurement} & {Value 1} & {Value 2} & {Value 3} \\
299\midrule
300Alpha & 12.345 & 1.2 & 0.00123 \\
301Beta & 1234.5 & 123.45 & 12.345 \\
302Gamma & 0.123 & 1234567.89 & 9.87654 \\
303Delta & 999.999 & 1.00001 & 100.0 \\
304\bottomrule
305\end{tabular}
306\end{table}
307
308Note: Column headers in \texttt{S} columns must be enclosed in braces.
309
310\subsection{Scientific Notation}
311
312\begin{table}[h]
313\centering
314\caption{Scientific notation with siunitx}
315\label{tab:scientific}
316\begin{tabular}{lS[scientific-notation=true]}
317\toprule
318{Constant} & {Value} \\
319\midrule
320Speed of light & 2.998e8 \\
321Planck constant & 6.626e-34 \\
322Avogadro number & 6.022e23 \\
323Electron mass & 9.109e-31 \\
324\bottomrule
325\end{tabular}
326\end{table}
327
328\subsection{Number Formatting Options}
329
330\begin{table}[h]
331\centering
332\caption{Custom number formatting with siunitx}
333\label{tab:formatting}
334\begin{tabular}{
335 l
336 S[table-format=2.3]
337 S[table-format=4.0, group-separator={,}]
338 S[table-format=1.2e2]
339}
340\toprule
341{Item} & {Precision} & {Large Numbers} & {Scientific} \\
342\midrule
343A & 12.345 & 12345 & 1.23e5 \\
344B & 9.876 & 987654 & 9.88e5 \\
345C & 45.123 & 4567 & 4.57e3 \\
346\bottomrule
347\end{tabular}
348\end{table}
349
350% ============================================================================
351\section{Wide Tables}
352% ============================================================================
353
354\subsection{Using \texttt{tabularx}}
355
356For tables that should fill the text width:
357
358\begin{table}[h]
359\centering
360\caption{Table with auto-width columns using tabularx}
361\label{tab:tabularx}
362\begin{tabularx}{\textwidth}{lXX}
363\toprule
364Category & Description & Example \\
365\midrule
366Type A & This is a longer description that will automatically wrap to fill
367the available space in the column. The X column type distributes space evenly.
368& Example content that also wraps automatically. \\
369Type B & Another description with significant content that demonstrates the
370automatic width adjustment feature of tabularx. & More example content here. \\
371Type C & Short description. & Short example. \\
372\bottomrule
373\end{tabularx}
374\end{table}
375
376\subsection{Landscape Tables}
377
378For very wide tables, use the \texttt{rotating} package (not shown here, but
379code example):
380
381\begin{verbatim}
382\usepackage{rotating}
383
384\begin{sidewaystable}
385\begin{tabular}{...}
386...
387\end{tabular}
388\end{sidewaystable}
389\end{verbatim}
390
391% ============================================================================
392\section{Long Tables Spanning Multiple Pages}
393% ============================================================================
394
395The \texttt{longtable} package allows tables to break across pages:
396
397\begin{longtable}{llrr}
398\caption{Long table example spanning multiple pages} \label{tab:long} \\
399\toprule
400ID & Name & Value 1 & Value 2 \\
401\midrule
402\endfirsthead
403
404\multicolumn{4}{c}{{\tablename\ \thetable{} -- continued from previous page}} \\
405\toprule
406ID & Name & Value 1 & Value 2 \\
407\midrule
408\endhead
409
410\midrule
411\multicolumn{4}{r}{{Continued on next page}} \\
412\endfoot
413
414\bottomrule
415\endlastfoot
416
417% Table content
418001 & Sample A & 12.5 & 15.3 \\
419002 & Sample B & 14.2 & 16.8 \\
420003 & Sample C & 13.7 & 17.2 \\
421004 & Sample D & 15.1 & 14.9 \\
422005 & Sample E & 12.9 & 16.5 \\
423006 & Sample F & 14.5 & 15.7 \\
424007 & Sample G & 13.3 & 17.8 \\
425008 & Sample H & 15.8 & 14.2 \\
426009 & Sample I & 12.2 & 16.1 \\
427010 & Sample J & 14.9 & 15.4 \\
428\end{longtable}
429
430% ============================================================================
431\section{Advanced Formatting Techniques}
432% ============================================================================
433
434\subsection{Line Breaks in Cells}
435
436Use \verb|\makecell| for line breaks within cells:
437
438\begin{table}[h]
439\centering
440\caption{Line breaks in cells}
441\label{tab:linebreaks}
442\begin{tabular}{lll}
443\toprule
444Method & \makecell{Advantages \\ (multiple)} & \makecell{Disadvantages \\ (multiple)} \\
445\midrule
446A & \makecell[l]{Fast \\ Accurate \\ Simple} & \makecell[l]{Expensive \\ Complex setup} \\
447B & \makecell[l]{Cheap \\ Easy} & \makecell[l]{Slow \\ Less accurate} \\
448\bottomrule
449\end{tabular}
450\end{table}
451
452\subsection{Fixed-Width Paragraph Columns}
453
454\begin{table}[h]
455\centering
456\caption{Using paragraph columns with fixed width}
457\label{tab:paragraph}
458\begin{tabular}{lp{5cm}r}
459\toprule
460ID & Description & Value \\
461\midrule
462A1 & This is a longer text that will automatically wrap within the 5cm wide
463column. It demonstrates the p column type. & 100 \\
464B2 & Another example with wrapped text showing how paragraph columns handle
465longer content automatically. & 200 \\
466C3 & Short text. & 150 \\
467\bottomrule
468\end{tabular}
469\end{table}
470
471\subsection{Diagonal Headers}
472
473Using \texttt{diagbox} package (example code):
474
475\begin{verbatim}
476\usepackage{diagbox}
477
478\begin{tabular}{|l|c|c|c|}
479\hline
480\diagbox{Row}{Column} & A & B & C \\
481\hline
4821 & 10 & 20 & 30 \\
4832 & 40 & 50 & 60 \\
484\hline
485\end{tabular}
486\end{verbatim}
487
488% ============================================================================
489\section{Complete Example: Research Data}
490% ============================================================================
491
492\begin{table}[htbp]
493\centering
494\caption[Experimental results summary]{Experimental results summary showing
495performance metrics across different algorithms and datasets. Bold values
496indicate best performance per metric.}
497\label{tab:complete}
498\rowcolors{2}{gray!5}{white}
499\begin{tabular}{
500 l
501 l
502 S[table-format=2.2]
503 S[table-format=2.2]
504 S[table-format=3.1]
505 S[table-format=1.2]
506}
507\toprule
508\multirow{2}{*}{Dataset} & \multirow{2}{*}{Algorithm} &
509{Accuracy} & {Precision} & {Time} & {F1} \\
510& & {(\%)} & {(\%)} & {(ms)} & {Score} \\
511\midrule
512\multirow{3}{*}{MNIST} & CNN & 98.45 & 98.32 & 125.5 & 0.98 \\
513 & ResNet & \textbf{99.12} & \textbf{99.05} & 187.3 & \textbf{0.99} \\
514 & VGG & 98.87 & 98.76 & \textbf{110.2} & 0.99 \\
515\midrule
516\multirow{3}{*}{CIFAR} & CNN & 85.23 & 84.95 & 142.8 & 0.85 \\
517 & ResNet & \textbf{92.56} & \textbf{92.34} & 210.5 & \textbf{0.93} \\
518 & VGG & 89.45 & 89.12 & \textbf{135.7} & 0.89 \\
519\midrule
520\multirow{3}{*}{ImageNet} & CNN & 72.34 & 71.89 & 1250.0 & 0.72 \\
521 & ResNet & \textbf{85.67} & \textbf{85.23} & 1875.5 & \textbf{0.86} \\
522 & VGG & 78.92 & 78.45 & \textbf{1120.3} & 0.79 \\
523\bottomrule
524\end{tabular}
525\end{table}
526
527Table~\ref{tab:complete} presents comprehensive experimental results across
528three datasets and three algorithms. ResNet consistently achieves the highest
529accuracy and F1 scores, while VGG offers the best computational efficiency.
530
531% ============================================================================
532\section{Table Best Practices}
533% ============================================================================
534
535\begin{enumerate}
536 \item \textbf{Use \texttt{booktabs}:} Avoid vertical lines; use
537 \verb|\toprule|, \verb|\midrule|, \verb|\bottomrule|
538
539 \item \textbf{Caption placement:} Place captions \emph{above} tables
540 (opposite to figures)
541
542 \item \textbf{Label after caption:} Always place \verb|\label| after
543 \verb|\caption|
544
545 \item \textbf{Alignment:} Align text left, numbers right or on decimal
546
547 \item \textbf{Units in headers:} Include units in column headers, not
548 in every cell
549
550 \item \textbf{Consistent precision:} Use the same number of decimal places
551 within a column
552
553 \item \textbf{Highlight key results:} Use bold or color sparingly to
554 emphasize important values
555
556 \item \textbf{Short captions:} Provide a short caption for the list of
557 tables:
558 \verb|\caption[short]{long caption}|
559
560 \item \textbf{Avoid overfull tables:} Break wide tables into multiple
561 smaller tables if possible
562
563 \item \textbf{Table notes:} Add notes below the table for explanations,
564 not in the caption
565\end{enumerate}
566
567% ============================================================================
568\section{Conclusion}
569% ============================================================================
570
571This document has demonstrated comprehensive table creation techniques:
572
573\begin{itemize}
574 \item Basic \texttt{tabular} environment with various column types
575 \item Professional formatting with \texttt{booktabs}
576 \item Multi-row and multi-column cells with \texttt{multirow}
577 \item Colored rows and cells with \texttt{xcolor}
578 \item Numerical alignment with \texttt{siunitx}
579 \item Wide tables with \texttt{tabularx}
580 \item Long tables with \texttt{longtable}
581 \item Advanced formatting techniques
582\end{itemize}
583
584Key packages for professional tables:
585\begin{verbatim}
586\usepackage{booktabs} % Professional rules
587\usepackage{multirow} % Multi-row cells
588\usepackage{siunitx} % Number alignment
589\usepackage[table]{xcolor} % Colored rows
590\usepackage{tabularx} % Auto-width columns
591\usepackage{longtable} % Multi-page tables
592\end{verbatim}
593
594\end{document}
595
596% ============================================================================
597% COMPILATION NOTES
598% ============================================================================
599% Compile with: pdflatex tables_demo.tex (run twice for cross-references)
600%
601% Common issues:
602% 1. \multirow requires the multirow package
603% 2. S columns (siunitx) require headers in braces: {Header}
604% 3. Vertical lines often look unprofessional - use booktabs instead
605% 4. longtable cannot be used inside a table environment
606% 5. Remember to \centering before tabular in a table environment
607% ============================================================================