# Need *.aux file in separate folder

When compiling with pdflatex we need the *.aux files in a separate folder.

-
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count. This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). – N.N. Sep 29 '11 at 9:37
MiKTeX's pdf(ta)tex has a -aux-directory=DIR – Jhor Jul 15 '13 at 17:49

With MiKTeX and TeX Live you can use --output-directory=dir. This will put all output files including .log and .pdf/.dvi in this directory (and append it to the search path so that auxiliary files are found).

In MiKTeX you can additionally set --aux-directory=dir which will put only the auxiliary files in this directory.

-
I'm using MikTex/TeXworks on 64Bit-Windows7 and am experiencing issue described in OP. Where do I put this 'output-directory=dir' in the config.txt file? – zundarz Apr 25 '13 at 20:25

If you compile your document using arara, we can write a rule to move selected files to an arbitrary directory. This is my humble attempt with the move.yaml plain rule:

This answer was rewritten to comply with the new 3.0 version of arara. For arara 2.0, see the revision.

!config
# Move rule for arara
# requires arara 3.0+
identifier: move
name: Move
command: <arara> @{isFalse(file == getOriginalFile(), isWindows("cmd /c move /y", "mv -f").concat(' "').concat(file).concat('"').concat(' "').concat(target).concat('"'))}
arguments:
- identifier: target
flag: <arara> @{parameters.target}


The rule should work on all platforms, provided that the target directory exists. I could write a more complicated rule, but I don't think we need to make things difficult here. :)

Now, we need to add the move directive to our mydoc.tex document:

% arara: pdflatex
% arara: move: { files: [ mydoc.log, mydoc.aux ], target: stuff }
\documentclass{article}

\begin{document}

Hello world.

\end{document}


After compiling the document via pdflatex, arara will move the .aux and .log files to the stuff directory:

mydoc.tex
mydoc.pdf
stuff/
|- mydoc.aux
|- mydoc.log


We can also use other targets, say:

% arara: pdflatex
% arara: move: { files: [ mydoc.log, mydoc.aux ], target: '/home/paulo/Documents/stuff' }
% arara: move: { files: [ mydoc.pdf ], target: '/home/paulo/Documents/articles' }
\documentclass{article}
....


arara 3.0 has also a items iterator, so we could write a different move.yaml rule:

!config
# Move rule for arara
# requires arara 3.0+
identifier: move
name: Move
command: <arara> @{isFalse(isEmpty(item), isWindows("cmd /c move /y", "mv -f").concat(' "').concat(getBasename(file)).concat('.').concat(item).concat('"').concat(' "').concat(target).concat('"'))}
arguments:
- identifier: target
flag: <arara> @{parameters.target}


For this new rule, we simply provide the extension we want to move to the target:

% arara: pdflatex
% arara: move: { items: [ log, aux ], target: stuff }
\documentclass{article}

\begin{document}

Hello world.

\end{document}


After compiling the document via pdflatex, arara will move mydoc.aux and mydoc.log files to the stuff directory (the rule says that's the main file basename + the provided extension):

mydoc.tex
mydoc.pdf
stuff/
|- mydoc.aux
|- mydoc.log


Other rules can be written, but for now I think it's enough. :)

-
+1 very cool Paulo, nice demo and nice tool :) – cmhughes Feb 1 '13 at 4:26
Why not move.yaml is a part of arara by defauls? Also, can it create the folder stuff if it doesn't exist already? – Harish Kumar Feb 4 '13 at 0:06
@Harish: personally, I'm not fond of moving files, I prefer deleting them (hence the clean rule instead). :) Since arara 3.0 is now released in the wild, I'm thinking of creating a contrib/ folder in the project repository to keep other rules that aren't in the default set. :) About the directory creation, I need to take a look at the flags of each command; if stuff doesn't exist as a directory, the file will be renamed as stuff. – Paulo Cereda Feb 4 '13 at 0:12
+1 for the tool. It makes life easier. Thanks Paulo. – Harish Kumar Feb 4 '13 at 0:19
@PauloCereda ... moving files has the advantage that log files are kept when we need to search for errors. I would also vote for a move and for a feature to assure that the directory is created if not already there. – Bernd Gloss Mar 12 '15 at 12:00

You could also use latexmk to do the job for you. auxdir is the directory for all auxiliary files but not the PDF file.

latexmk -auxdir=/tmp test.tex should work. You can extend this to autocompile with latexmk -auxdir=/tmp -pdf -pvc test.tex

The manual man latexmk explains more about the auxdir parameter:

-auxdir=FOO or -aux-directory=FOO

Sets the directory for  auxiliary  output  files  of  (pdf)latex
(.aux,  .log  etc).  This achieves its effect by the -aux-direc‐
tory option of (pdf)latex, which currently is  only  implemented
on the MiKTeX version of (pdf)latex.

See   also   the   -outdir/-output-directory  options,  and  the
$aux_dir,$out_dir,  and  $search_path_separator configuration variables of latexmk. In particular, see the documentation of$out_dir for some complications  on  what  directory  names  are
suitable.

-
Thanks a lot, I did not hear about latexmk before, it is fantastic and make life a little easier! – PHPst Jan 31 '13 at 15:53
Nice idea, though as the documentation says, it only works with MikTex. If you have TexLive, not only are the files not put there, but latexmk expects to see them there and when it doesn't find them it returns an error. – Stephen Mar 17 '15 at 17:47

You can do this using the filesystem. Here's a suggested workflow, which I'll illustrate with Bash/Unix, but which can be made to work on Windows: in your working directory, where you edit your Tex file and want your output to be produced, you have build, which is either a subdirectory, or a symlink to some build directory elsewhere.

1. Set up the directory:
1. Either mkdir build in the directory where you want to work, or create the symlink to Bash. If you use a subdirectory, then most flavours of Tex will be able to access resources in the working directory, as it is a parent directory.
2. If $FILE is the basename of the Tex file you want to compile, then ln -s "$FILE".tex build
3. If you are creating PDF output, then ln -s build/$FILE.pdf . 2. To run the Tex command, you can just run it in the build directory. Or you can create a wrapper for the executable (untested): with the following shell function you can have aliases such as alias pdflatex="runtexinbuild pdflatex" which will run in the builddirectory if it is there, otherwise give the usual behaviour. runtexinbuild () { local cmd="$1" status; shift
if test -e build
then pushd build
command $cmd "$@"; status=$? popd exit$status
else command $cmd "$@"
fi; }

-

Here is a TeXnicCenter-specific solution which will hopefully seem sound enough to the people claiming it's bad practice to have one single, huge repository for all LaTeX auxiliary files (which it probably is).

On the Build->Define Output Profiles menu, choose your standard build profile (say, LaTeX => PDF) and copy it to a project-specific profile. (Here, Dissertation.) Then add --aux-directory=directoryname to the command line arguments passed to MikTeX.

When compiling path/file.tex, this will create files as path/directoryname/file.aux, and so on. This should be enough of a compromise, I think.

EDIT: to work with BibTeX, this requires %tm to be changed to ./auxiliary/%tm in the command line arguments passed to BibTeX. This can cause some trouble if external "chapter" files are \included but this can be fixed by using \input. I suspect there's some deep reason for this but I don't know if there is another way around it.

-

You can move the auxiliary files only after the "complete" PDF output is generated. The "complete" means that the cross-reference is properly typeset.

Moving the auxiliary files will be easier if you create a make file (in Linux) or a batch file (in Windows).

An example of batch file (for Windows user) that not only compiles the input file several times (3 times should be enough) but also moves the auxiliary files to a specified folder at the end.

rem batch.bat

echo off

rem %1 TeX input filename without extension

rem %2 The number of times to invoke pdflatex in draftmode

rem %3 Folder name to which the auxiliary files will be moved

if exist "%~1.pdf" del "%~1.pdf"

if exist "%~1.tex" for /l %%x in (1,1,%2) do pdflatex --shell-escape -draftmode -interaction=batchmode "%~1.tex"

if exist "%~1.tex" pdflatex --shell-escape "%~1.tex"

if not exist "%~3" mkdir "%~3"

for %%x in (aux log out toc nav snm) do (if exist "%~1.%%x" move "%~1.%%x" "%~3")


%1 represents the TeX input filename without extension, %2 represents the number of times to invoke pdflatex in draftmode, and %3 represents the folder name to which the auxiliary files will be moved.

Note that pdflatex will be executed %2 plus one times in total.

## Exercise:

Assume we have an input file named filename and we want to invoke pdflatex twice in draftmode and the auxiliary files will be moved to dir\subdir\.

-
3 times often isn't enough, much safer to check that the aux file is stable, see tex.stackexchange.com/questions/30674/… for an extreme example – David Carlisle Jan 13 '13 at 12:59

With TexMaker, you have a checkbox that allows you to put all the generated files in a separate folder named 'build'

-

You can use also Eclipse IDE together with TeXlipse plugin. It is quite easy to separate there your .tex files from all that are generated by TeX.

-