4

The number of decimal places is "9" in the following example: string.format("\%0.9f", X).
How can one achieve the replacement of the fixed "9" decimal places with "NumberOfDecimalPlaces" (in LuaLatex) ?

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

enter image description here

3

The key here is to build the proper string in lua to pass to string.format. You want to change "\%0.9f" to "\%0." .. NumberOfDecimalPlaces .. "f" as .. concatenates strings in lua.

Changing your example gives the following:

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

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.