2

I have a table with values calculated with \directlua{}. They are formatted with siunitx columns.

I want to turn some values bold or change their color.

It appears that the output of \directlua{} macros are formatted like raw text, not as the siunitx S column should do.

MWE:

% Latex program : luaLatex
\documentclass{article}

% *** Fonts ***
\usepackage{fontspec}
\setmainfont{Tex Gyre Schola}[Numbers={Proportional,OldStyle}]
% *** Tabular figures ***
\newfontfamily\TLFfont{Tex Gyre Schola}[Numbers={Monospaced,Lining}]

% *** siunitx ***
\usepackage{siunitx}
% french typesetting, 3-figures blocks
   \sisetup{locale=FR,group-minimum-digits=4}
% font detection
   \sisetup{detect-all}

% *** Lua percent sign ***
% https://tex.stackexchange.com/questions/436979/problem-with-string-format-directlua-and-tex-sprint
\makeatletter
   \let\luaPercent\@percentchar
\makeatother
% *** Rounding value ***
\newcommand*{\roundingValue}{2}
% ***

\usepackage{xcolor}

% *** Tabular bold / siunitx ***
% https://tex.stackexchange.com/questions/66253/siunitx-bold-single-numeric-cells
\usepackage{etoolbox}
\robustify\bfseries


\begin{document}

\sisetup{mode=text,text-rm=\TLFfont,unit-mode=text}
\begin{tabular}{lSSr}
      &   {raw text value} &   {directlua value} &    \\
   basic text &   1231.45 &   \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))} &   right \\
   itshape &   \itshape 1231.45 &   \itshape \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))} &   wrong \\
   bfseries &   \bfseries 1231.45 &   \bfseries \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))} &   wrong \\
   color &   \color{red}1231.45 &   \color{red}\directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))} &   wrong \\
   tex.sprint &   &   \color{red}\directlua{n=1231.45 tex.sprint(string.format("\luaPercent.\roundingValue f",n))} &   wrong \\
   tex.cprint 11 &   &   \color{red}\directlua{n=1231.45 tex.cprint(11, string.format("\luaPercent.\roundingValue f",n))} &   wrong \\
\end{tabular}

\end{document}

I use an old style numbers font on purpose, to enhance the differences:

Image

The only right formatted text is when no modifier is applied.

What am I missing here?

2

The issue isn't directly related to \directlua, the same thing happens if you replace \directlua{...} with a macro which expands to a number. In both cases, siunitx has to look at the value after it has been expanded by TeX into the actual number, but the expansion code in siunitx is interrupted by the unexpandable commands \color, \itshape etc. You can avoid this by manually by manually adding (lots of \expandafters or) \expanded, to ensure that the value is fully expanded before siunitx sees the unexpandable command:

% Latex program : luaLatex
\documentclass{article}

% *** Fonts ***
\usepackage{fontspec}
\setmainfont{Tex Gyre Schola}[Numbers={Proportional,OldStyle}]
% *** Tabular figures ***
\newfontfamily\TLFfont{Tex Gyre Schola}[Numbers={Monospaced,Lining}]

% *** siunitx ***
\usepackage{siunitx}
% french typesetting, 3-figures blocks
   \sisetup{locale=FR,group-minimum-digits=4}
% font detection
   \sisetup{detect-all}

% *** Lua percent sign ***
% https://tex.stackexchange.com/questions/436979/problem-with-string-format-directlua-and-tex-sprint
\makeatletter
   \let\luaPercent\@percentchar
\makeatother
% *** Rounding value ***
\newcommand*{\roundingValue}{2}
% ***

\usepackage{xcolor}

% *** Tabular bold / siunitx ***
% https://tex.stackexchange.com/questions/66253/siunitx-bold-single-numeric-cells
\usepackage{etoolbox}
\robustify\bfseries


\begin{document}

\sisetup{mode=text,text-rm=\TLFfont,unit-mode=text}
\begin{tabular}{lSSr}
      &   {raw text value} &   {directlua value} &    \\
   basic text &   1231.45 &   \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))} &   right \\
   itshape &   \itshape 1231.45 &   \expanded{\noexpand\itshape \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))}} &   right \\
   bfseries &   \bfseries 1231.45 &   \expanded{\noexpand\bfseries \directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))}} &   right \\
   color &   \color{red}1231.45 &   \expanded{\noexpand\color{red}\directlua{n=1231.45 tex.print(string.format("\luaPercent.\roundingValue f",n))}} &   right \\
   tex.sprint &   &   \expanded{\noexpand\color{red}\directlua{n=1231.45 tex.sprint(string.format("\luaPercent.\roundingValue f",n))}} &   right \\
   tex.cprint 11 &   &   \expanded{\noexpand\color{red}\directlua{n=1231.45 tex.cprint(11, string.format("\luaPercent.\roundingValue f",n))}} &   wrong \\
   tex.cprint 12 &   &   \expanded{\noexpand\color{red}\directlua{n=1231.45 tex.cprint(12, string.format("\luaPercent.\roundingValue f",n))}} &   right \\
\end{tabular}

\end{document}

(Alternatively, the issue could also be fixed by printing \color or the font command through tex.print in the \directlua call instead of issuing them separately) enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.