[Tex/LaTex] sans serif math in tables and siunitx

fontsmath-modesans-serifsiunitxtables

I want to know the easiest way to ensure that the content inside a tabular, including siunitx columns is set using a/the sans serif math font. The normal document is set in serif font.

This approach shows some of my attempts:

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{dcolumn}
\usepackage{siunitx}

\makeatletter 
% dcolumn column
\newcolumntype{d}[1]{>{\DC@{.}{ ,}{#1}}l<{\DC@end}}
% sans serif d-column (from http://www.torsten-schuetze.de/tex/tabsatz-2004.pdf)
\newcolumntype{j}[1]{%
>{\DC@{.}{,}{#1}\mathsf\bgroup}l%
<{\egroup\DC@end}%
}
% sans serif column, including math
\newcolumntype{k}{%
>{$\mathsf\bgroup}l%
<{\egroup$}%
}
\makeatother

\begin{document}%

% table with d-columns: fails to compile
\begin{tabular}{j{4.2}d{4.2}ll}
\hline
2.3456 & 2.3456 & 2.3456 & 2.3456 \\
34.2345 & 34.2345 & 34.2345 & 34.2345 \\
56.7835 & 56.7835 & 56.7835 & 56.7835 \\
\hline
\end{tabular}

\mbox{}\vspace{1em}\
% using siunitx commands instead
\sisetup{table-figures-integer = 2, table-figures-decimal = 4, math-rm=\mathsf}
\sffamily
\begin{tabular}{S
k
S[table-number-alignment = left]
S[table-number-alignment = right]}
\hline
{Some Values} & {Some Values} & {Some Values / \si{kg/m^2}} & {Some Values} \\
\hline
2.3456 & 2.3456 & 2.3456 & 2.3456 \\
{$\mathsf{a^2}$} & 34.2345 & 34.2345 & 34.2345 \\
56.7835 & 56.7835 & 56.7835 & 56.7835 \\
\hline
\end{tabular}

\end{document}%

The first tabular fails to compile. The second is set in sans serif except the \si{} command. However the code looks quite complicated. Appended is the result.

enter image description here

Best Answer

With siunitx, there are a couple of potential approaches. First, as Stefan has said you can detect the current font:

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{booktabs,lmodern,siunitx}
\usepackage{siunitx}

\newcolumntype{k}{%
>{$\mathsf\bgroup}l%
<{\egroup$}%
}


\begin{document}
\sffamily

\begin{center}
  \sisetup{table-format = 2.4,detect-family}
  \begin{tabular}{S
  k
  S[table-number-alignment = left]
  S[table-number-alignment = right]}
  \toprule
  {Some Values} & {Some Values} & {Some Values / \si{kg/m^2}} & {Some Values} \\
  \midrule
  2.3456 & 2.3456 & 2.3456 & 2.3456 \\
  {$\mathsf{a^2}$} & 34.2345 & 34.2345 & 34.2345 \\
  56.7835 & 56.7835 & 56.7835 & 56.7835 \\
  \bottomrule
  \end{tabular}
\end{center}

\end{document}

The second approach is to set the font used by siunitx for both text and math mode material to be sanserif

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{booktabs,lmodern,siunitx}
\usepackage{siunitx}

\newcolumntype{k}{%
>{$\mathsf\bgroup}l%
<{\egroup$}%
}


\begin{document}
\sffamily

\begin{center}
  \sisetup{table-format = 2.4, math-rm=\mathsf, text-rm=\sffamily}
  \begin{tabular}{S
  k
  S[table-number-alignment = left]
  S[table-number-alignment = right]}
  \toprule
  {Some Values} & {Some Values} & {Some Values / \si{kg/m^2}} & {Some Values} \\
  \midrule
  2.3456 & 2.3456 & 2.3456 & 2.3456 \\
  {$\mathsf{a^2}$} & 34.2345 & 34.2345 & 34.2345 \\
  56.7835 & 56.7835 & 56.7835 & 56.7835 \\
  \bottomrule
  \end{tabular}
\end{center}

\end{document}

In both cases, I've used table-format = 2.4 as a shortcut to set up the reserved space. This is often easier than doing things piece by piece.

Related Question