File name autocompletion in \include for large latex projects

I am a very lazy person, so sorry in advance if my question seems a bit odd.

Working on a thesis, I am splitting the content of the whole document into multiple .tex files, one for each chapter section – that’s a pretty standard approach for such case. The master .tex file simply gathers everything together:

...
\chapter{Introduction}
\label{chap:introduction}
\include{ch01_01}
\include{ch01_02}
...
\chapter{...}
...


Correspondingly, the folder contains all required files:

ch01_01.tex
ch01_02.tex
...
chXX_XX.tex
...
thesis.tex


All works great except one one thing: it becomes rather hard to navigate to a particular section only by remembering its title. Section sequence (or even chapter sequence) shuffles over time, plus the table of contents is not a good source of the numbers since some sections might be commented in the main file to increase rendering speed. So what you have to do is previewing several .tex files in your folder before picking the right one.

Obviously renaming the files into something like this will help:

ch01_01-motivation.tex
ch01_02-aims-and-objectives.tex
...
chXX_XX-importance-of-stackexchange-for-phd-students.tex
...


But such improvement also adds headache: you have to update every instance of \include every time you play with file names. Besides, for some strange reason latex silently passes non-existing files and carries on rendering the rest of the document, so it's quite easy to lose control of what you’re doing and just start wasting time.

What could be a solution here is a function like \smartinclude{ch01_01}, that would scan the folder for files matching ch01_01???.tex and include one if there is no ambiguity. Ideally it would be also useful to get a fatal error in any other case. I know that something like this exists out there — you can omit the extension of an image in \includegraphics but still get what you want. Unfortunately my knowledge of latex is too narrow to suggest anything similar for my ‘problem’ so I would be grateful if someone suggested the solution to it.

How can the function I’m looking for be implemented?

• not addressing your actual question, but you really shouldn't be using \include for sections, especially not when the \chapter command is in your main file. \chapter forces a new page, and \include also forces a new page, so the output almost certainly won't be paginated like you want it to be. you should most likely be using \input here instead. (this doesn't affect your naming objections, with which i sympathize, but have no help for.) – barbara beeton Feb 4 '14 at 21:29
• The missing file is not "silent" You get a warning on the terminal and log: No file nonexistent.tex. – David Carlisle Feb 4 '14 at 21:52
• If you may want to shuffle sections, not using numbers in the filename would help, and also as barbara commnted you should probably be using \input not \include (and then missing files are an error) – David Carlisle Feb 4 '14 at 21:53
• @DavidCarlisle the trick is that when I shuffle sections, e.g. swap ch01_01-motivation.tex and ch01_02-aims-and-objectives.tex, the main .tex file does not need to be changed. It still says \input{ch01_01} \input{ch01_02} (thanks for the correction BTW). If I want to show/hide a particular section, I look at the list of files and thus figure out what chXX_XX to comment out in .tex. – Alexander Kachkaev Feb 5 '14 at 1:29
• @AlexanderKachkaev whatever makes it easier for you, to me this just seems to obscure the document structure from your tex file and put it all in the filesystem, which seems the wrong way round. – David Carlisle Feb 5 '14 at 1:37

I don't think this is a good idea: the file names should not have a hardwired numbering.

I suggest a “labeling system”:

\makeatletter
\newcommand{\definefile}[2]{% #1 is a label, #2 is the file name
\global\@namedef{fn@#1}{#2}%
}
\input{\@nameuse{#1}}%
}
\makeatother

\definefile{01_01}{ch01_01-motivation}
\definefile{01_02}{ch01_02-aims-and-objectives}

\definefile{XX_XX}{chXX_XX-importance-of-stackexchange-for-phd-students}


\loadfile{XX_XX}


A proof of concept for your \smartinput command; this needs checking for existence and uniqueness of the file. Run with pdflatex --shell-escape on a Unix system:

\documentclass{article}
\usepackage{catchfile}

\newcommand{\smartinput}[1]{%
\CatchFileEdef\temp{|"ls |grep '^#1'"}{}%
\input{\temp}
}

\begin{document}
\smartinput{ch01_01}
\end{document}

• > the file names should not have a hardwired numbering I know that it's normally so, but in this particular case such approach works better than just having section files named ‘motivation.tex’, ‘objectives.tex’, etc. The biggest advantage is that files are ordered in a natural way, you literally get a TOC with numbers and titles and you are free to shuffle things around easily. The only problem I see is a need of repeating that title inside of each \input. – Alexander Kachkaev Feb 5 '14 at 1:36
• @AlexanderKachkaev The labeling mechanism I proposed should be as manageable as your (problematic) idea. – egreg Feb 5 '14 at 10:28