UK TeX FAQ -- question label filename

### What’s the name of this file

One might want this so as to automatically generate a page header or footer recording what file is being processed. It’s not easy…

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

This is fine, for the case of a small document, held in a single file; most significant documents will be held in a bunch of files, and TeX makes no attempt to keep track of files input to the job. So the user has to keep track, himself — the only way is to patch the input commands and cause them to retain details of the file name. This is particularly difficult in the case of Plain TeX, since the syntax of the `\``input` command is so peculiar.

In the case of LaTeX, the input commands have pretty regular syntax, and the simplest patching techniques can be used on them.

If you’re not inputting things from a file that’s already been input, the job is almost trivial:

>     \def\ThisFile{\jobname}
>     \renewcommand{\input}[1]{%
>       \renewcommand{\ThisFile}{#1}%
>       \OldInput{#1}%
>     }

With that, the macro `\``ThisFile` always contains the last thing input: it starts pointing at the base file of your document (`\``jobname`), and thereafter changes every time you use `\``input{`file`}`.

Most ordinary users will quickly become irritated with the simplicity of of the `\``ThisFile` mechanism above. The following code is more cunning: it maintains details of the files you’ve ‘come through’ to get to where you are, and it restores `\``ThisFile` to what the previous file was before returning.

>     \def\ThisFile{\jobname}
>     \newcounter{FileStack}
>     \renewcommand{\input}[1]{%
>       \stackinput{#1}{\OrigInput}%
>     }
>     \newcommand{\stackinput}[2]{%
>       \stepcounter{FileStack}%
>       \expandafter\let
>         \csname NameStack\theFileStack\endcsname
>         \ThisFile
>       \def\ThisFile{#1}%
>       #2{#1}%
>       \expandafter\let\expandafter
>         \ThisFile
>         \csname NameStack\theFileStack\endcsname
>       \addtocounter{FileStack}{-1}%
>     }

To do the same for `\``include`, we need the simple addition:

>     \renewcommand{\include}[1]{%
>       \stackinput{#1}{\OrigInclude}%
>     }

Both examples of patching `\``input` assume you always use LaTeX syntax, i.e., always use braces around the argument.

The FiNK (“File Name Keeper”) package provides a regular means of keeping track of the current file name (with its extension), in a macro `\``finkfile`. If you need the unadorned file name (without its ‘`.tex`’), use the commands:

>     \def\striptexext#1.tex{#1}
>     ...
>     \edef\ThisFile{\expandafter\stripext\finkfile}

The FiNK bundle includes a fink.el that provides support under emacs with AUC-TeX.

`fink.sty`
  macros/latex/contrib/fink (or browse the directory)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=filename