以下示例中的小数位数为“9”:string.format("\%0.9f", X)。
如何用“NumberOfDecimalPlaces”(在 LuaLatex 中)替换固定的“9”位小数?
\documentclass{article}
\usepackage[nolinks,forget]{qrcode}%
\begin{document}
\newcommand{\PrintNumber}[1]{
\qrcode[version=5]{
\directlua{
X = #1
if X < 0.01
then NumberOfDecimalPlaces = 3
else NumberOfDecimalPlaces = 2
end
tex.sprint (string.format("\%0.9f", X))
}}}%
\PrintNumber{1}
%
% Doesn't work: ("\%0.NumberOfDecimalPlaces f", X)
% Doesn't work: ("\%0.NumberOfDecimalPlaces{f}", X)
% Doesn't work: ("\%0.NumberOfDecimalPlaces(f)", X)
% Doesn't work: ("\%0.{NumberOfDecimalPlaces}{f}", X)
\end{document}
答案1
这里的关键是在 lua 中构建正确的字符串以传递给string.format
。 您想更改"\%0.9f"
为在 lua 中连接字符串"\%0." .. NumberOfDecimalPlaces .. "f"
。..
更改示例可得到以下结果:
\documentclass{article}
\usepackage[nolinks,forget]{qrcode}%
\begin{document}
\newcommand{\PrintNumber}[1]{
\qrcode[version=5]{
\directlua{
X = #1
if X < 0.01
then NumberOfDecimalPlaces = 3
else NumberOfDecimalPlaces = 2
end
tex.sprint (string.format("\%0." .. NumberOfDecimalPlaces .. "f", X))
}}}%
\PrintNumber{1}
\end{document}