[comp.text.tex] How can I get the current input file name in TeX?

siemsen@sol.usc.edu (Pete Siemsen) (04/12/91)

Is there a way to put the name of the file currently being processed
into a LaTeX or TeX file?  I'd like something like \inputfilename that
puts the file name into my DVI file in the current font.

-- 
Pete Siemsen                         Pete Siemsen            siemsen@usc.edu
University of Southern California    645 Ohio Ave. #302      (213) 740-7391 (w)
1020 West Jefferson Blvd.            Long Beach, CA 90814    (213) 433-3059 (h)
Los Angeles, CA 90089-0251

wrtc@ciba-geigy.ch (Roland Tucci) (04/17/91)

In article <31860@usc>, siemsen@sol.usc.edu (Pete Siemsen) writes:
|>
|>Is there a way to put the name of the file currently being processed
|>into a LaTeX or TeX file?  I'd like something like \inputfilename that
|>puts the file name into my DVI file in the current font.
|>
|>-- 
|>Pete Siemsen                         Pete Siemsen            siemsen@usc.edu
|>University of Southern California    645 Ohio Ave. #302      (213)
|>740-7391 (w)
|>1020 West Jefferson Blvd.            Long Beach, CA 90814    (213)
|>433-3059 (h)
|>Los Angeles, CA 90089-0251
|>

The following code does it.

Hope this helps you

>>>----------------------------------------------------------------------<<<
%
% Inputfile style : Designed by R.Tucci, Ciba-Geigy AG Basle, June 29, 1990
%                   modified by R.Tucci Feb. 21, 1991
%
% The following commands are defined for internal use
%
% \@fileindex        : To keep the current level of \input{}
% \@iinput              : Redefines the \@iinput command of \latex.tex to
%                         save the filename of \input{...}
% \inputfilename         : Returns the filename depending on \@fileindex
% \@filenamei        : Contains the filename for \@fileindex=1
% \@filenameii       : Contains the filename for \@fileindex=2
% \@filenameiii      : Contains the filename for \@fileindex=3
% \@filename...      : ... etc.
% This Style should be used in the following manner
%
% 1. add this file to the \documentstyle[...]
% 2. use the command \inputfilename somewhere in the text and the current
%    input file name will be substituted
\typeout{filename environment loaded. WRTC , Version 1.1 22.02.91}
                        %
                        % Redefine \@iinput from latex.tex to keep the filename
                        % available
\newcount\@fileindex         % defines the pointer to the current filename
\@fileindex=0                %  and initialize it.
\def\@iinput#1{\advance\@fileindex by 1      % increment the pointer
                                % save the filename
        % the following line dose this Job:
        % 1. \expandafter prevents to redefine \csname, so
        % 2. \csname filename\romannumeral\@fileindex\endcsname
        %    expands to the control sequence \filename<x> where <x> is
        %    the roman character representation of \@fileindex. (e.g ii)
        % 3. \def defines the resulted control sequence of 2. to be #1
        \expandafter\def\csname filename\romannumeral\@fileindex\endcsname
{#1}
        \@@input#1                              % now read the file
        \ifnum\@fileindex>0                  % check for end of stack
        \advance\@fileindex by -1 \fi}       %   and decrement the pointer
                                % read the current filename if an \input
                                % has been performed, otherwise use \jobname
\def\inputfilename{\ifnum\@fileindex=0\jobname
                  \else\csname filename\romannumeral\@fileindex\endcsname
                  \fi}

raichle@informatik.uni-stuttgart.de (Bernd Raichle) (04/23/91)

wrtc@ciba-geigy.ch (Roland Tucci) writes:
> In article <31860@usc>, siemsen@sol.usc.edu (Pete Siemsen) writes:
> |>Is there a way to put the name of the file currently being processed
> |>into a LaTeX or TeX file?  I'd like something like \inputfilename that
> |>puts the file name into my DVI file in the current font.
>
> [Inputfile style deleted]
>

There is a better way to do this (and without nesting boundaries).
It uses a little trick to change a value locally without explicit
grouping (with {...} or \begingroup...\endgroup}:

\def\value{default}         % value must be expandable!

\def\doit#1{\def\temporary{#1}%
  \expandafter\ldoit\expandafter{\value}}

\def\ldoit#1{%
   \let\value=\temporary    % set to new value
   \doitoriginal            % ... do something ...
   \def\value{#1}}          % and reset it


Ok, here is the LaTeX style option:

-------------------- CUT HERE --------------------
% filename.sty   \filename contains the actual filename
%
% first version: 1 Mar 91 (br)
% last change  : 5 Mar 91 (br)
%
%
\typeout{Style option `filename' <5 Mar 91> (br)}

% default-value of \filename is \jobname.
% (\let is ok, because \jobname is one of the expandable primitives)
%
\let\filename=\jobname

% LaTeX saves TeX's \input-primitive in \@@input, we save it
% in \old@@input.
%
\let\old@@input=\@@input

% The macros \input and \include (and all internal \input's)
% expand the macro \@@input.
%
% First we save the argument (the new filename). Then we expand the current
% \filename with \expandafter as argument of \reset@filename, before we
% change the value of \filename.
% After the expansion with \expandafter's we have
%    \file@input \reset@filename{<current filename>}
% and in \file@name is #1, the <new filename>.
% After reading the file <new filename> with \file@input (we set \filename
% to <new filename>), we reset \filename to <current filename>.
%
% We can't use \begingroup ... \input ... \endgroup, because
% assignments in <new filename> shouldn't be local.
%
\def\@@input#1 {\def\file@name{#1}%
  \expandafter\file@@input
  \expandafter\reset@filename\expandafter{\filename}}

\def\file@@input{\let\filename=\file@name
  \old@@input\filename\relax}

%% Short form for...
%\def\reset@filename#1{\def\filename{#1}}
%% ...is...
\def\reset@filename{\def\filename}

\endinput
% -- end of `filename.sty'
-------------------- CUT HERE --------------------

-bernd
__________________________________________________________________________
Bernd Raichle, Student der Universit"at Stuttgart | "Le langage est source
privat: Stettener Str. 73, D-7300 Esslingen       |  de malentendus"
email:  raichle@azu.informatik.uni-stuttgart.de   |  (A. de Saint-Exupery)