3

I can tex.print() to set the \title command of my document, but when I try tex.print('\\rohead{...}), nothing appears.. any suggestions?

\documentclass{scrartcl}
\usepackage[headsepline]{scrlayer-scrpage}
\usepackage{luacode}
\begin{luacode*}
tex.print('\\title{TITLE}')
tex.print('\\rohead{R-HEAD}') -- why is this not setting?
tex.print('\\global\\rohead{R-HEAD}') -- why is this not setting?
\end{luacode*}
\lohead{L-HEAD}
% \rohead{THIS WORKS}
\begin{document}
\maketitle
\clearpage
\section{Introduction}
\end{document}

0

1 Answer 1

3

The problem isn't caused by tex.print but by the surrounding luacode environment. If you use \luaexec (or even just \directlua) instead then you can set this. Your attempt to add \global didn't work since \global only really works with TeX primitive setting commands, not with LaTeX commands.

\documentclass{scrartcl}
\usepackage[headsepline]{scrlayer-scrpage}
\usepackage{luacode}
\luaexec{
tex.print('\\title{TITLE}')
tex.print('\\rohead{R-HEAD}')
}
\lohead{L-HEAD}
% \rohead{THIS WORKS}
\begin{document}
\maketitle
\clearpage
\section{Introduction}
\end{document}
5
  • Thank you. Can you explain why this a shortcoming of luacode? Commented Mar 14, 2022 at 3:17
  • @likethevegetable luacode adds a group around the whole thing. Therefore all local settings (including \rohead) are only active until the end of the group which occurs at \end{luacode*}. luaexec on the other hand also executes in a group, but the code written using tex.write and friends is moved out of this group and therefore can have a longer effect. \directlua avoids groups completely. Commented Mar 14, 2022 at 3:23
  • Thank you. Let's say that the \luaexec{} call occurred inside of the group. Is there any way to "free" it? Are you able to answer why the KOMA command is affected but not the (I assume more generic) \title? Commented Mar 14, 2022 at 3:38
  • @likethevegetable If you don't want the group, use \directlua. \luaexec by design changes the meaning of e.g. \`, so in order to undo this at the end it is restricted to a group. \title` is not affected since it is always global. That's mostly because you only want one title in your document, while you might want to change \rohead only for parts of a document and therefore want to restrict it to a group. Commented Mar 14, 2022 at 3:51
  • That's understandable. Although, since \rohead is performed in the pre-amble, it's unfortunate (for me) that it did not work as expected. For what it's worth, in my actual usage I made an environment to parse data and set document parameters. In my actual use, it was my environment that was causing the mishap and it was by chance that my MWE for SE used luacode and (luckily!) the mishap persisted.. I found that using etoolbox's \AfterEndEnvironment{myenv}{\luaexec{...}} worked. Commented Mar 14, 2022 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .