It's annoying that LaTeX creates so many auxiliary files and TeXstudio offers no possibility to delete them automatically on program exit.
I also want to use latexmk, since it caches results and therefore builds very quickly, since it doesn't always have to compile everything.
This is a workaround to configure TeXstudio so that latexmk writes to a build directory.
- Think of a name for your build directory, I will use
.latexmk
in the following. Replace if necessary. - Go to "Options -> Configure TeXstudio".
- Click on "Show Advanced Options".
- Go to the "Build" tab.
- Create the following User commands:
ln_pdf
:ln -f .latexmk/%.pdf
.ln_synctex
:ln -f .latexmk/%.synctex.gz
.rm_synctex
:bash -c "sleep 1 ; rm -f %.synctex.gz"
- Select latexmk as a compiler by setting "Default Compiler":
txs:///latexmk
- Enter this as a command for "Build & View":
txs:///compile | txs:///ln_pdf | txs:///ln_synctex | txs:///view | txs:///rm_synctex
. - Put
.latexmk
in the input fields at the bottom for "Log File" and "PDF File" under "Additional Search Paths". - Go to the "Commands" tab.
- Change the command of
Latexmk
tolatexmk -pdf -output-directory=.latexmk -silent -synctex=1 %
. - You're good to go.
latexmk
will now write to .latexmk
and afterwards we will hard-link the PDF and synctex.gz
files
to the destination of your tex
file. Not that these are hard links, so it's absolutely safe to
delete the .latexmk
directory, your PDF outside the build directory will stay intact!
The synctex.gz
file gets deleted after a one second sleep which should be enough for the viewer to do its job. I tried the solution proposed here, i.e. without a sleep, but TeXstudio will complain that there is no synctex data available, so this hack sadly seems to be necessary.