Tx · LaTeX & TikZ

How to Make a LaTeX Table with Grouped Headers and a Total Row

Published By the 9to5Science editorial team

At a glance

  1. 1. Count the columns

    List the label column and every data column so each row has the same number of cells.

  2. 2. Group the headers

    Use \multicolumn to center a heading over the columns it describes.

  3. 3. Mark each span

    Use \cmidrule with the correct column numbers to draw short rules beneath grouped headings.

  4. 4. Add the totals

    Sum each data column, separate the total row with \midrule, and finish with \bottomrule.

A table with grouped headers needs one heading to span several data columns, while its final row often reports column totals. In LaTeX, \multicolumn creates the spans, and the booktabs package provides clean horizontal rules for separating header groups and totals.

The example below has a category column and three two-column groups: results for 2023, results for 2024, and the combined results. Its last row sums each numeric column.

Complete example

The example uses the booktabs package. Save it as a .tex file and compile it with a standard LaTeX engine.

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}[htbp]
\centering
\caption{Sales by category and region}
\label{tab:sales}
\begin{tabular}{lrrrrrr}
\toprule
& \multicolumn{2}{c}{2023}
& \multicolumn{2}{c}{2024}
& \multicolumn{2}{c}{Both years} \\
\cmidrule(lr){2-3}
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
Category & North & South & North & South & North & South \\
\midrule
Alpha & 12 & 9 & 14 & 11 & 26 & 20 \\
Beta  & 8  & 7 & 9  & 6  & 17 & 13 \\
Gamma & 10 & 8 & 14 & 12 & 24 & 20 \\
\midrule
\textbf{Total} & \textbf{30} & \textbf{24} & \textbf{37}
& \textbf{29} & \textbf{67} & \textbf{53} \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

How the grouped header works

The column specification {lrrrrrr} defines seven columns: one left-aligned label column (l) and six right-aligned numeric columns (r). Every row in the tabular must account for those same seven columns.

The first header row begins with an empty cell for the label column. Each \multicolumn{2}{c}{...} then makes one centered heading span two columns. For example, \multicolumn{2}{c}{2023} covers the North and South columns for 2023. The next header row gives the individual names of those columns.

The \cmidrule(lr){2-3} command draws a short rule under columns 2 and 3. The (lr) trims the line slightly at both ends, so neighboring group rules do not run together. Column numbers include the initial Category column: the three numeric groups therefore occupy columns 2–3, 4–5, and 6–7.

Add and check the total row

Add the values down each numeric column. In this example, the 2023 North total is 12+8+10=3012+8+10=30, and the 2023 South total is 9+7+8=249+7+8=24. The combined North total is 26+17+24=6726+17+24=67; the combined South total is 20+13+20=5320+13+20=53. The combined figures also equal the corresponding 2023 and 2024 figures added together.

The total row has one label followed by six values, just like every other body row. \midrule sets it apart from the data, and \bottomrule closes the table. The \textbf commands are optional; they make the label and figures stand out.

If you want a single overall total instead of a total for each column, sum each underlying observation once. Here, add the 2023 and 2024 column totals (30+24+37+2930+24+37+29) and exclude the “Both years” columns, which repeat those observations; the grand total is 120120. You can put it across the numeric columns with a row such as \textbf{Grand total} & \multicolumn{6}{r}{\textbf{120}} \\. The six-column span fills the numeric part of the table with one right-aligned entry.

Common mistakes

  • Incorrect column counts: A row with a label and six numbers needs seven cells. Use & between cells and end the row with \\.
  • Wrong span positions: \multicolumn and \cmidrule use table column numbers, counting the first column as column 1. Check that each rule covers the same columns as its heading.
  • Forgetting that \multicolumn replaces a cell: It takes the place of the cells it spans in that row. Do not add extra & separators for the columns it covers.
  • Using \hline for every rule: booktabs rules are designed for readable tables with grouped headings. Avoid combining them with vertical bars unless a specific style guide requires a different design.
  • Adding totals without checking the data: Recalculate each column independently, and verify that subtotals agree with combined columns when the table reports both.

If your table must continue across pages, a normal tabular inside a table float will not split automatically; use a multipage table environment instead. For a table that fits on one page, the example above provides a reusable pattern for grouped headers and a clear total row.

References

  1. The booktabs package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.