Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a table whose column headings (centre aligned with \multicolumn{1}{c}{Foobar here}) are wider than the left-aligned data. This looks somewhat odd, as there are large right-margins in each column.

It is probably easiest to illustrate my query with an example. Let say that the column heading is 10em and the data in the column varies from 2em to 3em. Currently each row of data will have a right margin of between 8em and 7em (as the data is left aligned so the right margin picks up any slack).

What I want is a means of evening this out; so the left margin is (10em - 3em) / 2 = 3.5em and the right margin varying between 3.5em and 4.5em.

How can I go about this? I know the text of each column heading and the text of the widest piece of data in the table.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

The eqparbox package will solve this. Here is an example (modified from the eqparbox documentation to have left-aligned data like you described, and to be more aesthetic by using booktabs). (You have to run it through latex twice, for the box widths to converge.)

\documentclass{article}
\usepackage{eqparbox}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{@{}lccc@{}} \toprule
& \multicolumn{3}{c}{Sales (in millions)} \\ \cmidrule{2-4}
Product &
October & November & December \\ \midrule
Widgets & \eqparbox{oct}{55.2} &
\eqparbox{nov}{\textbf{89.2}} &
\eqparbox{dec}{57.9} \\
Doohickeys & \eqparbox{oct}{\textbf{65.0}} &
\eqparbox{nov}{64.1} &
\eqparbox{dec}{9.3} \\
Thingamabobs & \eqparbox{oct}{10.4} &
\eqparbox{nov}{8.0} &
\eqparbox{dec}{\textbf{109.7}} \\ \bottomrule
\end{tabular}
\end{document}

To make the wrapping in eqparbox more automatic, you can do some trickery with the array package

\documentclass{article}
\usepackage{eqparbox}
\usepackage{array}
\newsavebox{\tempbox}
\newcolumntype{Q}[1]{>{\begin{lrbox}{\tempbox}}%
    c<{\end{lrbox}\eqparbox{#1}{\unhcopy\tempbox}}}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{@{}lQ{oct}Q{nov}Q{dec}@{}}
\toprule
    & \multicolumn{3}{c}{Sales (in millions)} \\ 
\cmidrule{2-4}
Product      & 
\multicolumn{1}{c}{October} & 
\multicolumn{1}{c}{November} & 
\multicolumn{1}{c}{December} \\
\midrule
Widgets      & 55.2 & 89.2 & 57.9 \\
Doohickeys   & 65.0 & 64.1 & 9.3 \\
Thingamabobs & 10.4 & 8.0  & 109.7 \\
\bottomrule
\end{tabular}
\end{document} 
share|improve this answer
    
Brilliant, that worked a treat. Does LaTeX provide any neat ways of automatically encasing a column with \eqparbox{foo}{#1}? Just to save on the typing. –  Freddie Witherden Aug 20 '10 at 10:32
    
I edited to show how to automatically wrap a column. –  Lev Bishop Aug 20 '10 at 14:43
    
@Freddie Witherder you could use \newcommand{} to create a function which represents one line, and in this command you can add the \eqparbox. for the above example: \newcommand{\salesline}[4]{#1 & \eqparbox{oct}{#2} & \eqparbox{nov}{#3} & \eqparbox{dec}{#4} \\ } you can than use in your table: \salesline{Widgets}{55.2}{89.2}{57.9} and the rest is easy :) –  Davy Landman Nov 13 '10 at 13:14
add comment

I have simply created multiple columns (and used a multi-column heading). Then inside the table body, I just need to replace & -> &&; not too much typing. In the first row you can add some spacing commands that ensures that the empty columns aren't too narrow.


If you want to keep your table body very simple, here is another trick:

\newcommand{\filler}{\hspace{2em}}
\begin{tabular}{r@{\filler}r@{\filler}r@{\filler}}
    \multicolumn{1}{c}{Long heading} &
    \multicolumn{1}{c}{Long heading} &
    \multicolumn{1}{c}{Long heading} \\
      1 &  2 & 123 \\
    111 & 12 &  23 \\
     21 &  2 &   3 \\
      1 & 22 &  13
\end{tabular}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.