Set width of tabular column to the width of tabular numerals

Take the following code:

\documentclass{article}
\usepackage[oldstyle, tabular]{libertine}
\usepackage{array}
\newlength{\period}
\settowidth{\period}{9999--9999}

\begin{document}
\begin{tabular}{>{\raggedleft}p{\period}l}
2002--2005 & Put some text and numbers 0123456789 here
\end{tabular}
\end{document}


In this example, I've defined the width of the left column (which is set to be ragged left) to be the width of eight tabular (i.e. monospaced) numerals and a hyphen.

But what I actually want is to set the width of the left column as above, but use proportional numerals outside of the first column. The example below obviously doesn't work, since this will set all numerals in the document to be proportional:

\documentclass{article}
\usepackage[oldstyle, proportional]{libertine} % <- change is here only
\usepackage{array}
\newlength{\period}
\settowidth{\period}{9999--9999}

\begin{document}
\begin{tabular}{>{\raggedleft}p{\period}l}
2002--2005 & Put some text and numbers 0123456789 here
\end{tabular}
\end{document}


So my question is: How can I set the width of the first column to be based on tabular numerals, and then use tabular numerals in that column in the document, but use proportional numerals elsewhere?

If this is best done with fontspec, then I'm open to using that instead.

• why not use an l column, why use a p column here? Apr 20, 2015 at 15:18
• @DavidCarlisle Because I want the columns to line up with other tables in the same document in which the content of the first column doesn't match the width of 2002-2005. Apr 20, 2015 at 15:22

You can define a \tabfig command that chooses oldstyle digits in tabular format:

\documentclass{article}
\usepackage[
oldstyle,
proportional
]{libertine}
\usepackage{array}
\newlength{\period}

\newcommand{\tabfig}{\fontfamily{LinuxLibertineT-TOsF}\selectfont}

\AtBeginDocument{\settowidth{\period}{\tabfig 0000--0000}}

\begin{document}

\begin{tabular}{>{\raggedleft\tabfig}p{\period}l}
2002--2005 & Put some text and numbers 0123456789 here
\end{tabular}

\end{document}


Note the delaying of the measurement after LaTeX has issued \normalfont.

• Great! Based on this, I assume I can use the same trick with fontspec, i.e. \newcommand{\tabfig}{\newfontfamily{...}} and then use \raggedleft\tabfig? Apr 20, 2015 at 18:14
• @Sverre Not certainly with \newfontfamily: \newcommand{\tabfig}{\addfontfeatures{Numbers=Monospaced}} Apr 20, 2015 at 19:38
• I just tested, and it works fine with \newfontfamily\tabfig[Ligatures = {TeX}, Numbers = {OldStyle, Monospaced}]{Linux Libertine O} for me. I've learned from Khaled Hosny, I believe, that it's better to use \newfontfamily than to use \addfontfeatures. (I don't know what I was thinking in my previous comment suggesting \newcommand etc.). Apr 20, 2015 at 20:06