我有一個表,其中包含使用 計算的值\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
也會發生相同的情況。\directlua{...}
在這兩種情況下,siunitx 都必須查看被 TeX 擴展為實際數字後的值,但是 siunitx 中的擴展代碼會被不可擴展的命令\color
等\itshape
中斷。 siunitx 看到不可展開的指令之前該值已完全展開:\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