siunitx 表,格式化 \directlua{} 输出

siunitx 表,格式化 \directlua{} 输出

我有一张表格,其中的值是用 计算出来的\directlua{}。它们被格式化为siunitx列。

我想将一些值变为粗体或者改变它们的颜色。

看起来\directlua{}宏的输出格式像原始文本,而不是siunitx S列应该的那样。

梅威瑟:

% 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}

我特意使用了旧式数字字体,以增强差异:

图像
(来源:toile-libre.org

只有未应用任何修饰符时,文本格式才正确。

我在这里遗漏了什么?

答案1

该问题与 并不直接相关,如果您用扩展为数字的宏\directlua替换,也会发生同样的事情。在这两种情况下,siunitx 都必须在 TeX 将其扩展为实际数字后查看该值,但 siunitx 中的扩展代码被不可扩展的命令等打断。您可以通过手动添加(大量s 或)来避免这种情况,以确保在 siunitx 看到不可扩展的命令之前该值已完全扩展:\directlua{...}\color\itshape\expandafter\expanded

% 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}

(或者,也可以通过在调用中打印\color或字体命令来解决问题,而不是单独发出它们) tex.print\directlua在此处输入图片描述

相关内容