5

I started to learning LuaLateX nowadays and I'd like to get some rules with increasing lengths, but I can't figure out why this simple code doesn't give the desired output:

\documentclass[11pt]{book}
\usepackage{xcolor}
\usepackage{luatex,pgffor, luacode, calc}
\begin{document}
\newcommand{\myrules}{
    \dimen0=2cm
    \luaexec{
      for i=1,4 do
      tex.dimen[0]=tex.dimen[0]*i
      tex.print("\\color{red}\\rule{\\dimen0}{3pt}\\par")
      end
       }
   }
\myrules
\end{document}
7

\luaexec is a wrapper for\directlua. It executes the Lua code and expands to:

\color{red}\rule{\dimen0}{3pt}\par
\color{red}\rule{\dimen0}{3pt}\par
\color{red}\rule{\dimen0}{3pt}\par
\color{red}\rule{\dimen0}{3pt}\par

Also \luaexec calls \directlua inside a group. Thus the local assignments of tex.dimen[0] are lost before TeX proceeds with the expansion result of \directlua.

If the rules should be 2cm, 4cm, 6cm, and 8cm, then there is also a flaw in the algorithm that yields 2cm, 4cm, 12cm, 48cm.

Example file for the first version:

\documentclass[11pt]{book}
\usepackage{xcolor}
\usepackage{luatex,pgffor, luacode, calc}
\parindent=0pt
\begin{document}
\newcommand{\myrules}{%
    \dimen0=2cm %
    \luaexec{
      for i=1,4 do
        tex.print("\\color{red}\\rule{", tex.dimen[0]*i, "sp}{3pt}\\par")
      end
   }%
}
\myrules
\end{document}

Result

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.