3

I have two tex files

% file1.tex (this is the inputing file)
\input file2
\bye

and

% file2.tex (this is the file input into file1.tex)
\printTheNameOfWhateverFileInputMe

What is working to show the first file name is

% file2.tex (this is the file input into file1.tex) \jobname %(this prints "file1")

but this would not work if file2.tex was to be included from file1.tex who is the principle job, hence coincides with \jobname not in such a situation

% file0.tex
\input file1
\bye

% file1.tex
\input file2

% file2.tex
\jobname %(shows "file0" instead of "file1")
  • Are you open to LaTeX solutions? – Werner Aug 14 '16 at 17:41
  • @Werner yes I think it would help me too – humanityANDpeace Aug 14 '16 at 17:42
4

currfile provides hooks into filenames:

enter image description here

\documentclass{article}

\usepackage{currfile,filecontents}

\begin{filecontents*}{file1.tex}
\verb|file1|: \currfilename

\input{file2}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
\verb|file2|: \currfilename
\end{filecontents*}

\begin{document}

\verb|file0|: \jobname .tex

\input{file1}

\end{document}

You can also capture \currfilename for use in another input file, effectively capturing the "parent file". This is provided by the [parent] option and using \parentfilename:

enter image description here

\documentclass{article}

\usepackage[parent]{currfile}
\usepackage{filecontents}

\begin{filecontents*}{file1.tex}
\verb|parent|: \parentfilename \par
\verb|file1|: \currfilename

\input{file2}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
\verb|parent|: \parentfilename \par
\verb|file2|: \currfilename
\end{filecontents*}

\begin{document}

\verb|file0|: \jobname .tex

\input{file1}

\end{document}
  • Very much appreciate. When reading the answer I fear I have been not expressing myself well enough. At least it seems to me in each file I am only ever able to track its own filename, which is good by itself.....Anyway it would be much better to be able to tell the filename of the file that \input the file currently parsed. In my examples for instance file2.tex should get the information file1.tex, since it has been input by it. – humanityANDpeace Aug 14 '16 at 17:52
  • @humanityANDpeace: You can capture \currfilename in another macro (say \parentfilename) to keep it... – Werner Aug 14 '16 at 18:15
  • thank you for adding to answer the example to help solve the question/problem. good idea – humanityANDpeace Aug 14 '16 at 19:23
  • @humanityANDpeace the package currfile define command \parentfilename and even more. – touhami Aug 14 '16 at 20:10
1

An implementation in expl3 that works over Plain TeX:

Main file hap.tex

\input expl3-generic

\ExplSyntaxOn

% we implement a stack
\seq_new:N \g_hap_filename_seq
\seq_gput_right:Nx \g_hap_filename_seq { \c_sys_jobname_str }

\cs_new_protected:Npn \sinput #1
 {
  \seq_gput_right:Nx \g_hap_filename_seq { #1 }
  \tex_input:D #1 \scan_stop:
  \seq_gpop_right:NN \g_hap_filename_seq \l_tmpa_tl
 }

\cs_new:Npn \currentfile
 {
  \hap_parent:n { -1 }
 }
\cs_new:Npn \parentfile
 {
  \hap_parent:n { -2 }
 }
\cs_new:Nn \hap_parent:n
 {
  \seq_item:Nn \g_hap_filename_seq { #1 }
 }

\ExplSyntaxOff

current file: \currentfile, parent file: \parentfile

\sinput{hap-first}

\bye

Secondary file hap-first.tex

current file: \currentfile, parent file: \parentfile

\sinput{hap-second}

Secondary file hap-second.tex

current file: \currentfile, parent file: \parentfile

Output

enter image description here


With just Plain TeX:

\newtoks\filestack
\global\filestack={{\jobname}{}}

\def\sinput#1{%
  \edef\sinputtemp{{#1}\the\filestack}%
  \global\filestack=\expandafter{\sinputtemp}%
  %\immediate\write20{Stack: \the\filestack}% for debugging
  \input #1\relax
  \edef\sinputtemp{\expandafter\gobble\the\filestack}%
  \global\filestack=\expandafter{\sinputtemp}%
  %\immediate\write20{Stack: \the\filestack}% for debugging
}
\def\currentfile{\expandafter\firstitem\the\filestack\filestack}
\def\parentfile{\expandafter\seconditem\the\filestack\filestack}
\def\gobble#1{}
\def\firstitem#1#2\filestack{#1}
\def\seconditem#1#2#3\filestack{#2}

current file: \currentfile, parent file: \parentfile

\sinput{hap-first}

\bye
  • It is a delight that the buzz-word "stack" is found in this answer. I hope that maybe there will be a way to extract the stack-building technique and unlink it from a dependency, i.e hopfully it will allow to design an pure plain tex solution. than you for the intermediated step – humanityANDpeace Aug 15 '16 at 9:10
  • @humanityANDpeace Here it is. :) – egreg Aug 15 '16 at 11:39
  • great addition to the answer, I am still at deciphering it and then hope to learn from it, thank you – humanityANDpeace Aug 15 '16 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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