[Tex/LaTex] AUCTeX -output-directory + copy *.pdf to working directory


Part I:

Could someone please give me an example of how to configure AUCTeX to use the -output-directory option?

I've tried inserting -output-directory="/tmp" in a variety of ways at different locations, but it's not working — with two hyphens, with one hyphen, with double quotes, with single quotes, without any quotes, before the %t, after the %t, right before the closing single quote, . . . .

Here is the generic latexmk code for the working directory in my init.el file:

 '(TeX-command-list (quote ( ("LaTeX" "%`%l%(mode)%' %t" TeX-run-TeX nil (latex-mode) :help "Run LaTeX"))))

Part II — Extra Credit

Then, I'd like to take one step further and copy the PDF back to the working directory with the following example. I'm on OSX with TeX Live, so -aux-directory is not supported.

Configure latexmk so that the command it uses for pdflatex does the ordinary pdflatex command and then copies the output file where you want it. You'd invoke latexmk with the -output-directory option.

An appropriate line in one of latexmk's configuration files is

$pdflatex .= ' && cp "%D" "%R.pdf"';

You could also persuade it to display a message about the copying of the output file:

$pdflatex .= ' && (cp "%D" "%R.pdf"; echo Output file copied from "%D" to "%R.pdf" in current directory)';

(N.B. The second suggestion was all supposed to be on one line.)

Then you invoke latexmk by something like the following:

latexmk -outdir=/tmp

For anyone interested in synctex with the -output-directory option on an OSX box, add -synctex=1 to the line of code proposed by T. Verron in the answer below:

;; use Skim as default pdf viewer
;; Skim's displayline is used for forward search (from .tex to .pdf)
;; option -b highlights the current line; option -g opens Skim in the background

(setq TeX-PDF-mode t)

(setq TeX-view-program-selection '((output-pdf "Skim")))

(setq TeX-view-program-list
    '(("Skim" "/Applications/Skim.app/Contents/SharedSupport/displayline -b -g %n /tmp/%o %b")))

(server-start); start emacs in server mode so that skim can talk to it

See also https://stackoverflow.com/questions/7899845/emacs-synctex-skim-how-to-correctly-set-up-syncronization-none-of-the-exi

EDIT: The following stackoverflow link contains an alternative method of using Emacs and start-process to run latexmk and copy the .pdf file back to the working directory (leaving the auxiliary files in the /tmp folder) — if the latexmk compilation is successful, then Skim is opened (with forward sync) — if the latexmk compilation generates errors, then the error buffer is displayed. https://stackoverflow.com/questions/18705774/if-latexmk-finishes-okay-then-show-pdf-else-display-errors

Best Answer

If you want to use AUCTeX so desperately:

(add-hook 'LaTeX-mode-hook (lambda ()
                  '("Latex_outdir" "%`pdflatex --output-directory=/tmp %(mode)%' %t" 
                TeX-run-TeX nil (latex-mode doctex-mode) 
                :help "Run pdflatex with output in /tmp")

The question you linked to in your last comment is still unanswered, and will cause a serious problem with this setup: AUCTeX won't be able to locate your auxiliary files, and thus deduce the next step in the compilation step.

I suggest you use latexmk for the whole process instead:

(add-hook 'LaTeX-mode-hook (lambda ()
                  '("Make" "latexmk -outdir=/tmp %t" TeX-run-TeX nil t
                :help "Make pdf output using latexmk.")

Latexmk is aware of the option outdir, and will automatically search for the auxiliary files in this directory.

Either way, note the following point (from man latexmk):

Commonly, the directory specified for output files is a subdirectory of the current working directory. However, if you specify some other directory, e.g., "/tmp/foo" or "../output", be aware that this could cause problems, e.g., with makeindex or bibtex. This is because modern versions of these programs, by default, will refuse to work when they find that they are asked to write to a file in a directory that appears not to be the current working directory or one of its subdirectories. This is part of security measures by the whole TeX system that try to prevent malicious or errant TeX documents from incorrectly messing with a user's files. If for $out_dir or $aux_dir you really do need to specify an absolute pathname (e.g., "/tmp/foo") or a path (e.g., "../output") that includes a higher-level directory, then you need to disable the security measures (and assume any risks). This can be done by temporarily setting the operating system's environment variable openout_any to "a" (as in "all"), to override the default "paranoid" setting.

Another potential problem with this setting is that all your documents will share the same temporary directory, so you will need to make sure they all have different names.

For your cp step, you can use this line in your .latexmkrc:

$pdflatex .= ' && cp -v %Z/%D %D';

Note that most of this is untested, please let me know if it doesn't work.

Related Question