2

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}
0

2 Answers 2

0

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

1

With {NiceTabular} of nicematrix, you have a command \RowStyle to fix the style of the current row. That command has a option bold. Tiwht that option, bold style is used both in text mode and in math mode.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{amssymb}

\begin{document}
\begin{NiceTabular}{@{}>{\boldmath$}l<{$}ccc@{}}
\toprule
\RowStyle[bold]{}
n         & 2 & 1 &0\\
x[n]      & 1 & 0 &3\\
\midrule
2x[n]     & 5 & 0 &1\\
y[n]=\sum &   & 1 &0\\
\bottomrule
\end{NiceTabular}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .