How can I pass the content of the TeX variable \jobname to a Lua variable?

(I am using LuaLaTeX with the luacode package.)

  • 2
    I'm not clear on what you want: Mico's answer seems sensible, but that seems almost trivially-obvious so I wonder if there is more to the question.
    – Joseph Wright
    Commented Aug 30, 2014 at 8:53
  • Indeed, <code>\luadirect{p = \luastring{\jobname}}</code> does what I want. Many thanks to Mirco. Commented Aug 30, 2014 at 9:38

2 Answers 2


Since you're loading the luacode package, you could make use of its macro \luastring. (Yes, the name of the tex file in this example really is uvw.)

enter image description here

% !TEX TS-program = lualatex
function foo(s) -- "s" is a variable
  tex.sprint( s )
The value of \verb+\jobname+ is \directlua{tex.sprint(\luastring{\jobname})}.

The value of \verb+\jobname+ is \texttt{\directlua{ foo(\luastring{\jobname})}}.
  • I see no Lua variable here.
    – egreg
    Commented Aug 30, 2014 at 9:12
  • @egreg - the macro \luastring{...} creates a Lua variable, which gets operated on by tex.sprint in the MWE. I've modified the MWE to set up a function that explicitly operates on a variable s that contains the value of \luastring{\jobname}.
    – Mico
    Commented Aug 30, 2014 at 9:52
  • Can one combine it with dofile("...") somehow?
    – koppor
    Commented Mar 7, 2018 at 16:32
  • @koppor - Sure can! (a) Plunk the three lines of Lua code into an external file called, say, myfile.lua. (b) Run \directlua{dofile("myfile.lua")} in the preamble. (c) Run The value of \verb+\jobname+ is \texttt{\directlua{ foo(\luastring{\jobname})}} as in the example shown above. If this instruction needs to be run several times, create a shortcut LaTeX macro, e.g., \newcommand\printjobname{\texttt{\directlua{foo(\luastring{\jobname})}}} in the preamble (and after running dofile); then, just type The value of \verb+\jobname+ is \printjobname in the body of the document. Cheers!
    – Mico
    Commented Mar 7, 2018 at 17:19
  • 1
    Thus there is an even number :-)
    – Sebastiano
    Commented Mar 26, 2020 at 22:49

An addition to Micos answer, you can also access the original \jobname, which was active at the start of LuaTeX: It is available as tex.jobname. This will normally be the same as \jobname, except it doesn't change if \jobname is redefined:

% !TEX TS-program = lualatex
The value of \verb+\jobname+ is \directlua{tex.sprint(\luastring{\jobname})}, the ``real'' jobname is \directlua{tex.sprint(tex.jobname)}.


The value of \verb+\jobname+ is \directlua{tex.sprint(\luastring{\jobname})}, the ``real'' jobname is \directlua{tex.sprint(tex.jobname)}.

% Change back, otherwise LaTeX is unhappy

If the jobname is job, this will result in enter image description here

You must log in to answer this question.

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