TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a package that saves information to an external file. I would like to name that file according to the source one; something like foo.tex -> foo.bar But I cannot find any variable that contains 'foo'. I am pretty sure that there must be one because the table of contents is saved to foo.toc.

Any pointers?

share|improve this question
For this purpose \jobname is always the right way to go. If you want to get the real main file name, which can be different from the job name if it is changed using the -jobname compiler argument, then see Macro to retrieve the real name of the source file if pdflatex is invoked with -jobname – Martin Scharrer May 8 '12 at 7:53
up vote 24 down vote accepted

You want the macro \jobname. This isn't quite the filename; to quote from the TeX FAQ:

TeX retains what it considers the name of the job, only, in the primitive \jobname; this is the name of the file first handed to TeX, stripped of its directory name and of any extension (such as .tex). If no file was passed (i.e., you're using TeX interactively), \jobname has the value texput (the name that's given to .log files in this case).

So if you're processing foo.tex, \jobname will be foo (which, of course, is probably what you want anyway).

share|improve this answer
Curiously, TeX does retain the current line number, see inputlineno – Yossi Gil Feb 28 '11 at 3:58

You are looking for \jobname.

EDIT: To add some value to my answer - pages 288-289 of the LaTeX2e sources contain the definition of \@starttoc, the command that reads/writes ToC data from/to external files. This definition reveals the answer to your question.

share|improve this answer
Thank you for the link. It is always nice to have good sources. – Hector Dec 13 '10 at 1:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.