# Include/input every subfile from a subfolder

Our chemistry teacher gave us a collection of vocabulary cards which include every term we should be able to explain (looking forward for the final exams). Because just having those cards doesn't help a friend and I would like to create a glossary. At the moment the collections consists of about 180 terms. Considering the size of the final document we decided to go with LaTeX and to divide this project into single files so we could work on different terms very easily. The initial setup for the project looks like this:

Every term should get a single file in the entries subfolder. Is it possible to input all files with a single command into the main file? Is there something like \input{entries/*}?

EDIT:

The files should have the name of the term the explain. Therefore the files won't be numbered and have entirely different names.

If the files are numbered sequentially then you could use the pgffor package to insert them with a loop:

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \x in {1,...,10}{
\input{file\x}
}
\end{document}


This will input file1 to file10 into your document.

If the files have arbitrary names, you could still use a loop, but then each name would need to be specified in the \foreach command, e.g. \foreach \x in {worda,wordb,wordc} etc.

This answers your actual question, but there may be other solutions to the larger problem you are trying to solve, i.e., making a glossary. I would be more inclined to keep the entries in a spreadsheet and then use datatool to create the glossary, for example.

• Ok sorry, I forgot to mention that the names won't follow any rule. I edited the question. Mar 24, 2015 at 13:52

You didn't explain what order of the input files is excepted. If the order is the same as the order of the output of the Unix ls command and you are using bash command interpreter, then you can create the script runtex with the contents (for example):

#!/bin/bash
ls entries/* > entfiles
echo {} >> entfiles
pdfcsplain main.tex


Note: replace the pdfcsplain to the command you are running TeX. Probably it will be latex or something else. Finally, the main.tex file can include:

\def\inputfile#1 {\ifx^#1^\else \input#1 \expandafter\inputfile\fi}
\expandafter\inputfile \input entfiles


Then you can try to run the runtex command or bash runtex command.

All the solutions to this kind of problems that I have seen until now have recourse to shell scripts or temporary files created with \write18 and subsequently \input(ted). But it may be worth noting that it is also possible to use pipes instead of temporary files. Actually, I am not sure that the “piped input” feature works with \openin, as well as with \input, on all systems, but on mine (a MacTeX/TeXLive installation) it does.

To construct an (almost) minimal working example, create a file Main.tex placed alongside of a subdirectory Data, into which, in turn, you put, say, four files named Foo.tex, Bar.tex, Baz.tex, and AnotherFileName.tex. These four files could read as follows (for example):

Contents of Foo.tex:

\subsection{The \emph{Foo} file}
\lipsum[2]


Contents of Bar.tex:

\subsection{The \emph{Bar} file}
\lipsum[3]


Contents of Baz.tex:

\subsection{The \emph{Baz} file}
\lipsum[4]


Contents of AnotherFileName.tex:

\subsection{There is of course a problem\ldots}

\ldots~the order of file inclusion depends on the order in which they are
listed by the \texttt{ls} command; but I~am afraid this cannot be avoided!

\lipsum[5-8]


You are now ready to test the (A)MWE, which consists of the file Main.tex with the following contents:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

\@ifdefinable\@currentfilename{}
\newcommand*\InputAllFilesInSubfolder[1]{
% #1 <- Path to subfolder: MUST end with the (operating system dependent)
%       character for separating path components.
\openin\@fileliststream |"ls '#1'"\relax
\loop \unless\ifeof\@fileliststream
\begingroup
\endlinechar \m@ne % cf. exercise 20.18
\endgroup
\ifx\@currentfilename\@empty \else
\typeout{Inputting "#1\@currentfilename".}%
\input{#1\@currentfilename}%
\fi
\repeat
\closein\@fileliststream
}

\makeatother

\begin{document}

\tableofcontents

\section{Main section}
\lipsum[1]

\InputAllFilesInSubfolder{Data/}

\end{document}


In this case, I think that showing the output is not particularly meaningful.

Pretty obviously, however, the options of the ls command give you some control over the order in which the files are input(ted): you might want to try, e.g.,
  \openin\@fileliststream |"ls -rtU '#1'"\relax