129

If I create just a basic hello.tex file an compile it using

pdflatex hello.tex

The pdflatex program is creating a plethora of file which is a little annoying. Currently it writes all those files about which I really don't care:

hello.aux, hello.log, hello.nav, hello.out, hello.snm, hello.toc

Is there a way to tell pdflatex to only write hello.pdf if the build is successful ? If not, can I specify the output directory of those files ? Maybe I could just write them in /tmp.

7
  • 24
    Sometimes you need those extra files. For example, aux keeps information about crossreferences and the like. They won't work unless latex can read that information from the aux file.
    – Seamus
    Feb 15, 2011 at 13:43
  • 7
    See this question and this question
    – Seamus
    Feb 15, 2011 at 13:45
  • 3
    Try using Gummi. You'll have just the .tex file and the .pdf file. If you check out the screenshots, the "Error Output" will let you look at the log. It just doesn't leave a file behind to clutter things up. Gummi is best for small documents since it is constantly compiling to give you an almost immediate view of what you're creating.
    – DJP
    Aug 13, 2011 at 21:45
  • May be you can try with arara: tex.stackexchange.com/a/87818/11232
    – user11232
    Dec 29, 2012 at 1:48
  • 1
    FWIW, ConTeXt stores all the auxiliary information in a single file filename.tuc. So overall, only two extra files are written: filename.tuc and filename.log. If you want, you can compile the document using context --purge filename.tex which will delete the .tuc and .log files at the end of the run.
    – Aditya
    Sep 15, 2014 at 22:17

20 Answers 20

112

You might not care about these files, but pdflatex does quite a bit. These files hold information collected during the first run(s) and are needed to build the final PDF with correct ToC, references, PDF bookmarks, etc.

Your can delete these files afterwards, e.g. manually or using a front-end tool like latexmk (-c option). However, future compilations of the PDF would then need again several compiler runs.

You can define an output directory for all files using the -output-directory command line argument of pdflatex. After compilation you can then move the PDF in the current directory.

With MiKTeX, you can specify a directory in which all the auxiliary files are put (but not the PDF output) by using the -aux-directory command line argument. You can even combine -output-directory and -aux-directory.

5
  • 30
    Why does LaTeX do this? Why doesn't it do the multiple runs in the background, keeping toc, references, bookmarks, etc. in memory instead of cluttering the filesystem? Oct 24, 2013 at 2:19
  • 18
    @JonathanBaldwin TeX (upon which LaTeX is built) was written in 1978, before the PC era. It was designed to work in resource constrained environments, and therefore uses explicit multiple passes instead of keeping stuff in memory (and then breaking everything like word does). Mar 30, 2014 at 20:51
  • 4
    Texmaker has an option called "Use a "build" subdirectory for output files" that will put all extra files in a folder called build automatically.
    – qwr
    Oct 17, 2016 at 5:02
  • 1
    @MartinScharrer How do you automate the moving of the output pdf to a separate directory? Is it possible? Thanks. Mar 23, 2018 at 9:10
  • Why is the MiKTeX behaviour of putting all aux-files into a separate folder not standard for the usual pdflatex-command? This would be so much cleaner!
    – exchange
    Dec 14, 2022 at 14:00
64

In addition to Martin's answer, I thought it might be useful to explain why LaTeX creates all these extra files. Let's take the example of the .aux file.

Let's say you have a \label in your document and a reference to it somewhere above where the label occurs. When pdflatex reads your .tex file, it reads the \ref first. Now, it doesn't know what to do with this ref: it hasn't yet encountered what it's referring to. Now when pdflatex reaches the label, it makes a note of what the label is referring to. By "makes a note" I mean it writes something to the .aux file that says roughly "when you encounter references to this, this is what is meant".

Then, on a second pdflatex run, when it reaches the reference, it looks in the .aux file and it knows what it is supposed to refer to and can substitute in the relevant text.

Auxiliary files are used for lots of other similar things (like tables of contents, lists of figures and so on). They are annoying, but deleting them after each run would break things. A lot.

5
  • 51
    I don't mean to raise a flamewar here, but maybe it's more fair to say that the .aux file is a kludge which is due to the current implementation of TeX. When I compile a C program, the compiler doesn't write a file with the memory offsets assigned to every variable, so it isn't clear why TeX should do something similar. Simply, it's an artifact from the past. Oct 6, 2011 at 11:17
  • 8
    Actually, the C compiler does create such file, it's the object file. Without knowing where the variables are, the linker would have pretty hard time linking everything together. If you run make clean, it usually remove these files for you, which is similar what you with do with tools like latexmk. You are right that in most cases with TeX, this could be resolved instead by TeX itself making multiple passes through the file, storing the data in memory. I believe I have seen someone trying to do something like that, but I don't really see why. Oct 23, 2011 at 16:23
  • 34
    The C compiler creates the .o file only if it is given an explicit command-line switch to do it. Otherwise it keeps all in memory and spits out only the executable file. Moreover, when you modify only a portion of your program, typically most .o files are unchanged, so they are effectively useful as a "compilation cache" to reduce work. In contrast, typically, when you add a paragraph at the end of a .tex file the references change and you have to compile twice, so there is no saving. Tell me, how many other languages do you know where you normally have to compile your file twice? Feb 15, 2012 at 8:35
  • 1
    You can use the .aux files as cache too, if you use for example \includeonly.
    – Juri Robl
    Jun 11, 2012 at 9:51
  • 4
    @FedericoPoloni While a C compiler can compile in a single pass (iff you don't want fancy optimizations), more modern languages can not be compiled in a single pass (e.g. Java or C#). In addition, only toy programs get compiled by passing all source files directly to the compiler. I can agree with you though that in this day and age there is little reason for TeX and LaTeX not to perform those passes by themselves. Mar 30, 2014 at 20:56
31

You already got lots of very good answers explaining why pdflatex needs all those auxiliary files. However you might still feel frustrated about having to live with all those files polluting a directory where (I'm guessing) you would like to keep all your LaTeX documents and their corresponding .pdf outputs.

The best solution is to keep one directory for each document you have.

You can keep, for example, a main Documents folder and then individual Paper1, Paper2, ... folders; each with their own main.tex file. Then you can happily let LaTeX store whatever auxiliary files it wants in their respective folders. The difference is that now, for you, there is a clear structure of where your documents are.

3
  • 3
    pdflatex also has a CLI option for -aux-directory=dir, so you could simply have all your aux files and such. I remember seeing an easy way to make an alias to do this with just the pdflatex command, but I can't find it Feb 15, 2012 at 18:02
  • 3
    @MercurialMadnessMan: The version of pdflatex I have under Debian Squeeze does not include an -aux-directory option. I think that is a MiKTeX option.
    – SabreWolfy
    Jun 3, 2012 at 12:31
  • 3
    I am the boss of my computer. If I don't want my computer to make these files, it should not make these files. I understand that in the 80-ies it was wasteful to recompute everything. Today, my computer can compile my 1-2 page much faster than all the time I lose from auxiliary file mess that has been created.
    – mnr
    Sep 28, 2018 at 15:31
29

TeX writes the .log file. It contains more information about processing the job than what is shown on the console. It's very useful for debugging.

LaTeX writes the .aux and .toc files. They are used for managing cross-references and table-of-contents information. Since TeX's organism digests the input document from beginning to end, once per job, there's no other way to have a part of the document change based on later content.

The beamer class writes .snm and .nav files. The .snm file is to assist you with including images of slides into an article version of the document. The .nav file assists in creating navigation bars on slides. Beamer is not apparently set up to suppress writing those files if they are not needed (i.e., if you do not need the functionality they enable).

The hyperref package writes the .out file to assist in creating bookmarks in the pdf file. Sometimes this isn't needed; I looked at the last few jobs I had which used hyperref and the .out files are empty. Again, this doesn't seem suppressable.

You can write the auxiliary files to a temporary directory. Then you'll have to instruct TeX to look in that temporary directory. Also you will have to make sure that the included auxiliary files are the right ones, not ones placed in the temporary directory by another process.

I think it's best to learn to live with these files. If you don't want them after you're "done" with writing the document, just delete them.

2
  • 3
    If everyone thought according to your last paragraph, we wouldn't even have the printing press let alone computers and LaTeX. Rejecting the status quo is the first step of progress. Oct 24, 2013 at 2:36
  • 3
    @JonathanBaldwin: Not all stati quo are created equal. IMO the work needed to force LaTeX to not use auxiliary files will not lead to inventions on the level of the printing press. Nov 1, 2013 at 15:23
20

This is quite an old post with many good answers but, surprisingly, none of them mention \nofiles. There are times when you create documents with:

  • no cross references (with \ref and \label)
  • no citations (with \cite)
  • no table of contents.

In these cases you don't want latex to create the long list of files mentioned above and you can tell it not to using \nofiles:

\documentclass{article}
\nofiles
\begin{document}
  Hello world
\end{document}

If you are running (pdf)latex "by hand" then the only files created will be the .log file and the .dvi (or .pdf file). If you are using something ike synctex then there will be a few more files for controlling the automatic compilation.

0
7

You can use this workaround as a rough guide because I am not sure whether you are a Windows user.

Step 1: create a batch file

rem batch.bat takes a input name without extension.
echo off

rem remove the previous PDF output to avoid confusion
rem when compilation fails.
del %1.pdf

pdflatex %1
pdflatex %1
rem list other programs such as
rem invoking bibtex, etc.

del %1.log
del %1.aux
del %1.nav
del %1.out
del %1.toc

rem other operations such as deleting or
rem renaming files.

Save it in a dedicated, safe folder such that reformatting hard disk will not lose it.

Step 2: set the PATH

enter image description here

Step 3: Configure TeXnicCenter

enter image description here

enter image description here

2
  • 1
    Two compiler passes may not be enough. In fact, theoretically there may be any number of LaTeX passes needed. Aug 6, 2011 at 3:50
  • @Andrey: Of course. The batch above must be adapted to suit our need. Aug 6, 2011 at 4:04
7

Solution for Emacs with AucTeX, just run

M-x TeX-clean

This does not get rid of any temporary directories that are created, just files

1
  • 4
    Or just run C-c C-c Clean. Note that C-c C-c Clean All deletes also DVI or PDF file. Jun 25, 2012 at 5:51
6

I have the (Mac) application Hazel watching my Articles folder and subfolders, with rules that delete all these auxiliary files after a certain interval since they were last modified. Usually it's two days or so. It cleans up files for papers I'm not currently working on.

Addressing the concerns about deletion raised above, if such files are needed in the future they can be created anew. Though this will require multiple runs, latexmk automatically runs the tex engine the necessary number of times, so they will be created.

1
  • Never new about Hazel - that's great!
    – Werner
    Aug 6, 2011 at 2:06
5

For those like-minded as me, and running linux:

#! /bin/bash

echo -n > .hidden
for i in *.out; do echo "$i" >> .hidden; done
for i in *.log; do echo "$i" >> .hidden; done
for i in *.aux; do echo "$i" >> .hidden; done
for i in *.bbl; do echo "$i" >> .hidden; done
for i in *.blg; do echo "$i" >> .hidden; done
for i in *.dvi; do echo "$i" >> .hidden; done
for i in *.toc; do echo "$i" >> .hidden; done
for i in *.synctex.gz; do echo "$i" >> .hidden; done

Put this bash script in your path, to do that put this line:

export PATH=$PATH:∼/scripts

into your ∼/.bashrc file (in this example ~/scripts is the folder containing the bash script above), this can be at the end of that file. Maybe you also have to run this file from the terminal, but this will ensure you can run this script anywhere, wherever you're keeping it.

Now you run this script in the terminal, while being in the directory needing cleaning. That means that if the script is named tex.clean, you'd browse to the folder needing cleaning in terminal, enter

tex.clean

in the terminal, and browse to the file in the graphical file browser, and it should be clean :)

All the file with endings mentioned in the script will be written to that directory's .hidden file. This means they will be hidden, but still usable :).

In nautilus (at least in fedora distributions) ctrl+h will show or hide hidden files.

P.S. I tried to have this as detailed as I could so people new to bash and command line like me could use this. I hope I succeeded :).

5
  • 5
    You can avoid repetition of code by writing for i in *.{out,log,aux,bbl,blg,dvi,toc,synctex.gz}.
    – Ryan Reich
    Jul 6, 2012 at 1:22
  • I didn't know that writing file names to a file called .hidden will hide them like all other files that start with .. In which unices is that supported? (not Mac OS as far as I can tell) Nov 20, 2013 at 20:59
  • It's not unix, only nautilus file manager (and possibly others, most certainly its forks nemo and caja) supports hiding via .hidden. If you browse that directory via ls you'll continue to see those files. Still, it's a fairly good solution to remove bothering files if you are a nautilus user.
    – p91paul
    Mar 30, 2015 at 9:48
  • This has nothing to do with file managers. In Linux (Unix) any file (or directory) starting with a dot is always a hidden file even in a terminal (ls cannot show them) . What a file manager make is un-hide that files by default (mc) of after a switch shortcut as Ctrl-H
    – Fran
    Feb 2, 2019 at 6:18
  • Of course, ls -a or ls .* can show hidden files of current directory, but not ls alone.
    – Fran
    Feb 2, 2019 at 6:26
4

Though this is an older question, I want to contribute something that might help people who love clean workspaces. I created a shell script that deletes all junk files at once. It will work on Mac and Linux; with some adjustments Windows should be possible too. Download the file "cleanlatexjunk.sh" from my repository and follow the instructions:

https://github.com/cgraumann/LatexUtils

  1. Open Preferences/Build in TexStudio
  2. Select Advanced options at the bottom
  3. Add a new user command with the name cleanjunk
  4. As the command, enter: "/PATH/TO/SCRIPT/cleanlatexjunk.sh" -fp ?me ?a) and change /PATH/TO/SCRIPT/ accordingly
  5. In the meta-command for Build & View add | txs:///cleanjunk at the end

Important: As described by people before, the "junk" files are actually needed. Therefore you should also adjust your build workflow to do multiple compiles in a row. This assures that all TOCs and links are rendered correctly. After two or three compile runs, you can safely use my script as described.

Now every build & view execution should result in a clean workspace :-)

1
2
# write a makefile and remove all of the unwanted files after compilation.   
# change your folder structure as you see fit. 
# Folder structure
DOCDIR = doc/
TEXDIR = doc/tex/ 

# The compiler and the -output-directory flag
TCC = pdflatex
TEXFLAGS = -output-directory 

documentation: 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename1.tex 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename2.tex 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename3.tex 
        rm -f $(DOCDIR)*.log 
        rm -f $(DOCDIR)*.aux            
        rm -f $(DOCDIR)*.toc 
        rm -f *.pdf
        rm -f *.aux 
        rm -f *.toc 
        rm -f *.log 

# Though if using this method you have to be careful with the image paths in  
# your tex source file. They're taken from the makefile directory.

# To use the file type >> make documentation << in a terminal 

# You can also do it like this

documentation: 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename1.tex 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename2.tex 
        $(TCC) $(TEXFLAGS) $(DOCDIR) $(TEXDIR)filename3.tex 

clean: 
        rm -f $(DOCDIR)*.log 
        rm -f $(DOCDIR)*.aux            
        rm -f $(DOCDIR)*.toc 
        rm -f *.pdf
        rm -f *.aux 
        rm -f *.toc 
        rm -f *.log 

# now >> make clean documentation << outputs with no crappy files
# and >> make documentation << outputs with the crappy files
# and >> make clean << cleans all the crappy files.
2

Going back to why so many extra files (and why the C compiler doesn’t – apparently – do this): C is a language designed for single-pass compilation. Names must be defined before used. LaTeX isn’t. In a world with tight memory, the way to handle this is to use two passes, storing information needed for the second pass on the way through.

In today’s world with big memories, this is not entirely necessary. A LaTeX compiler could in principle store everything needed in internal tables and go back and patch in information as it became available.

If you really want to manage this properly, you need to work out how to use a makefile (or other similar build manager) to decide automatically when a second run is needed (some cases are obvious, like when you change your bibliography, others less so). Read this if you want to find out about Makefiles and LaTeX in detail.

4
  • 1
    "C is a language designed for single-pass compilation." False. C uses multiple passes (preprocessing, compiling, maybe some optimizing, linking, maybe more optimizing) and a C compiler may even choose to dump 5 million files into pwd that are useless after the passes are finished. Most compilers don't unless you tell them to. Nov 7, 2013 at 0:42
  • The only thing that would be needed is that pdflatex puts all aux-files in a separate folder. That would have worked also in a memory-constrained environment and would be clean. Instead they put everything in the same folder cluttering stuff up. That is the problem.
    – exchange
    Dec 14, 2022 at 14:05
  • Preprocessing isn’t really part of the compile. You can actually leave it out if you don’t need it. No one would call linking a compile stage. Optimizing isn’t a language requirement.
    – PhilipM
    Dec 16, 2022 at 8:47
  • As it happens, the first compilers did use more than one pass: bell-labs.com/usr/dmr/www/primevalC.html – but the second pass was code generation, for reasons of small memory rather than a semantic requirement to read the source twice.
    – PhilipM
    Dec 16, 2022 at 8:56
2

For Mac OS X 18.* users (I'm using Mountain Lion) the .hidden file list does not appear to work any more for Finder. An alternative to the previous script would be

for i in *.{out,log,aux,toc,bbl,dvi,blg,synctex.gz};
    do chflags hidden $i;
done

which also works in some flavors of Linux.

2

As of February 2022, there is now a simple solution to this question! To quote from latexmk's changelog for v4.73:

Provide emulation of -aux-directory, so that it can be used with implementation other than MiKTeX

This means that if you build your document with latexmk, all you have to pass is a location where the auxiliary files will be dumped. For example, to build your hello.tex, run:

latexmk -pdf -emulate-aux-dir -aux-directory=\tmp hello.tex

You'll need the an updated version of latexmk—my Latex distribution, MacTex-2021 doesn't have it. You can check which version you have by running latexmk --version. It needs to be v4.73 or later. I was able to upgrade by running:

tlmgr update latexmk
2

You can use OpTeX instead LateX. OpTeX creates its .ref file (and only this, no more) only if it is explicitly needed. I.e. if there are forward references, request to creating table of contents etc. But if you process

Hello world
\bye

by OpTeX, then only hello.pdf and hello.log are created.

1

There's still another option, you can merge the Clean button and the close button in TexMaker so when you close all the auxiliary files are deleted. This way you don't have to remember to Clean all the time yet you can still use the auxiliary files as long as you don't close.

This can be done downloading the sourcecode. The important file is texmaker.cpp and the processes are CleanAll and fileClose

1
  • This is a feature in Configure Texmaker > Commands > Launch the "Clean" tool when exiting Texmaker
    – qwr
    Oct 17, 2016 at 5:10
1

Texmaker includes an option to "use a build directory for output files" which kind of does the trick.

If you enable this setting Texmaker creates a 'build' folder in the same directory as the document .tex file. It automatically cleans the old output and outputs into the build folder instead.

The setting works well but isn't perfect. For example I've found that after adding a new citation to the document Bibtex complained when it couldn't find the .aux file. I had to copy the aux file back out of the build folder to compile the document. I'm not sure if there's a way to fix that yet.

0
1

These files are needed but yes, there's no need to have them at the same path. Use this bash script to avoid cluttering of the folder containing your .tex file:

# plcl: pdflatex clean
infile=`readlink -f "$1"`
inpath=`dirname "$infile"`
tmpath="/tmp/pdflatex`echo "$inpath" | sed -e 's/\//-/g'`"

mkdir "$tmpath"
pdflatex -output-directory "$tmpath" "$1"
mv "$tmpath"/*pdf "$inpath"

This requires bash and sed.

1

The LaTeX build wrapper ltx2any may be a solution. This does not prevent the creation of all the auxiliary files, but places them in another directory, which you can specify

ltx2any -t path_where_you_want_the_auxiliary_files_to_go filename.tex 

This path can very well be a tmp folder.

For more options, see ltx2any --help.

-2

Just do it: It's dirty but useful.

cd /usr/bin && mv pdflatex __backup_real_pdflatex
vim pdflatex
#!/bin/bash

fname="$1"
/usr/bin/__backup_real_pdflatex $@ && rm -f "$fname."{log,nav,out,aux,toc,snm}
exit $?

WARNING: The script above is not tested even once!!

2
  • 1
    So this will delete all the files after a single compile. How do you manage dealing with cross-references that need the .aux, or your Table of Contents that needs the .toc?
    – Werner
    Feb 2, 2019 at 5:24
  • @Werner So it can only handle the simple cases. That's why I say it's "dirty but useful".
    – recolic
    Feb 3, 2019 at 5:50

You must log in to answer this question.

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