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}
The following was added after the original question was asked.
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}
\addfontfeatures{Numbers={Monospaced,Lining}}{
\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
\addfontfeatures{Numbers={Monospaced,Lining}}{
\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}
Best Answer
Problems
siunitx
overrides any other font settings because it gets the final say.tabular
are incompatible because it asks for the math font to be proportional lining yet monospaced lining at the same time...fontspec
manual):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:
Full MWE:
Full MWE Output
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.2bsiunitx
2020/05/25