# Input file appearing more then once after compiling?

I am using the function given in this answer https://tex.stackexchange.com/a/7657/110509 (with a minor alteration of ls #1/*.tex to ls #1/*/*.tex #1/*.tex so files in subdirectories also get included).

However if I run

latex -shell-escape -interaction=nonstopmode  Master.tex
pdflatex -shell-escape -interaction=nonstopmode  Master.tex


My file Master.dvi has no content table but the file Master.pdf has all the files included by the above function doubled (i.e. they all appear twice in the document). Every other combination of latex and pdflatex I have tried, with and without -shell-escape gives either no table of context, or files appearing more then once.

Does anyone know, firstly why this is happening? and secondly how I can get around it?

It seems to me that the function is running every time I compile it.

Edit

I have now had chance to play around with an example. Here is my Master.tex file:

\documentclass[a4paper,16pt]{article}
\makeatletter
\def\app@exe{\immediate\write18}
\def\inputAllFiles#1{%

\app@exe{ls #1/*/*.tex #1/*.tex | xargs cat >> \jobname.tmp}%
\InputIfFileExists{\jobname.tmp}{}
\AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}
\makeatother
\begin{document}
\title{Test}
\date{\today}

\maketitle
\newpage
\tableofcontents
\newpage

\end{document}


Where in the directory .../Main1/ I have ./Min1_1/File1_1.tex and ./Min1_2/File1_2.tex and in .../Main2/ I have ./Min2_1/File2_1.tex and ./Min2_2/File2_1.tex. The code in each of the e.g. File1_1.tex documents is very simple e.g.

\section{File 1.1}
Hi
\subsection{subsection 1}


Interestingly the code works fine if I comment out one of the \inputAllFiles functions and it seems to be the fact that I am using two (or more) of them that is causing the problems.

• Please provide a Minimum Working Example which can be used to recreate the problem. This needs details of your (MWE) directory hierarchy and the directory contents (obviously) in order for people to reproduce. – cfr Jul 28 '16 at 12:44
• Note that ls #1*.tex does not occur in the answer you linked to so it is not at all obvious what code you are actually running. Also, be sure to read the comments. The code in the answer definitely has bugs. – cfr Jul 28 '16 at 12:49
• @cfr Concerning your first comment, I been trying to make an example but every time I do it seems to work fine - I will look further into it. Concerning your second comment: That was a typo :) on my part, I have edited it in the question. – Quantum spaghettification Jul 30 '16 at 5:28
• @cfr See Edit, I think I have provided what you have asked for. Let me know if there is any further information that would be of use. Thanks. – Quantum spaghettification Jul 30 '16 at 9:57
• The rm command tries to delete an unknown file (in #1 directory). Try with rm -f \jobname.tmp... – Paul Gaborit Jul 30 '16 at 10:10

I can not test this because I'm using windows, so I ask you to test the following.

\def\inputAllFiles#1{%
% line 2
\app@exe{ls #1/*/*.tex #1/*.tex | xargs cat >> \jobname.tmp}% line 3
\InputIfFileExists{\jobname.tmp}{}                          % line 4
\AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}            % line 5


I see a problem in line 3: >> \jobname.tmp extends or creates the file \jobname.tmp in the current directory (that have not to be that directory containing the first subfiles ...) So how about creating this temporal file in the subdirectory it belongs to like >> #1/\jobname.tmp or simular as you did in line 5? Then there is no file \jobname.tmp in your second subdirectory ...

As far as I remember linux does with > \jobname.tex overwrite an existing file \jobname.tmp, but I'm not sure (didn't use linux for a long time). That could be an alternative for your command ...

Or you can just delete for example in line 2 every perhaps existing file \jobname.tmp (del \jobname.tmp > nul), because you called the command twice. In the original solution it was only called one time and therefore there could not be an older file \jobname.tmp.

As a quick fix (Note I am the OP), replacing the function with

\makeatletter
\def\app@exe{\immediate\write18}
\def\inputAllFiles#1#2{%

\app@exe{ls #1/*/*.tex #1/*.tex | xargs cat >> \jobname#2.tmp}%
\InputIfFileExists{\jobname#2.tmp}{}
\AtEndDocument{\app@exe{rm -f #1/\jobname#2.tmp}}}

\makeatother


And then using the input \inputAllFiles{pathtodir}{random_string}. Works, although I am still interested in what was originally going wrong and if the above code could be made more efficient e.g. by not needing the second argument.

• Well you are deleting the tmp-file only at end document, which means that your second call of \inputAllFiles appends the content of the second directory to the still existing tmp file with the content of the first directory. – Ulrike Fischer Aug 2 '16 at 13:29