BudiBadu Logo
Samplebadu

LaTeX by Example: Tables

LaTeX2e

Tables organize data in rows and columns. This sample shows the `tabular` environment, column alignment, and horizontal lines.

Code

\documentclass{article}

\begin{document}
    % Basic table with vertical lines
    % l = left align, c = center, r = right align
    % | = vertical line
    \begin{tabular}{|l|c|r|}
        \hline
        \textbf{Item} & \textbf{Qty} & \textbf{Price} \\
        \hline
        Apple & 5 & $1.20 \\
        Banana & 12 & $0.80 \\
        Cherry & 50 & $0.10 \\
        \hline
    \end{tabular}
    
    \vspace{1cm}
    
    % Professional looking table (requires booktabs package)
    % No vertical lines, different thickness horizontal lines
    % \usepackage{booktabs} in preamble
    % \begin{tabular}{lcr}
    %     \toprule
    %     Item & Qty & Price \\
    %     \midrule
    %     Apple & 5 & $1.20 \\
    %     ...
    %     \bottomrule
    % \end{tabular}
\end{document}

Explanation

The tabular environment is the standard tool for creating tables. The environment takes a mandatory argument that defines the column alignment and vertical separators. For example, {|l|c|r|} defines three columns: left-aligned, centered, and right-aligned, with vertical lines between them and on the outsides.

Inside the table, columns are separated by the ampersand &, and rows are ended with double backslashes \\. Horizontal lines are drawn using the \hline command. You can place \hline between any rows or at the top/bottom of the table.

For professional-quality tables, it is widely recommended to avoid vertical lines entirely and use the booktabs package. This package provides commands like \toprule, \midrule, and \bottomrule which create horizontal lines of varying thickness and spacing, resulting in a much cleaner and more readable layout.

Code Breakdown

7
{|l|c|r|} specifies the column format. | draws a vertical line.
9
Item & Qty & Price \\ defines the header row. & separates cells.