When creating table headings, a 1-cell \multicolumn
is often used. To simplify this, one might make a table-head macro:
% this definition works, but doesn't allow controlling alignment
\newcommand{\Th}[1]{\multicolumn{1}{l}{\textbf{#1}}}
However, we might not always want a left-aligned column; sometimes it might be more appropriate to right-align or center, etc. However, it’s very difficult to add an optional argument controlling the column specification:
\newcommand{\Th}[2][l]{\multicolumn{1}{#1}{\textbf{#2}}}
% when used: ! Misplaced \omit.
% \multispan ->\omit
% \@multispan
% l.8 \Th{Header}
% & \Th[r]{header (\textsc{usd})} & \Th{header} \\
Below are some ideas I had for potential solutions; the error messages they create are above the definition. What am I missing here?
This answer to “re-using column specifications in tabular” implies that solution #3 — which temporarily creates a new column-type and uses it — should work, but it still creates an error.
I’m also aware that the makecell
package provides a partial solution via its \thead
macro, but it’s not granularly-configurable enough — and I would like to know why these definitions don’t work either way.
\documentclass{article}
\usepackage{array}
\makeatletter
% attempt #1: edef/expandafter solution
% when used: ! Use of \\Th doesn't match its definition.
% \text@command #1->\def \reserved@a {
% #1}\ifx \reserved@a \@empty \let \check@...
% l.16 \Th{Header}
% & \Th{header (\textsc{usd})} & \Th{header} \\
\newcommand{\Th}[2][l]{%
\edef\@multicolumncontents{{1}{#1}{\textbf{#2}}}%
\expandafter\multicolumn\@multicolumncontents}
% attempt #2: expandafter/begingroup solution
% when used: ! Misplaced \omit.
% \multispan ->\omit
% \@multispan
% l.21 \Th{Header}
% & \Th[r]{header (\textsc{usd})} & \Th{header} \\
\newcommand{\Th}[2][l]{%
\expandafter\multicolumn\begingroup{1}{#1}{\textbf{#2}}\endgroup}
% attempt #3: newcolumntype solution
% when used: ! Misplaced \omit.
% \multispan ->\omit
% \@multispan
% l.31 \Th{Header}
% & \Th[r]{header (\textsc{usd})} & \Th{header} \\
\newcommand{\Th}[2][l]{%
\newcolumntype\@thcol{#1}%
\multicolumn{1}{\@thcol}{\textbf{#2}}}
\makeatother
\begin{document}
\begin{tabular}{rrr}
\Th{Header} & \Th[r]{header (\textsc{usd})} & \Th{header} \\
data & 12831 & data \\
1238 & 12.38 & 4893258439 \\
$\delta$ & 10 & 1238 \\
\end{tabular}
\end{document}