2

I'm using toggle switches to create different outputs with a single tex-file. However my output PDF is always named after the producing tex-file e.g. luaexample.tex produces luaexample.pdf.

Along with the toggles I now want to create a variable

\def\fileID{Z} 

which should be appended to the filename, to get e.g. luaexampleZ.pdf.

I thought using lua would be a create option to do so, and indeed the following almost works:

\documentclass{article}
\usepackage{luacode}

\def\fileID{Z}

\begin{document}
A random number:
\begin{luacode}
tex.print(math.random())
\end{luacode}

\begin{luacode}
   os.remove("\jobname\fileID.pdf")
   os.rename("\jobname.pdf", "\jobname\fileID.pdf")
\end{luacode}

\end{document}

But the PDF due to rename is created after \end{document} but the lua code section appears before. If I compile the document twice, it works, but the firstly compiled PDF is renamed. I'd like to avoid this double compilation.

If I put the lua code after \end{document} it is not executed.

So, how can I execute lua code or in general latex code after \end{document}?

  • this came up before somewhere, luatex list I think, I don't think it is possible, – David Carlisle Jul 13 '16 at 11:19
  • @DavidCarlisle using \renewcommand? That does not produce an output, just an aux file. But I think it would kind of crash quite a lot depending on the job name, wouldn't it? I just want to rename the final pdf, not all intermediate files. And I wouldn't know how to append a string to the \jobname – thewaywewalk Jul 13 '16 at 11:22
  • @ChristianHupfer - my MWE? but just with double compilation, right? – thewaywewalk Jul 13 '16 at 11:23
  • @thewaywewalk: Oh yes, I didn't catch that with the double compilation – user31729 Jul 13 '16 at 11:25
  • Can't be done from the 'TeX side': there is no callback executed after the PDF is closed. You have to do this by using a wrapper script to call LuaTeX (or whatever). – Joseph Wright Jul 13 '16 at 13:40
3

I think you can use the stop_run callback:

\documentclass{article}


\def\fileID{Z}

\begin{document}
A random number:


\directlua{
luatexbase.add_to_callback(
'stop_run',
function ()
 texio.write_nl("renaming output to " ..  "\jobname" .. "\fileID.pdf")
  os.rename("\jobname" .. ".pdf", "\jobname" .. "\fileID.pdf")
end,
"renamepdf"
)
}





\end{document}
  • The renaming does not work. In the office I'm still using the old luatex 0.8, if that could be the reason I'll try it tonight on another computer. I compile with texStudio lualatex.exe -synctex=1 -interaction=nonstopmode -enable-write18 %.tex – thewaywewalk Jul 13 '16 at 12:21
  • it worked for me with 0.95 not sure if synctex would have an effect – David Carlisle Jul 13 '16 at 12:25
  • @DavidCarlisle: Doesn't work for me neither on TL16 + windows. I can rename some other file with the code, but \jobname.pdf doesn't change. --shell-escape or synctex doesn't matter. – Ulrike Fischer Jul 13 '16 at 13:13
  • @UlrikeFischer as I said in comments originally I thought I remembered reading that no callback was late enough, but this works for me (cygwin64) but of course much of the file handling at this level is in the system dependent parts of the code. – David Carlisle Jul 13 '16 at 13:15
  • I found out, that it is absolutely essential to include os.remove("\jobname" .. "\fileID.pdf") before the os.rename in case the destination file could already exist, because the command is not executed. Maybe that's the problem for you as well @UlrikeFischer? I edit my question accordingly. The concatenation can be done directly, also. There is no need for .. – thewaywewalk Jul 13 '16 at 15:08

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.