# latexmk -outdir with \include

I'm trying to get latexmk to compile a .tex file with external `\include{...}` references to a specific `build` directory so I don't pollute my directory tree with `.aux` files, etc.

However, it appears `latexmk` doesn't do as I'd expect when it compiles the included files. For example, consider a file `file1.tex` which has something like

``````%%%%%
\include{chapters/test}
%%%%%
``````

When I run

``````latexmk -pdf -pdflatex="pdflatex -interaction=nonstopmode -file-line-error" file1.tex
``````

everything works fine, but

``````latexmk -pdf -pdflatex="pdflatex -interaction=nonstopmode -file-line-error" -outdir=build file1.tex
``````

gives me the error

``````Latexmk: Missing input file: 'chapters/test.aux' from line
'No file chapters/test.aux.'
``````

If I don't have the `\include` statement, using `\input` instead, everything compiles nicely to the build directory as you would expect.

For reference, I'm working on Mac OSX w/ MacTeX, as I understand there are a few differences in the implementation of `latexmk` between MacTeX and MikeTeX... any help would be much appreciated, as I've been beating my head against this for a while now.

• you should never use the `.tex` extension when using `\include` just use `\include{chapters/test}` – David Carlisle Oct 12 '14 at 0:45
• Ah yeah, you're right. FWIW, that just slipped in as I was typing the question, my actual source file just uses `\include{chapters/test}` as you suggest! I updated the question accordingly :) – Neal Pisenti Oct 12 '14 at 1:32
• OK in that case I'll retype the comment I started to type before spotting the .tex:-) 999 times out of 1000 if people configure tex to write files in non standard places they have problems, you need to configure tex, bibtex, makeindex, everything, to find the files and it's never worth it:-) – David Carlisle Oct 12 '14 at 1:35

Actually, this isn't a `latexmk` problem. If you look at the `.log` file or at the actual screen output, you'll see that it is `pdflatex` that had a problem. If you run `pdflatex` from the command line with the `-output-directory=build` option, you will get exactly the same errors. The primary error is that `pdflatex` can't create the file `chapters/test.aux`.

The solution is simply to create a `chapters` subdirectory of your build directory.

(The messages you saw from `latexmk` are its summary of what it is currently programmed to treat as common significant warnings and errors. But it's an incomplete summary. In general, if you find an error and don't understand what to do about it, it's worth going through the `.log` file; that provides much more information than `latexmk`'s summary.)

• This sure sounds like a bug in pdflatex (not creating directories when they don't exist). – BenC May 4 '15 at 2:41
• To programmatically mirror the directory structure, you can add a line like `system ("find . -type d ! -path './.git*' ! -path './\$out_dir*' -exec mkdir -p \$out_dir/{} \\;");` (which is just the approximately appropriate shell command wrapped in Perl's `system` call) after the definition of `\$out_dir = <...>` in your local `.latexmkrc` file. Probably not a good idea for a global `.latexmkrc`, though - otherwise beware of that quick `latexmk test.tex` in your home directory. I agree that this is just a workaround, and a proper solution would require pdflatex to handle that situation gracefully. – akobel May 24 '16 at 21:26

As @john-collins wrote above, this is not an issue in `latexmk` but in your tex interpreter like `pdflatex`.

Another solution to your problem is redefining the `\include` command to create the output dir before actually executing `\include`.

My implementation in `lualatex`:

``````\ifluatex%
\directlua{%
function makeparentdirs(path_with_filename)
local mkdirp = require('mkdirp_lua/mkdirp')
mkdirp(path_with_filename)

require('lfs')
% -- delete lowest directory which collides with file name
if lfs.attributes(path_with_filename).mode == 'directory' then
lfs.rmdir(path_with_filename)
end
end
}%
\fi

\let\OLDinclude\include
\renewcommand{\include}[1]{%
\ifluatex%
\directlua{makeparentdirs(\luastring{\auxdirname} .. '/' .. \luastring{#1})}%
\fi%
\OLDinclude{#1}%
}
``````

This solution is not nice though, because:

• depending on both platform and command line switches, the directory for aux files may differ. You have to manually define it by running `\newcommand*{\auxdirname}{auxdir}` or similar before.
• it depends on a separate lua library from https://github.com/xpol/mkdirp.lua

This solution works fine with tikz' external library, which also requires the folders in outdir/auxdir to be created when using custom (not auto-numerated) directories.

There are two possibilities:

1. I also got these errors, when using the `\doincludeonly` from https://en.wikibooks.org/wiki/TeX/includeonly On this case, these errors can be safely ignored.

But, if you would like, you can make latex stop generate these errors, if you generate a full version of the document, before using the `\doincludeonly` commands.

Then, on the next times, it will not throw the errors until you clear your latex cache of the files.

2. Can also happen due to this bug How to report the --output-directory bug on pdflatex/latexmk on Tex Live? which does not create the output directory by himself. So, you are stuck with manually creating the directories.

You can use this I can't write on file 'foo.aux' to create the directories structure by yourself.

You can also see this Why latex does not tell me which command is undefined? to learn more about other bugs.