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

  • 15
    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
  • 1
    MiKTeX's pdf(ta)tex has a -aux-directory=DIR – Jhor Jul 15 '13 at 17:49
  • 1
    pdflatex --output-dir=temp mytexsource.tex is the one-line answer buried down. Here, temp subdirectory is used to store all intermediate files while compiling the file named mytexsource.tex – Loves Probability Jun 8 '16 at 8:55
up vote 69 down vote
+100

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.

  • 3
    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. :)

  • Why not move.yaml is a part of arara by defauls? Also, can it create the folder stuff if it doesn't exist already? – user11232 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
  • @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.

          If you also use the -cd option, and the specified auxiliary out‐
          put directory is a relative path, then the path  is  interpreted
          relative to the document directory.
  • 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
  • 6
    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; }
    
  • Where should I put the above code in Texstudio? – Nanashi No Gombe Mar 23 at 9:03

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).

enter image description here

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.

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

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\.

enter image description here

The LaTeX build wrapper ltx2any may be a solution. It places all the auxiliary files in a separate directory, which you can specify

ltx2any -t path_where_you_want_the_auxiliary_files_to_go filename.tex 

For more options, see ltx2any --help.

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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