2

I'm fairly new to LaTeX. I am using Overleaf for my thesis, and, in order for Overleaf to display my chapters in the right order, I've prepended a number next to each of them:

01-intro.tex
02-background.tex
etc.

Then, chapter-outline file looks like this:

\include{chapters/01-intro}
\include{chapters/02-background}

I was wondering if there was a way include a pattern instead of a filename. Something like:

\include{chapters/*-intro}
\include{chapters/*-background}

So if I change the number in the filename, the include will still work. Is there an existing command to do this? Not super important. Just curious. The documentation I've found doesn't seem to support it.

xdhmoore
  • 153
  • TeX in general can't search the filesystem without knowing the file name (unless you use the --shell-escape flag, or if you use LuaTeX, in which case something can be implemented) – Phelype Oleinik May 04 '21 at 22:57
  • This looks relevant: https://tex.stackexchange.com/questions/98203/can-i-test-if-a-file-exists – xdhmoore May 05 '21 at 00:42
  • 2
    Wouldn't it be easier to just remove the numbers from the file names if you're worried, the manually added numbers won't match the chapter numbers LaTeX assigns? – leandriis May 05 '21 at 07:43
  • Yes of course. But then Overleaf displays them in alphabetical order which is a tad less user friendly while I'm writing. I'm pretty new to LaTeX so I am just curious if there is an easy way to do it. – xdhmoore May 05 '21 at 17:59

1 Answers1

4

When you wite \tryinput{prefix*-name} then TeX will try to input file prefix01-name.tex or prefix02-name.tex or prefix03-name.tex or ... or prefix99-name.tex. First file which exists with such a name wins and it is read. If none such files exist then nothing is read.

You can define \tryinput macro by following code:

\newcount\tmpnum

\def\thetmpnum{\ifnum\tmpnum<9 0\fi\the\tmpnum}

\def\tryinput#1{\tryinputA#1\relax} \def\tryinputA #1*#2\relax{ \tmpnum=0 \let\nexti=\relax \loop \openin15=#1\thetmpnum #2 \ifeof15 \advance\tmpnum by1 \else \closein15 \edef\nexti{\noexpand\input #1\thetmpnum #2 }\tmpnum=100 \fi \ifnum\tmpnum <99 \repeat \nexti }

%% test:

\tryinput{chapters/*-intro}

wipet
  • 74,238