# Include filename in a nested document

I have a large project split in multiple files. I'd like to get each subfile name in the pdf to monitor my document.

From different posts on this forum i understand Currfile package could help by using \currfilename. From this post, I get an answer that would be far too manual to implement since I have probably 100 subfiles, it would be far too long to add \currfilename to every one of them.

Is there a clever way to input the file name of the subfile that in being \input in Main.tex without having to type it for each subfile ? In other words, ws there a way to include \currfilename right after \input calls the subfile ?

\begin{filecontents}{File1.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File2.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File3.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File4.tex}
\currfilename
\lipsum
\end{filecontents}

\documentclass[10pt]{article}
\usepackage{currfile}
\usepackage{lipsum}

\begin{document}

\input{File1.tex}
\input{File2.tex}
\input{File3.tex}
\input{File4.tex}

\end{document}

• if you did not have \currfilename in the file isn't the required output just File1.tex \input{File1.tex} File2.tex \input{File2.tex} ? – David Carlisle Apr 24 '19 at 22:39
• Forgive my English David. I wish I had know about '\currfilename' before I wrote the 100 (actually more) subfiles. In MWE it was easy to implement the result I wanted. How could I avoid the burden of rewriting '\currfilename' in every single subfile ? – Julien-Elie Taieb Apr 24 '19 at 22:45
• The mwe would if it had over 100 similar {filecontents} be a simple internal find and replace on a key phrase such as the \begin{itemize} replace with \currfilename \begin{itemize} . However I suspect in reality your file is nothing like the mwe structure (is it ?) so I then have to query could you not do a find and replace in each of the real \inputs. to give an accurate answer requires knowing it is a true reflection of the question. – user170109 Apr 24 '19 at 22:55
• @KJO I thought the purpose of MWE was actually to show a simplified version of my problem ? My problem is I have many subfiles. They'll all be '\input{Subfile1},...,\input{Subfile100}' from my main.tex. I am just trying to know if there is a way to force '\input' to include the file name I am inputing right before it parse the text of this file. – Julien-Elie Taieb Apr 24 '19 at 23:02

You linked to the answer but just to be clear try this in the editor cautiously find and replace \input{ with \inputf{

As queried this will affect the editors ability to jump to from subfiles the safest way to handle that would be to simply prepend all subfiles with the line \currfilename and I give two more risky alternative solutions below.

\documentclass[10pt]{article}
%\usepackage{currfile} % not required
\usepackage{lipsum} % for demonstration each file contains \lipsum[1]

\newcommand\inputf[1]{\texttt{#1} \par\nobreak\input{#1}} % filename before
%\newcommand\inputf[1]{\input{#1}} % Per David's comment, uncomment this line and comment the line above to "resume normal service"

\begin{document}

\inputf{File1.tex}

\inputf{File2.tex}

\inputf{File3.tex}

\inputf{File4.tex}
\end{document}


We could stick with the original request to add \currfile to all \inputs (thus retaining the natural workflow. so the main.tex stays the same with one added line)

\documentclass[10pt]{article}
\usepackage{currfile} %required for modified inputs
\usepackage{lipsum}  %for demo only

\begin{document}
\input{file1.tex} % note it is this name that the file has (lowercase file)
\input{File2.tex} % note in the screenshot this is the name that \input \currfilename uses, however the real file name is lowercase file..
\input{File3.tex}
\input{File4.tex}
\end{document}


each of the tex files is prepended with a header stored in a file say currfile.txt containing

\texttt \currfilename \par


On windows this can be easily processed to the start of each txt file by using a AddCurrfile.cmd file in the same directory. (you run this at your own risk I have included a safety backup on first line but always test on a copy folder.)

for %%f in (file???.tex) do ren %%f %%~nf.bak
for %%f in (file???.bak) do copy currfile.txt/B+%%f/B %%~nf.tex/B


To revert the tex files to previous behaviour you could script the removal/rename HOWEVER I did say keep a backup folder without the changes did I not ?

Finally probably the solution you were looking for but its somewhat risky since it modifies a major command thus may impact other \inputs and I thus cannot control how you apply it.

% changing the syntax of a major command is not a good approach, so better to consider sticking with one of the two above solutions: