24

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

  • 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 – user31088 May 22 '13 at 15:31
28

The recommended way to do this is to use \bfseries and make it robust using etoolbox

\documentclass{standalone}
\usepackage{etoolbox,siunitx}
\robustify\bfseries
\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.

  • 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 7 '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 8 '12 at 7: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 8 '12 at 8: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 8 '12 at 8:11
  • @JosephWright Does siunitx acknowledges only boldmath or could I tell it to use a special \mathversion{fancy}? – Ulrike Fischer Aug 8 '12 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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