While writing a thesis or working on a large project, the number of files in the main project directory can be overwhelming. A number of metafiles are generated by LaTeX itself, for glossaries, or for bibliographies. Often, the author doesn’t touch these files. In this article, I discuss a neat way to hide these files in a separate folder, in order to reduce clutter in the main project folder.
Basic idea
The idea is to create a folder for metafiles in the main project directory and place all meta-/output-files in that folder. As a consequence, the final PDF will also be generated in that folder, even though we would want it in the project directory. I’ll show how to typeset a document while keeping all metafiles in a separate folder. Moreover, I’ll provide a solution for how to have the final PDF in the main project folder.
Project directory/
|__ main.tex
|__ main.bib
|__ main.pdf
|__ chapter1.tex
|__ chapter2.tex
|__ …
|__ metafiles/
| |__ main.aux
| |__ main.bcf
| |__ main.blg
| |__ main.glo
| |__ main.ist
| |__ main.log
| |__ main.toc
| |__ …
|__ figures/
|__ figure1.pdf
|__ …
LaTeX
Let’s first focus on LaTeX. To generate the PDF from a raw TeX/LaTeX file I use pdflatex
. The command takes an optional argument: output-directory=<directory>. Through this parameter, I can write all meta-/output-files to a specific directory. I call the directory metafiles
. First, I create the directory and then I typeset the LaTeX source file: main.tex
from the main project directory:
mkdir metafiles pdflatex -output-directory=metafiles main
Biber/Biblatex
I use biber
to generate the bibliography using the biblatex package. Similar to pdflatex
, biber
takes an output-directory parameter. In addition, I want to specify the input-directory parameter, which tells biber
where to look for the bibliography files generated by pdflatex
. Note the double dash (- -) for biber parameters:
biber --input-directory=metafiles --output-directory=metafiles main
makeglossaries/makeindex
One way to generate a glossary or list of acronyms is to use the glossaries package. The package also installs a perl script makeglossaries
that simplifies the glossary generation through makeindex
. See the glossaries package documentation for more details. Similar to pdflatex
and biber
, a parameter can be specified to tell makeglossaries
where input files are located.
makeglossaries -d metafiles main
It seems makeindex
doesn’t support a directory parameter. Therefore, either I have to manually change directory (cd
) or provide the exact path to all input and output files. For example, to generate a glossary the commands would either be:
cd metafiles makeindex -s main.ist -t main.glg -o main.gls main.glo cd ..
or
makeindex -s metafiles/main.ist -t metafiles/main.glg -o metafiles/main.gls metafiles/main.glo
Output PDF in project directory
Finally, I want to have the output PDF file in my project directory, rather than in the metafiles
folder. There are two possible ways. Either I simply move (mv
) the file out of metafiles
or I create a soft-link in the project directory, pointing to the actual file.
mv metafiles/main.pdf ./main.pdf ln -s metafiles/main.pdf
Complete bash script
#!/bin/bash # Create directory if it doesn't exist if [ ! -d "metafiles" ]; then mkdir metafiles fi # Run pdflatex and biber with metafiles as in-/output directories pdflatex -output-directory=metafiles main biber --input-directory=metafiles --output-directory=metafiles main pdflatex -output-directory=metafiles main pdflatex -output-directory=metafiles main makeglossaries -d metafiles main pdflatex -output-directory=metafiles main # Create a softlink to the output PDF ln -s metafiles/main.pdf
Limitations/Extensions
The scripts are written in bash
and are likely to work on Linux/UNIX-based systems, including Mac OS X. There are other ways to do the same thing, for example using latexmk or Snakemake. Also, since I’m not working with Windows, I wasn’t able to provide a command-line script for Windows users. Feel free to post your script below for others to use.
Source: The idea for this post and some of the code was taken from here.