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 – Robert Seifert Jul 13 '16 at 11:22
  • @ChristianHupfer - my MWE? but just with double compilation, right? – Robert Seifert Jul 13 '16 at 11:23
  • @thewaywewalk: Oh yes, I didn't catch that with the double compilation –  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
  • @JosephWright Alright, then I either use the 'lua side' as suggested by David (if it works) or just compile twice. – Robert Seifert Jul 13 '16 at 13:50

1 Answers1

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}
David Carlisle
  • 757,742
  • 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 – Robert Seifert 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 .. – Robert Seifert Jul 13 '16 at 15:08
  • @thewaywewalk: No the destination file didn't exist on my system (I made actually even a test with some fix name like "XXXXXX.pdf"). Also imho the callback is not meant for such manipulations and if it works it is an undocumented side-effect on which you shouldn't rely. I think it is better to start a dedicated additional run with a different jobname, see e.g. https://tex.stackexchange.com/questions/5228/can-one-tex-file-output-to-multiple-pdf-files/5265#5265 – Ulrike Fischer Jul 13 '16 at 15:24
  • @UlrikeFischer Good to know, that you see it as undocumented behaviour. I rather don't manipulate the jobname, as it produces too much unneccesary temporary files. But with two compilation runs, the lua approach in the question just works great. – Robert Seifert Jul 13 '16 at 15:30
  • Alrigtht, on my other computer with an up-to-date miktex, it doesn't work neither. – Robert Seifert Jul 13 '16 at 22:04
  • @thewaywewalk Ok it works for me with cygwin implementation but just a two line bat file that runs luatex then moves the file would be safer anyway;-) – David Carlisle Jul 13 '16 at 22:21
  • Could Arara move the file? System dependent, of course, but it seems to be anyway. – cfr Sep 06 '16 at 23:25