# How to select monospaced numerals for tables only when using mathspec?

For one of my documents I used OldStyle numerals in regular text. For math I use Lining numbers. For tabular material I would like to use monospaced lining numbers if these numbers are math-related (I format those using the S column type from the siunitx package). However, up till now I haven't been able to get this last part to work. I'm using XeLaTeX with the mathspec package.

In the MWE below I first set the main font (Alegreya Sans) and the default (non-Monospaced) math font (as an aside, probably not relevant to this question I'm using Fira Sans for Greek letters, but the the other characters from Alegreya Sans).

In my partial solution as shown in the MWE I tell siunitx to treat the numbers as text and then switch the main font just before starting the tabular material. By putting this in curly braces and closing them before the caption, numbers in the caption look fine (OldStyle, non-Monospace). However, as you can see in the first column this also obviously changes the numbers in the first column, which I would like to keep OldStyle and non-Monospace.

Is there a way to simply switch math font somewhere in the text? The mathspec manual (and my experience) show that using \setmathsfont{} outside the preamble doesn't work. I found this and this answer which uses \setmathfont[version=something], but that requires the unicode-math package, which I don't need (and which seems to do more harm than good in my document, since I'm not planning to use Unicode symbols when typesetting math).

The MWE:

\documentclass{scrartcl}

\usepackage[]{mathspec}

\setmainfont[Numbers=OldStyle,BoldFont={Alegreya Sans Bold}]{Alegreya Sans}
\setmathsfont(Greek)[Numbers={Lining},BoldFont={Fira Sans Medium},Scale=0.90]{Fira Sans Book}
\setmathsfont(Digits,Latin)[Numbers={Lining},BoldFont={Alegreya Sans Bold}]{Alegreya Sans}
\setmathsf[Numbers={Lining},BoldFont={Alegreya Sans Bold}]{Alegreya Sans}
% The following is needed to get units typeset correctly when using \SI{}{}
\setmathrm[Numbers={Lining},BoldFont={Alegreya Sans Bold}]{Alegreya Sans}

\usepackage{siunitx}

\begin{document}

Table~\ref{tab:monospaced} should show monospaced, lining numbers. The
following numbers should not be monospaced and should be OldStyle:
0123456789. Testing siunitx: \SI{123}{mm}.
\begin{table}
\centering
{
\sisetup{mode=text}
\setmainfont[Numbers={Monospaced,Lining},BoldFont={Alegreya Sans Bold}]{Alegreya Sans}
\begin{tabular}{@{} lS[table-format=3]S[table-format=2]S[table-format=2]S[table-format=2.6] @{}}
Text & {People} & {Miles} & {Boats} & {Time}\\
\hline
Something & 999 & 75 & 13 & 12.3456\\
I want OldStyle here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle here 99 & 151 & 991 & 22 & 0.987611
\end{tabular}
}
\caption{This table should be correctly formatted. The math/siunitx
numbers should be Monospaced and Lining. These numbers in the
caption should be OldStyle: 0123456789.}
\label{tab:monospaced}
\end{table}

To verify the fonts aren't changed permanently,
Table~\ref{tab:defaultFont} shows the same table, but with default
fonts settings.
\begin{table}
\centering
\begin{tabular}{@{} lS[table-format=3]S[table-format=2]S[table-format=2]S[table-format=2.6] @{}}
Text & {People} & {Miles} & {Boats} & {Time}\\
\hline
Something & 999 & 75 & 13 & 12.3456\\
I want OldStyle here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle here 99 & 151 & 991 & 22 & 0.987611
\end{tabular}
\caption{A table with the default font settings for comparison.
These numbers in the caption should be OldStyle and not
Monospaced: 0123456789.}
\label{tab:defaultFont}
\end{table}
The following numbers should not be monospaced and should be OldStyle:
0123456789.
\end{document}


Based on @Henri Menke's suggestions in the comments I tried to wrap the tabular part in an \addfontfeatures block (as is done in section 7.3 of the fontspec manual). Looking at the output this results in Monospaced OldStyle numbers in column 1 (not exactly what I want, but acceptable). However, now the numbers in the other columns (formatted by siunitx) become Oldstyle (strange!) and are non-Monospaced. This may just be the undefined behaviour mentioned in the fontspec manual.

This is the code for Table 1:

\begin{table}
\centering
\sisetup{mode=text}
\begin{tabular}{@{} lS[table-format=3]S[table-format=2]S[table-format=2]S[table-format=2.6] @{}}
Text & {People} & {Miles} & {Boats} & {Time}\\
\hline
Something & 999 & 75 & 13 & 12.3456\\
I want OldStyle here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle here 99 & 151 & 991 & 22 & 0.987611
\end{tabular}
}
\caption{This table should be correctly formatted. The math/siunitx
numbers should be Monospaced and Lining. These numbers in the
caption should be OldStyle: 0123456789.}
\label{tab:monospaced}
\end{table}


In order to try and fix this I also removed the \sisetup{mode=text} line. As you can see below, this is almost getting where I want. The numerals in the first column are OldStyle , but Monospaced (not exactly what I want, but acceptable), however, the numerals in the other (math) columns are Lining (what I want), but non-Monospaced. So my conclusion is that the \addfontfeatures line only affects numbers in the main font, not in the math font.

\begin{table}
\centering
\begin{tabular}{@{} lS[table-format=3]S[table-format=2]S[table-format=2]S[table-format=2.6] @{}}
Text & {People} & {Miles} & {Boats} & {Time}\\
\hline
Something & 999 & 75 & 13 & 12.3456\\
I want OldStyle here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle here 99 & 151 & 991 & 22 & 0.987611
\end{tabular}
}
\caption{This table should be correctly formatted. The math/siunitx
numbers should be Monospaced and Lining. These numbers in the
caption should be OldStyle: 0123456789.}
\label{tab:monospaced}
\end{table}


• You should use \addfontfeatures{Numbers={Monospaced,Lining}} instead of again calling \setmainfont. – Henri Menke May 16 '16 at 15:06
• I haven't tested, but with the etoolbox package, you might be able to use \AtBeginEnvironment{tabular}{\addfontfeatures{Numbers={Monospaced,Lining}}}. – Henri Menke May 16 '16 at 15:07
• @HenriMenke, the reason why I didn't use \addfontfeatures is because the fontspec manual (section 7.3) says: "Note however that the behaviour in this regard will be unreliable (subject to the font itself) if you attempt to change an already selected feature", which I interpreted as "be careful when trying to set a feature that is mutually exclusive with another feature", so in order to avoid any possible mix up/undefined behaviour I chose to simple set the main font. – ph0t0nix May 18 '16 at 7:32
• Nevertheless I tried to wrap the tabular part of the table in a \addfontfeatures block. See my updated question. As for your \AtBeginEnvironment suggestion: yes, I am planning to use that once I figured out the exact details of the font problem. Thanks for mentioning it. – ph0t0nix May 18 '16 at 7:59
• I don't understand where the problem is: if you use the default mode=math for siunitx, don't you get what you want? – egreg May 18 '16 at 8:29