I want to access labels from an external document -- in general this is not a problem with the xr package. But in this case both documents are compiled on sharelatex, and there \jobname is always set to output. So I'm wondering how to save the aux-file from the external document so that it isn't overwritten by the next compilation.

One idea I had is to redefine \jobname:

\def\jobname{testnewjobname} %better before the class ...



This seems to work fine in such a small example, but I'm wondering if it can explode in larger setups.


I made some tests on sharelatex and the idea seems to work fine. sharelatex writes and finds the auxiliary files. They are also deleted when one clears the cache of auxiliary files.

  • 2
    Probably this is going to depend on the exact situation you have. For example, you've defined \jobname here using catcode-11 chars, so at the very least I'd go with \edef\jobname{\detokenize{<new-name>}} (assuming e-TeX). – Joseph Wright Jun 26 '15 at 9:24
  • 2
    I believe that once TeX has decided the names of the main output file and of the log file and also \jobname, it doesn't look to \jobname any more. Macros can use \jobname, of course, but they'll use whatever \jobname expands to. – egreg Jun 26 '15 at 9:35
  • @egreg Indeed, that's true even in LuaTeX where you can alter tex.jobname in a callback: the core input/output names are unchanged. – Joseph Wright Jun 26 '15 at 9:38

TeX (any engine) decides the job name it will use for the main output and log files in the following way

  1. If a file name has been specified in the command line, that file name is used as job name
  2. If started in interactive mode, the first file that is \input at the ** prompt determines the job name
  3. However, if something needs to be written to the log file before the job name has been determined, or the primitive \jobname is accessed to, the job name is set to texput

Note that the -jobname command line option overrides the above steps and makes TeX set the job name to what's specified with the option.

When the job name has been established, the meaning of \jobname becomes the string (with characters of category code 12) representing the job name as determined before (spaces keep category code 10, however, provided the implementation of the TeX engine is able to cope with spaces in the file name).

Afterwards, no “internal” access to \jobname is made and it's impossible to change the file name of the main output file (DVI or PDF) or log file.

So changing the meaning of \jobname is possible. Macros will use the current meaning of \jobname, which however should be either the primitive or a macro expanding to a string.

For instance, LaTeX does \@input{\jobname.aux} when processing \begin{document} and then, unless \if@filesw returns false, \immediate\openout\@mainaux\jobname.aux.

Macros such as \makeindex will set up an output stream based on \jobname and so on. The main output file name is not available to TeX other than with the primitive \jobname.

Therefore I see no real problem in redefining it. However it should be a string (category code 12 characters) for uniformity and to avoid problems if some macro does a check on \jobname. Hence

\edef\TeXjobname{\jobname} % keep a copy just for safety

seems to be what you need, where WHATEVER stands for the string you'd like to use.

If e-TeX is not assumed, the second definition could be

\xdef\jobname{\expandafter\string\csname WHATEVER\endcsname}

As \jobname is an expandable primitive, there should be no difference in behavior, so long as the redefined \jobname is a macro expanding to a string.

Note that in pdfTeX and LuaTeX one can use \pdfprimitive\jobname for accessing the original meaning. It is \primitive\jobname in XeTeX. So saving the original meaning with the above code is really necessary with Knuth TeX.

  • Note that in LuaTeX both \pdfprimtive and \primitive are available (although the latter is currently called \luatexprimitive in the LaTeX format). Hans Hagen has said that the long-term plan for LuaTeX is to remove pdf from all non-PDF-related primitive names. – Joseph Wright Jun 26 '15 at 13:26
  • 1
    @JosephWright There must be a way to avoid this multiplication of names. ;-) – egreg Jun 26 '15 at 13:27
  • Thanks, and while I agree that it is possible I don't think that one should do it after some package or class has already used \jobname so it probably best to do it before \documentclass. – Ulrike Fischer Jun 26 '15 at 14:03
  • @UlrikeFischer Yes, I agree. – egreg Jun 26 '15 at 14:09

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.