4

LuaLaTeX: The desire is to maintain some parameters in a lua script and, from these, amongst other things, construct a \LoadClass statement which can be invoked from one's own class file.

I can get some "other" tex commands to work via a tex.print from the cls file's lua function call but, when attempting a LoadClass with some visually obvious cues (such as landscape) based on the article class, the compiler complains there is a Missing \begin{document} having invoked the lua function from the cls file. I get the impression that there are a limited number of things you can get lua to do in the cls file.

This is an up to date MiKTeX 23.5 on Windows 11 x64 running TeXstudio 4.6.3 using LuaHBTeX Version 1.17.0.

A minimal tex document:

\documentclass{class/base}
\begin{document}
document content

\uf{}
\end{document}

In the class subdirectory we have a minimal user base class:

\ProvidesClass{class/base}[2023-09-26 v1.0.1 Base Class]
\newcommand{\@base@class}{base}% 
\newcommand{\@base@pkgname}{cms-base}% 

\RequirePackage{currfile}

\RequirePackage{luacode}

\directlua{baselua = require('./class/baselua')}

\newcommand\userParms[1][]{%
    \directlua{
        local parm1 = 'landscape'
        local parm2 = '#1'
        tex.print(baselua.getTexContent(parm1,parm2))
        }
}

\userParms[\currfilename]

%\LoadClass{article}

Also in the class subdirectory is a trivial lua module:

local function returnsometex(p,v)
    return string.format([[\LoadClass[%s,twoside]{article}]].."\n"..[[\def \uf {%s}]],p,v)
end

return {getTexContent=returnsometex}

I'm 'expecting' (perhaps vainly) that the tex.printed LoadClass will be enforced so that, at compile time, it will then find the \begin{document} in the document's tex file.

1

1 Answer 1

4

Your problem is not related to \LoadClass but with the \n. tex.print expects every argument to be one line of input, it will not try to split the strings you pass to it into lines. Therefore \n (ASCII character 0x0A) does not have any special meaning here and will just be a regular character appearing in the middle of your line.

Normally \n serves as line terminator, so it never appears in the middle of a line (even though TeX allows you to add it there by writing e.g. This will^^0a also fail ). Therefore LaTeX does not set 0x0A up in any special way and TeX considers it to be just a regular character which will be typeset with the current font. Since you are before \begin{document}, trying to typeset \n fails (as any attempted typesetting fails at that point).

You can avoid this by passing the lines to tex.print as separate arguments:

local function returnsometex(p,v)
    return string.format([[\LoadClass[%s,twoside]{article}]], p),
           string.format([[\def \uf {%s}]],v)
end

return {getTexContent=returnsometex}
1
  • Thank you for this golden nugget of information. I have now completely re-coded my scripts to handle this based upon a "local" lua user options file containing 31 "options" in a nested table. Now debugged and working fully "as expected". Sep 19, 2023 at 14:10

You must log in to answer this question.

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