31

I'm struggling to make a single numeric cell in an siunitx table bold. (A similar question for making a whole column bold is available here.)

I want to define a simple macro which I can set for the largest value in a row to make it bold.

A quick MWE:

\documentclass{standalone}
\usepackage{siunitx}
\usepackage{times}

\newcommand{\maxf}[1]{\ensuremath{\mathbf{#1}}}

\begin{document}


\begin{tabular}{S[table-format=1.2]S[table-format=2.2]}
\sisetup{detect-weight=true,detect-inline-weight=math}
{A} & {B} \\
1.01 & \maxf{11.1}\\
\maxf{2.1} & 1.94 \\
\end{tabular} 

\end{document}

(I use times because the math font (CM) and text font (Times) are slightly different in my document ... this is just to highlight that need to format numbers in math mode.)

As I understand it, the following line should make siunitx listen for bold in math font.

\sisetup{detect-weight=true,detect-inline-weight=math}

But then I get the following:

bold not aligned

... the decimals are not aligned in either column.

If I instead try:

\newcommand{\maxf}[1]{\ensuremath{\boldmath #1}}

...with boldmath used in page 18 of the siunitx documentation (have never seen this command before), the numbers do not appear bold.


  • How can I define a macro (\maxf) to make single numeric cells bold in math mode and keep decimals aligned in siunitx?
  • If this is not possible or convenient, what highlighting options are available? For example, I notice that underlining using \newcommand{\maxf}[1]{\ensuremath{\underline{#1}} breaks decimal alignment.

EDIT, with Joseph's code below, I get the following (no bold):

enter image description here

EDIT3 This was because I had an older version of siunitx (v 2.3). I later upgraded to version 2.5 and it worked.


EDIT2 I solved my original highlighting needs by shading the cells of the max results instead.

\documentclass{standalone}
\usepackage{siunitx}
\usepackage{times}
\usepackage[table]{xcolor}

\newcommand{\maxf}[1]{{\cellcolor[gray]{0.8}} #1}

\begin{document}


\begin{tabular}{S[table-format=1.2]S[table-format=2.2]}
\sisetup{detect-weight=true,detect-inline-weight=math}
{A} & {B} \\
1.01 & \maxf{11.1}\\
\maxf{2.1} & 1.94 \\
\end{tabular} 

\end{document}

enter image description here

badroit
  • 7,557
  • 1
    I don't have enough rep to do anything other than leave a new answer, so I apologize if this is out of place. If somebody with more rep wants to repost this as a comment on Joseph's post, please feel free. I ran into trouble using the solution posted by Joseph as-is. However, siunitx provides a workaround for the problem of mathptmx not providing bold math mode numerals - you can specify mode=text in addition to your other sisetup options to –  May 22 '13 at 15:31

1 Answers1

39

The recommended way to do this is to use \bfseries

\documentclass{standalone}
\usepackage{siunitx}
\begin{document}

\sisetup{detect-weight=true,detect-inline-weight=math} \begin{tabular}{S[table-format=1.2]S[table-format=2.2]} {A} & {B} \ 1.01 & \bfseries 11.1\ \bfseries 2.1 & 1.94 \ \end{tabular}

\end{document}

Note that you need to set up the font detected outside of the first cell, as table cells form groups.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    @JosephWright -- there seems to be something weird going on: Your method works for C Modern, L Modern, mathpazo, txfonts, and the newtxtext/newtxmath font groups, but not if the mathptmx (Times Roman for both text&math) package is loaded; in the latter case, the \bfseries command simply doesn't seem to have an effect. – Mico Aug 07 '12 at 23:51
  • 1
    @Mico That depends on how the fonts are set up, and in particular if the font provides proper access to bold math mode numerals via \boldmath. If you try this with mathptmx, it does not, hence the issue. – Joseph Wright Aug 08 '12 at 07:03
  • @Mico: mathptmx explictly disables \boldmath. When using it you should get a warning in the log Package mathptmx Warning: There are no bold math fonts on input line 25. One could probably define a "poorbold" mathversion which emboldens only the numbers and letters but if it would work with siunitx depends on how siunitx choose or acknowledge a mathversion. Beside this: \boldmath is a command to make all the math in a formula bold and it should be issued before the math like this {\boldmath$a=b$}. – Ulrike Fischer Aug 08 '12 at 08:07
  • @UlrikeFischer Yes, that's quite true about how \boldmath works, and that's internally how siunitx sets it (basically \hbox <font set up>\ensuremath{<content>}). – Joseph Wright Aug 08 '12 at 08:11
  • @JosephWright Does siunitx acknowledges only boldmath or could I tell it to use a special \mathversion{fancy}? – Ulrike Fischer Aug 08 '12 at 08:25
  • @UlrikeFischer - many thanks for explaining the situation with mathptmx. I was completely unaware of this font not allowing bold in math. – Mico Aug 08 '12 at 11:35
  • 2
    I need to use detect-all: detect-weight=true,detect-inline-weight=math was not sufficient, content do not become bold. – audeoudh Oct 11 '19 at 11:33
  • As of 2021, it's not longer necessary to "robustify" \bfseries. The answer could be updated to reflect that. – Faheem Mitha Sep 07 '21 at 18:34
  • It doesn't depend on the current year but (obviously) your LaTeX version. For those < 2021, I couldn't reproduce this solution and still needed \robustify\bfseries as mentioned in the siunitx manual. Then it worked. @audeoudh: It would be good to know which part you replaced by detect-all. – Marius Hofert Nov 21 '21 at 13:43
  • I also had to use \sisetup{detect-all=true} because the proposed solution only resulted in bold numbers but the actual units were still not bold, e.g., the result looked like \textbf{123} GB – mxmlnkn Jan 06 '23 at 16:13