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 special macro \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:

  \def\ThisFile{\jobname}
  \newcounter{FileStack}
  \let\OrigInput\input
  \renewcommand{\input}[1]{%
    \stepcounter{FileStack}
    \expandafter\let
      \csname NameStack\theFileStack\endcsname
      \ThisFile
    \def\ThisFile{#1}%
    \OrigInput{#1}%
    \expandafter\let\expandafter
      \ThisFile
      \csname NameStack\theFileStack\endcsname
    \addtocounter{FileStack}{-1}%
  }
(And similarly for \include.) The code assumes you always use LaTeX syntax for \input, 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. The bundle includes a fink.el that provides support under emacs with AUC-TeX.

fink.sty
macros/latex/contrib/supported/fink.tar.gz