[Tex/LaTex] pdflatex / xelatex -output-directory and subdirectories when using \include

auxiliary-filescompilingfolders

When I include a file from a subdirectory with \include{subdir/sub} and compile the file with pdflatex -output-directory .tex_test/ main.tex, pdflatex complains, that it can't write to .tex_test/subdir/sub.aux. (Same happens with xelatex)

Is there a way to automatically create the necessary file structure in the output-directory? Of course it would also be ok, if sub.aux would be written directly to .tex_test/, but I guess that is not possible…

Full MWE:
./main.tex contains:

\documentclass{article}
\begin{document}
Main Document

\include{subdir/sub}
\end{document}

and ./subdir/sub.tex contains just anything. More importantly the directory .tex_test/ exists.

Best Answer

Just recreate in the invisible directory the same tree as your "visible" one: I tried and creating a subdir directory inside .tex_test worked.

Actually I changed the names, but it's just the same. Here's the log

This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012)
 restricted \write18 enabled.
entering extended mode
(./canasub.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2012-05-30, ngerman-x-2012-05-30, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, friulan, galician, german, ngerman, swissgerman, monogreek, greek, h
ungarian, icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, ma
rathi, oriya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, 
kurmanji, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk,
 polish, portuguese, romanian, romansh, russian, sanskrit, serbian, serbianc, s
lovak, slovenian, spanish, swedish, turkish, turkmen, ukrainian, uppersorbian, 
welsh, loaded.
(/usr/local/texlive/2012/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2012/texmf-dist/tex/latex/base/size10.clo))
(.canaout/canasub.aux (.canaout/canasub/sub.aux)) [1{/usr/local/texlive/2012/te
xmf-var/fonts/map/pdftex/updmap/pdftex.map}] (./canasub/sub.tex) [2]
(.canaout/canasub.aux (.canaout/canasub/sub.aux)) )</usr/local/texlive/2012/tex
mf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on .canaout/canasub.pdf (2 pages, 13249 bytes).
Transcript written on .canaout/canasub.log.

As you see, the sub.aux file is read in from .canasub/canasub


With shell escape enabled, you can create the directory:

\documentclass{article}

\makeatletter
\let\latex@include\include
\def\include#1{\include@aux#1\@nil}
\def\include@aux#1/#2\@nil{\@mkdir{#1}\latex@include{#1/#2}}
\ifnum\pdfshellescape=\@ne
  \def\@mkdir#1{\immediate\write18{mkdir -p .tex_test/#1}}
\else
  \def\@mkdir{\typeout{Expect errors}}
\fi

\begin{document}
Main Document

\include{canasub/sub}
\end{document}

Here I assume a Unix system (I don't know how to create a directory on Windows) and that all \include commands have the same structure

\include{subdir/file}

It should be quite easy to cope with different patterns. Of course you have to add -shell-escape to the call of pdflatex.

However the .tex_test part must be hard encoded, as TeX has no internal knowledge of the output directory.

Related Question