# \input with wildcards (regex) in the file name

I would like to \input all tex-files in a certain directory starting with "prefix", in alphabetical order. Something like \input{prefix*tex} would be nice, but it doesn't work.

Is there some workaround?

I would use your operating system, so for exanple

 ls prefix*tex | sed -e 's/$$.*$$/\\input{\1}/' > list-prefix.tex
pdflatex main


will make a file list-prefix.tex with one \input{..} per line, for each file matching prefix*.tex.

will input all the files if main.tex has \input{list-prefix} at some point.

If you only need to input (and never add headings etc, and want to avoid making a temporary file, you can use

\input{"|cat prefix*.tex"}


but then you have to process your main.tex with pdflatex --shell-escape or equivalent.

I've used unix shell commands here but the same works on windows if you substitute the appropriate commands (or use bash on windows, which is what I do personally)

You have of course to use the shell escape, because TeX has no wild card capability.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\xinput}{m}{%
\azimut_xinput:n { #1 }
}

\ior_new:N \g_azimut_xinput_stream

\cs_new_protected:Nn \azimut_xinput:n
{
\ior_open:Nn \g_azimut_xinput_stream { "|ls ~ #1" }
\ior_map_inline:Nn \g_azimut_xinput_stream { \file_input:n { \tl_trim_spaces:n {##1} } }
}
\ExplSyntaxOff

\begin{document}

\xinput{az-*.tex}

\end{document}


The ls command on Unix systems return the file list one element per line, so we can map the lines to \file_input:n.

You have to run this with

pdflatex -shell-escape


On Windows system the command to use might be different from ls.

I tested it with three files, az-a.tex, az-b.tex and az-c.tex. This is the terminal output showing that the files have been input.

(./azimut.aux) (./az-a.tex) (./az-b.tex) (./az-c.tex) [1{/usr/local/texlive/201
4/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (./azimut.aux) )</usr/local/te
xlive/2014/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on azimut.pdf (1 page, 10198 bytes).


This can easily be extended to do more things; for instance, you may want to type the file name:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\xinput}{m}{%
\azimut_xinput:n { #1 }
}

\ior_new:N \g_azimut_xinput_stream

\cs_new_protected:Nn \azimut_xinput:n
{
\ior_open:Nn \g_azimut_xinput_stream { "|ls ~ #1" }
\ior_map_inline:Nn \g_azimut_xinput_stream
{
\par\noindent\texttt{##1}\par\nopagebreak
\file_input:n { \tl_trim_spaces:n {##1} }
}
}
\ExplSyntaxOff

\begin{document}

\xinput{az-*.tex}

\end{document}


The files just contain one letter, for the sake of the example.