34

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?

lockstep
  • 250,273
Hector
  • 1,100

4 Answers4

43

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).

David Carlisle
  • 757,742
  • Curiously, TeX does retain the current line number, see inputlineno – Yossi Gil Feb 28 '11 at 03:58
  • This is correct. Nevertheless, if you input a tex file into another, \jobname will hold the name of the file which is importing the other file. – gies0r Jan 20 '19 at 14:13
10

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.

lockstep
  • 250,273
9
\usepackage{currfile}
Filename: \currfilename
gies0r
  • 191
  • 1
  • 3
  • 1
    My use case was that I had a large main file with several other tex files included - this is what I needed! (the other suggestion gives only the "main.tex" file where I include the other .tex files. – user1941126 Aug 01 '21 at 09:47
1

In LuaTeX, the equivalent of \jobname on the lua side is tex.jobname. In LaTeX3 we have

\documentclass{article}
\begin{document}
\jobname\\
\ExplSyntaxOn
\g_file_curr_dir_str/
\g_file_curr_name_str.
\g_file_curr_ext_str
\ExplSyntaxOff
\end{document}

Some use case may benefit from the recent ltfilehook package which is built into LaTeX, one has \CurrentFile, \CurrentFilePath... available in the hook instructions.