[Tex/LaTex] How to organize the working directory and generate the output files (PDF and auxiliary files) into selected folders

latexmkmiktextexstudiowindows

How can I choose the folders at which I can store/generate the PDF file and auxiliary files using TeXstudio and Latexmk on Windows?


Attention

This question is meant to help the new users get started with organizing their working directory by specifying specific folder for the PDF file and another folder for the whole generated auxiliary files.

Best Answer

For the following, it is assumed that two folders are needed to host all the generated files: one for the output PDF file (suppose the folder name is PDF) and the other for all the auxiliary files generated by pdflatex (I neamed the folder AuxDirectory). Two approaches are considered: TeXstudio and Latexmk.

TeXstudio

First, TeXstudio needs to understand where to put the output files. SO, two options will be changed: --aux-directory for auxiliary files and --output-directory for PDF output file.

Go to Options > Configure TeXstudio > Commands. In pdfLaTeX field, add --aux-directory=AuxDirectory --output-directory=PDF before %.tex.

enter image description here


In case of using BibTeX:

In BibTeX field, change it from bibtex.exe % to bibtex.exe AuxDirectory/% -include-directory=AuxDirectory. This is very important step to make the bibliography tool able to find aux files.

enter image description here

In case of using Biber:

In Biber field, change it from biber.exe % to bibtex.exe AuxDirectory/%.

enter image description here


To make TeXstudio able to locate PDF output file for external pdf readers, in the left tab menu, go to Build > Additional Search Paths: and write the folder name you wish to store log file into (I chose AuxDirectory) and the folder name that TeXstudio asks the external PDF viewer to go there to open the file.

enter image description here

Now, you are able to make a clean and well organized directory for the LaTeX project.


Latexmk

Latexmk is a powerful automation tool and also smart. More information is given in its documentation. The same previous options (--aux-directory and --output-directory) are used since the same engine/compiler is given.

For example, if you want to compile a file named main.tex using latexmk you have to run this command (providing that the command current directory is the same as the one hosting main.tex)

latexmk -quiet -bibtex -aux-directory=AuxDirectory --output-directory=PDF -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode" main.tex

Latexmk is very smart and able to determine the required number of pdfLaTeX and BibTeX runs to properly generate the PDF output file.

Additionally, if you found it very annoying to open the command window each time you want to compile your document, you can create a bash file that opens the command window and runs the previous command by just double-clicking it.

In order to do so, create a file with extension .bat in the same working directory of main.tex and edited it to have

cmd /k latexmk -quiet -bibtex -aux-directory=AuxDirectory --output-directory=PDF -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode" main.tex.

Any other suggestions/improvements are highly appreciated.