78

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

5

10 Answers 10

91
+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.

Edit 2023: I do not recommend to use this options. In my opinion it only make compilation complicated and personnally I never used that.

4
  • 4
    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
    Commented Apr 25, 2013 at 20:25
  • 2
    Jeez, why doesn't pdflatex have this option.
    – Daniel C
    Commented Jun 15, 2020 at 17:12
  • 1
    @Daniel It does. You need to specify the option before the filename though.
    – Nicolas
    Commented Jan 26, 2021 at 0:52
  • I am afraid the option --output-directory=dir is not better than compiling the latex file from the directory dir. (e.g., running cd dir and pdflatex <or latexmk> ../filename.tex). I agree that the option is convenient. Is there a way to redirect the generated pdf file alone? Commented Jun 20, 2021 at 7:05
23

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

4
  • 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
    Commented Feb 4, 2013 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. Commented Feb 4, 2013 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. Commented Mar 12, 2015 at 12:00
  • Could we have an update for move.yaml for arara-v4?
    – cis
    Commented Jan 17, 2020 at 13:19
17

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.
2
  • Thanks a lot, I did not hear about latexmk before, it is fantastic and make life a little easier! Commented Jan 31, 2013 at 15:53
  • 11
    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
    Commented Mar 17, 2015 at 17:47
7

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; }
    
3
  • Where should I put the above code in Texstudio? Commented Mar 23, 2018 at 9:03
  • @NanashiNoGombe I don't know Texstudio, but I think you can't run complex bash scripts in it. You would either need to translate something like the above code into TS's scripting language, or call bash scripts from TS. I recommend that you ask this as a question. Commented Oct 3, 2018 at 13:08
  • @NanashiNoGombe Texstudio allows to give the explicit command for compilation. I am not sure whether we can given multiple commands there. Commented Jun 20, 2021 at 7:01
3

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.

2

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

2

The LaTeX build wrapper ltx2any may be a solution. It avoids most of the pitfalls of using a separate folder for the auxiliary files by actually performing the compilation in a separate folder and then copying back the results. The separate folder can be specified with

ltx2any -t path_where_you_want_the_auxiliary_files_to_go filename.tex 

For more options, see ltx2any --help.

1

I have created a python module for saving the auxiliary files that are generated by LaTeX and BibTeX engines into a desired folder. On my GitHub site are more informations: https://github.com/amrane99/LaTeXCompiler. Once the module has been installed (using pip) you can simply compile the .tex file using pdflatex and no BibTeX engine as follows -- the generated files will be stored in the .aux_files folder --:

LaTeXCompiler -file <full_path_to_file>.tex 
              -tex_engine pdflatex -no_bib_engine -aux_folder .aux_files

It is important to not set -aux_folder .aux_files as -aux_folder .aux since this will interfere with the algorithm that copies the generated files into the folder, because the generated .aux file will have the same character sequence in it as the desired folder name, namely .aux and this is excluded, since the algorithm would try to move the folder into itself and might likely result in an error. A detailed description can be found here.

I hope this helps with the common issue of saving the auxiliary files in a separate folder so the working directory is always kept clean. This is very helpful for Mac users, since the -aux-directory=DIR does not work with the TexShop Editor.

0

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

1
-1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .