Take the 2-minute tour ×

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 http://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. http://stackoverflow.com/questions/18705774/if-latexmk-finishes-okay-then-show-pdf-else-display-errors

share|improve this question
The question is quite unclear. It has two parts, one about emacs and one about latexmk. For the emacs part, I don't see the difference between "your generic latexmk code" and the default option for latex-ing documents. Maybe somewhere else, you have some code which tells auctex to substitute %l with latexmk instead of (pdf)latex? (1/3) –  T. Verron May 1 '13 at 22:51
Now, since you are using latexmk, the -output-directory option should be given in the config file for latexmk, instead of your init.el. See this answer for a way of doing it : tex.stackexchange.com/a/31253/9517 . In any case, there is good chance that I missed your point, since as I said, I don't understand what is the question. (2/3) –  T. Verron May 1 '13 at 22:52
It almost looks like the first part is a question and the second one its answer -- except the answer doesn't really answer the question. (3/3) –  T. Verron May 1 '13 at 22:52
The package AUCTeX for Emacs uses the difficult to use format cited in my question and that is why I'm having trouble. I can do this from the terminal and also from Sublime Text with a plugin that I wrote myself, but AUCTeX doesn't seem to support the same format as the terminal. –  lawlist May 1 '13 at 22:59
The first component of this question regarding inserting the -output-directory=/tmp should be a question that a user of Emacs AUCTeX can answer fairly easily. The second part of the question was just in case there is a genius in the audience . . . :) I'd be happy with the first component of the question, and I'll post the second answer myself in a couple of weeks after I get the basic format under my belt. –  lawlist May 1 '13 at 23:03

2 Answers 2

up vote 5 down vote accepted

If you want to use AUCTeX so desperately:

                  '("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:

                  '("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.

share|improve this answer
Thank you very much for taking the time to post such a detailed solution. I'll try it out late this evening after work and report back . . . –  lawlist May 2 '13 at 21:34
Both of the hooks that are provided in this answer by T. Verron work perfectly, with the advantage being the latexmk solution and copying the *.pdf back to the working directory -- eliminating the need to clean-up. I was not able to get the copy command working with the copy code in this answer. However, the copy code provided in the question does work on my OSX box. That particular code for copying the *.pdf actually came from John Collins, Ph.D. (author of latexmk.pl). Thank you very much for the hooks -- your help is greatly appreciated ! –  lawlist May 5 '13 at 1:05
I've added the synctex option to the bottom of my question for anyone who is interested. –  lawlist May 5 '13 at 4:30
In order to assign a keyboard shortcut to build and view, it will be necessary to create a defun equivalent for the hooks. TeX-command-master is not linked to build, but rather Recent Output Buffer (i.e., whatever command was last run). I do not yet have a complete solution, but will post one when I find it . . . A variation of this seems to be working, but I need to adapt it to the jump-to-pdf also: tex.stackexchange.com/questions/60181/… –  lawlist May 5 '13 at 5:54
When you press C-c C-c, you are prompted for the name of the compilation command. Entering Make here should start the latexmk script. Alternatively, you can add (add-hook 'LaTeX-mode-hook '(lambda () (setq TeX-command-default "Make"))) to your .emacs. Note that latexmk is also able to start a viewer automatically when compilation ends. –  T. Verron May 5 '13 at 9:16

As for the cp step, on Ubuntu 13.10 it worked for me using this line in my latexmkrc file: $pdflatex = 'xelatex %O %S && cp %D %R.pdf';.

My complete latexmkrc file goes like this:

$pdf_mode = "1";
$out_dir = "out";
share|improve this answer
Does Tex Live for Linux distributions also not support the command line of -aux-directory or the $aux_dir option in the .latexmkrc, and is that the reason you are using copy? I used the copy command on OSX because those auxiliary functions are not supported (as of the year 2012). Or is it a different story when using xelatex? –  lawlist Oct 18 '13 at 5:20
-aux-directory is indeed not implemented for any TeX Live distribution. Ubuntu 13.10 has TeX Live 2013 in its repository. –  Apteryx Oct 18 '13 at 20:11

Your Answer


