# separately centered table headers, separately centered table content

edited for clarity:

dear texperts: I have an interesting puzzle. I am thinking that the following format is better than one in which the middle column is all 'r' and the right column is all 'l'. given package ulem,

\documentclass[12pt]{article}
\usepackage{ulem}
\usepackage{times}
\begin{document}
\begin{tabular}{c c c}
\uline{Value} & \uline{A Very Interesting Factorial} & \uline{More Columns} \\[0.2ex]
\begin{tabular}{r} 3 \\ 6 \\ 8 \\ 10 \end{tabular} &
\begin{tabular}{r} 6 \\ 720 \\ 40,320 \\ 3,628,800 \end{tabular} &
\begin{tabular}{l} small \\ large \\ larger \\ largest \end{tabular} \\
\end{tabular}
\end{document}


(how do some of you include .png images in stackexchange.com? I made a png for easy visualization, but could not figure out how to embed it here.) A (egreg): use the image tab at the top, not markdown.

in this example, my format is exactly what I want it to be. (I was unclear in my first description, thinking the latex could speak for itself---apologies.) my point here was not booktabs. my point is about how the header row and the content rows align. my desire is that the columns are centered below, though with content cell alignment of 'r' for the middle one and 'l' for the right one. it is not a mere "center" in the non-header columns. it is that the header has column names that are much longer than their contents, and therefore should center more nicely above their contents. this seems to me to be a more sensible format (albeit in a concocted example) than what I have been using to date.

another common solution to the same conundrum here---which I am not asking for---is to break the header cell so that it is not much wider than its content below, and then using its own formatting, e.g., via a \multicolumn. this can be more pleasing in many situations (e.g., portrait documents with lots of vertical space but not much horizontal space, and easily broken row titles), but not in all.

yet the way I accomplished this is not a good idea, because it loses the tex-ish association of columns. when rows have different heights, it does not work. looks wise, it is exactly what I want: the values and factorial contents cells are all right justified, but the entire columns are centered below their headers. similarly, the "more columns" is left-justified, but because its row header is larger, the content is all indented (here by about two em's).

I could achieve something like it with hand-tuned \makebox[Xem][c]{} commands in the first row, but this again requires a lot of physical (rather than logical) markup.

a good logical markup would be something like

\begin{tabular}{rrl}
3 &   6 & small \\
6 & 720 & large \\
8 & 40,320 & larger \\
10 & 3,628,800 & largest \\
\end{tabular}


plus some preamble magic that knows that each header cell should center with the widest row cell below it, and then add whatever space is needed left and right so that it does not stick into its neighbors.

more specifically, is there a package that does what I "hand-formatted" physically the logical way instead? probably not, but LaTeX always surprises me, both with its flexibility and shortcomings.

/iaw

PS: scribe and then latex get a lot of logical markup right, which is even more remarkable given that html5 still gets basics wrong 40 years later (section, article, header confusion). but latex forgot in its logical markup to separate tabular column and row headers, aka html <th> vs <td> . but this is a digression.

• You can upload a PNG file by clicking the “screen” icon on the top of the editing window. Please, add also a minimal preamble for making the examples compilable. – egreg Jan 19 '15 at 22:51
• It isn't at all clear what your question is. Usually If entering tables I'll use a format something like your last example, with a suitable local definition of \header depending on the format required, typically \newcommand\header[1]{\multicolumn{1}{c}{\textbf{#1}} is that what you are looking for? – David Carlisle Jan 19 '15 at 22:54
• Are you asking how to make/let the text in a header cell wrap around, in a column of a prespecified width? – Mico Jan 19 '15 at 23:19
• thx, egreg and david. (nope, mico, I don't want to wrap it. this would be another solution to the long row issue.) – ivo Welch Jan 20 '15 at 4:13

This gives, I think, what the OP asks for, and also demonstrates that it works if not all row heights are equal.

\documentclass[12pt]{article}
\usepackage{ulem}
\usepackage{times}
\usepackage{tabstackengine,calc}
\newcommand\mk[3][r]{\protect\makebox[\widthof{\csname big#2\endcsname}][#1]{#3}}
\begin{document}
\def\bigA{10}
\def\bigB{3,628,800}
\def\bigC{largest}
\tabularShortstack{ccc}{
\uline{Value} & \uline{A Very Interesting Factorial} & \uline{More Columns}\\
\mk{A}{3} & \mk{B}{6} & \mk[l]{C}{small}\\
\mk{A}{6} & \mk{B}{720} & \mk[l]{C}{large}\\
\mk{A}{8} & \mk{B}{40,320} & \mk[l]{C}{\stackanchor{larger}{(!!!)}}\\
\mk{A}{10} & \mk{B}{3,628,800} & \mk[l]{C}{largest}
}
\end{document}


As I pointed out to the OP in the comments below, the TABstack used above could be replaced with a tabular, while still using the \mk[alignment]{column}{data} approach:

\documentclass[12pt]{article}
\usepackage{ulem}
\usepackage{times}
\usepackage{calc, stackengine}
\newcommand\mk[3][r]{\protect\makebox[\widthof{\csname big#2\endcsname}][#1]{#3}}
\begin{document}
\def\bigA{10}
\def\bigB{3,628,800}
\def\bigC{largest}
\begin{tabular}{ccc}
\uline{Value} & \uline{A Very Interesting Factorial} & \uline{More Columns}\\
\mk{A}{3} & \mk{B}{6} & \mk[l]{C}{small}\\
\mk{A}{6} & \mk{B}{720} & \mk[l]{C}{large}\\
\mk{A}{8} & \mk{B}{40,320} & \mk[l]{C}{\stackanchor{larger}{(!!!)}}\\
\mk{A}{10} & \mk{B}{3,628,800} & \mk[l]{C}{largest}
\end{tabular}
\end{document}

• wow. great. definitely one solution. will check as solved in a day or two if this is the best one, too. thanks, steven. – ivo Welch Jan 20 '15 at 4:31
• @ivoWelch I used a TABstack, but the same \mk{column}{data} approach could be used inside a tabular as well, if you prefer. – Steven B. Segletes Jan 20 '15 at 4:33
• one drawback is that one needs to know a priori which cell is widest (3,628,800). but I guess this is unavoidable. is tabstackengine still needed in the second tex code example? – ivo Welch Jan 21 '15 at 0:45
• @ivo Required fore-knowledge is the drawback, indeed. You are right, tabstackengine is not required for MWE #2, however stackengine is, since I still use a \stackanchor within the tabular. – Steven B. Segletes Jan 21 '15 at 1:00

Do you want something like makecell and/or booktabs? For example:

The different weights of rules and improved spacing are from booktabs. (This could be emulated using just makecell if required.) The column head format is produced by \thead{} (equivalent of your \header, if I've understood) and set up in the preamble using a couple of the commands provided by makecell.

\theadfont determines the font used in the headers. \theadalign determines the alignment.

makecell is certainly overkill in this example, and a simple custom macro would be a more sensible choice. However, I'm not entirely clear what the extent of your modifications will be, and you may find it offers a flexible and powerful alternative to layers of nested tabulars and manual spacing adjustments....

I am not quite sure what you want to use underlining for - it seems an odd choice in a tabular - so I've contented myself with a simple \midrule from booktabs.

If you are customising tabulars, you should definitely read the booktabs documentation on producing professional-quality tabulars.

\documentclass{article}
\usepackage{makecell,booktabs}

\begin{document}

\begin{tabular}{rrl}
\toprule
3 &   6 & small \\
6 & 720 & large \\
8 & 40,320 & larger \\
10 & 3,628,800 & largest \\
20,987,456.879  &   &   another\\\bottomrule
\end{tabular}

\end{document}


Your question isn't very clear. I thought you might be looking for

\documentclass{article}
\begin{document}
\begin{tabular}{rrl}
3 &   6 & small \\
6 & 720 & large \\
8 & 40,320 & larger \\
10 & 3,628,800 & largest \\
\end{tabular}

\end{document}


which produces

But perhaps your issue is that the headers are not obviously centred as they are the widest entry,so perhaps you'd prefer

\documentclass{article}

\begin{document}
\begin{tabular}{crc crc clc}
&    3 &&&   6 &&& small &\\
&   6 &&& 720 &&& large &\\
&    8 &&& 40,320 &&& larger &\\
&   10 &&& 3,628,800 &&& largest &\\
\end{tabular}

\end{document}


or

\documentclass{article}
\usepackage{dcolumn}
\begin{document}
\begin{tabular}{D..{3.2}D..{8.0}l}