In my LaTeX document I have quite a few large tables where I would like to set the first row in bold font as well as the first column. This works nicely with some well-known tricks. However, when my first column is in math mode, compilation fails with "command \bfseries invalid in math mode".

When I use an empty dummy first column before the math column everything works fine, except for some extra indentation before the first column - see the attached M(N)WE.

Any ideas how I could get a nicely aligned first column in math mode?

\documentclass{article}
\usepackage{array}
\usepackage{booktabs} % midrule, tabularnewline
\usepackage{amssymb} % boldmath

\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}

\begin{document}
% works fine but gives unwanted indent:
\begin{tabular}{@{}+p{0pt} >{\boldmath$}l<{$} ^c *{2}{^c}@{}}\toprule
\rowstyle{\bfseries}
&n       &2 &1 &0\tabularnewline
&x[n]    &1 &0 &3 \tabularnewline \midrule
&2x[n]   &5 &0 &1 \tabularnewline
&y[n]=\sum{}&&1 &0\tabularnewline \bottomrule
\end{tabular}

\vspace{1 cm}

% gives the error described above:
\begin{tabular}{@{}>{\boldmath$}l<{$} +c *{2}{^c}@{}}\toprule
\rowstyle{\bfseries}
n       &2 &1 &0\tabularnewline
x[n]    &1 &0 &3 \tabularnewline \midrule
2x[n]   &5 &0 &1 \tabularnewline
y[n]=\sum{}&&1 &0\tabularnewline \bottomrule
\end{tabular}

\end{document}
up vote 0 down vote accepted

Your MWE is full of tricks, so one more will not be harmful :-). Try the following:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs} % midrule, tabularnewline
\usepackage{amssymb} % boldmath

\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}

    \begin{document}
    \begin{tabular}{@{\hspace{-2\tabcolsep}}% <-- compensate tacolsep of empty column
+p{0pt} >{\boldmath$}l<{$} ^c *{2}{^c}@{}}
    \toprule
\rowstyle{\bfseries}
& n         & 2 & 1 &0\tabularnewline
& x[n]      & 1 & 0 &3\tabularnewline 
    \midrule
& 2x[n]     & 5 & 0 &1\tabularnewline
& y[n]=\sum &   & 1 &0\tabularnewline 
    \bottomrule
\end{tabular}
    \end{document}

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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