1

I have a document which compiles fine. I am trying to refactor it into separate files but the input command seems to be basing its paths relative to the build directory rather than the source directory.

My directory structure:

project/src/main.tex
project/build/

main.tex looks initially like this, simplified

\documentclass[a4paper,12pt]{book}
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\begin{document}
...
\end{document}

If I do this

$> cd project/build
$> pdflatex ../src/main.tex

then I get the desired output in project/build/ with no junk added to project/src, great.

Now move \renewcommand... into project/src/commands.tex and edit main.tex

\documentclass[a4paper,12pt]{book}
\input{commmands}
\begin{document}
...
\end{document}

Now when I run pdflatex it halts on the second line:

! LaTeX Error: File `commands.tex' not found.

If I edit main.tex thus:

\documentclass[a4paper,12pt]{book}
\input{../src/commands}
\begin{document}
...
\end{document}

then it compiles fine.

This is clearly idiotic. The source code should not care about the working directory of the compiler invocation.

How to I write main.tex so that it includes all other files relative to its own path (and for this behaviour to apply to includes-of-includes and so on) so that I can compile the document out-of-source?

  • 1
    As you found out, LaTeX uses its actual working directory as root for relative pathes. Of course, you could try to work with absolute pathes: \input{C:/project/src/commands} should do the trick, assuming that project/ is located in the C:-directory of your windows-system. By using absolute pathes, your documents is no more portable, as with relative pathes. – Jan Jan 5 '17 at 14:22
  • Nonsense, of course it's more portable with relative paths -- what if you want to share the project, or rename some parent directory? Requiring absolute paths is madness, and so is using the working directory as root. I can't believe there's no way to change this?!? – spraff Jan 5 '17 at 14:26
  • What do you mean with "Nonsense". I read it, that my comment makes "no sense". I doubt, that. – Jan Jan 5 '17 at 14:34
  • Maybe \usepackage{import} comes to the rescue – vaettchen Jan 5 '17 at 15:01
  • That is not how \input is defined. It may not be what you expected but it is clearly not idiotic, people have worked with it that way for over 30 years. (I would not use absolute paths just use the TEXINPUTS search path, just as \documentclass{book} (which is \input{book.cls}) just requires the local filename not a path. – David Carlisle Jan 5 '17 at 15:09
1

Just use

\documentclass[a4paper,12pt]{book}
\input{commands}
\begin{document}
...
\end{document}

or better call the file commands.sty not commands.tex and use

\documentclass[a4paper,12pt]{book}
\usepackage{commands}
\begin{document}
...
\end{document}

with a calling sequence of

cd project/build
TEXINPUTS=../src//:
pdflatex main.tex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.