# 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. 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}}}. 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. 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. 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? May 18 '16 at 8:29

### Problems

1. siunitx overrides any other font settings because it gets the final say.
2. The OP's way of specifying fonts for tabular are incompatible because it asks for the math font to be proportional lining yet monospaced lining at the same time...
3. ...and they can't be changed because (from the fontspec manual):

When \setmainfont, \setsansfont and \setmonofont are used in the preamble, they also define the fonts to be used in maths mode inside the \mathrm-type commands. This only occurs in the preamble because LaTeX freezes the maths fonts after this stage of the processing. The fontspec package must also be loaded after any maths font packages (e.g., euler) to be successful.

### Solution

Having tried a variety of ways, the only solution I can find is to drop the conflicting requirements for math mode (body) to be proportional lining but math mode (table) to be monospaced lining. The neatest seems to be dropping the requirement to be proportional.

The code can be simplified:

### Minimal MWE:

\setmainfont[Numbers={OldStyle,Proportional},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
\setmathsfont(Greek)[Numbers={Lining},BoldFont={Fira Sans Medium},Scale=0.90]{Fira Sans Book}
\setmathsfont(Digits,Latin)[Numbers={Lining,Monospaced},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
\setmathsf[Numbers={Lining,Proportional},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
% The following is needed to get units typeset correctly when using \SI{}{}
\setmathrm[Numbers={Lining,Monospaced},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
\usepackage[mode=math]{siunitx}


### Full MWE:

\documentclass[oneside,11pt]{article}
\usepackage{mathspec}

\setmainfont[Numbers={OldStyle,Proportional},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
%\setmathsfont(Greek)[Numbers={Lining},BoldFont={Fira Sans Medium},Scale=0.90]{Fira Sans Book}
\setmathsfont(Digits,Latin)[Numbers={Lining,Monospaced},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
\setmathsf[Numbers={Lining,Proportional},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
% The following is needed to get units typeset correctly when using \SI{}{}
\setmathrm[Numbers={Lining,Monospaced},BoldFont={AlegreyaSans-Bold.otf}]{AlegreyaSans-Regular.otf}
\usepackage[mode=math]{siunitx}

\begin{document}

For one of my documents I used proportional OldStyle numerals in regular text:\\
x = 1234567890 111 888\\
x = 0123456789 888 111\\
But math should be proportional lining but as that's impossible we'll settle for monospaced lining:\\
\ensuremath{x = 1234567890 111 888}\\
\ensuremath{x = 0123456789 888 111}

For math I use Lining numbers. For tabular material I would like to use monospaced lining numbers if these numbers are math-related.

Table~\ref{tab:monospaced} should show monospaced, lining numbers. The
following numbers should be proportional and OldStyle:
1234567890. Testing siunitx: \SI{1234567890}{mm}.
\begin{table}[!hb]
\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 proportional here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle proportional 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 and proportional: 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. Given that OP's requirements and the changes I have made, the OP's typesetting for Table~\ref{tab:defaultFont} should be the same as Table~\ref{tab:monospaced} and the comment To verify the fonts aren't changed permanently' is obsolete.
\begin{table}[!hb]
\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 proportional here 12 & 111 & 54 & 11 & 0.111456\\
I want OldStyle proportional 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 Proportional: 0123456789.}
\label{tab:defaultFont}
\end{table}

For one of my documents I used proportional OldStyle numerals in regular text:\\
x = 1234567890 111 888\\
x = 0123456789 888 111\\
But math should be proportional lining but has to be monospaced lining:\\
\ensuremath{x = 1234567890 111 888}\\
\ensuremath{x = 0123456789 888 111}
\end{document}


### Update

Given that math font and its features are fixed in the preamble, I guess you could use combinations of \text with \addfontfeatures to adjust the appearance of numbers in running text to emulate the math style.

### Version

This is currently working on

• XeTeX Version 3.14159265-2.6-0.999992 (MiKTeX 2.9.7440 64-bit)
• mathspec 2016/12/22 v0.2b
• siunitx` 2020/05/25