I would like to handle my projects such that every time I compile a .tex file, the compiler would always place the output .pdf in a directory named output (and create it if it doesn't exist).

Here's a sample Terminal session where the --output-directory option cannot create the specified directory if it doesn't exist.

$ tex --output-directory=output
This is TeX, Version 3.1415926 (TeX Live 2010)
! I can't write on file `texput.log'.
(Press Enter to retry, or Control-D to exit; default file extension is `.log')
Please type another transcript file name: 

In TeXShop, how do I specify the command to do as I wish?

enter image description here


insert before the pdftex command

mkdir -p output

TeX itself does not create the directory if it doesn't exist


Herbert's solution will cause the directory to be created any time you issue the pdflatex command. It only works from the command line, and can't be used within TeXShop itself.

Personally I would implement this using TeXShop's "engine" scripts, so that the basic pdflatex command stays the same, and you can selectively decide whether a particular document goes into an output folder or not.

To do this, make the following shell script:

PATH= $PATH:/usr/texbin:/usr/local/bin
mkdir -p output
pdflatex --output-directory=output --file-line-error --shell-escape --synctex=1 "$1"

Save this as ~/Library/TeXShop/Engines/pdflatex-output.engine and make it executable:

chmod +x ~/Library/TeXShop/Engines/pdflatex-output.engine

If you restart TeXShop, you will now have a new Engine "pdflatex-output" that is available in the pull-down menu beside the Typeset button on your source window.

You can choose this as the Engine to do your typesetting, or you can put

% !TEX TS-program = pdflatex-output

as the first line of your source file, and that engine will automatically be chosen. (You can insert the line manually or you can choose it using the Program macro from the Macros menu.)

This solution gives you the flexibility of having the output directory be used only when you want it to be used.


One problem with the solution above is that TeXShop can no longer find the resultant .pdf file, since it assumes that it is in the same directory as the .tex source. As you've agreed in the comments, it would be acceptable to have all the auxiliary files (.aux, .toc, .bbl, etc.) in the output directory and leave the .pdf file in the same folder as the .tex file. Here's another script written in Python from Marcus Whybrew which does exactly that.

However, rather than following the installation instructions there (which I don't recommend), you should use this script as an engine, just like the simpler bash script above.

  • this looks neat. How come the preview window no longer opens automatically? What other option is needed? – Kit Sep 16 '11 at 1:20
  • @Kit Sorry for not replying to your previous comment. I got busy and this problem isn't as simple as it looks. The reason the preview window doesn't open is that TeXShop can't find the pdf, which is now in the output directory. (This will also mess up synctex.) Is your main goal to separate the pdf or to separate the associated files (.toc, .bbl, .aux) etc? Because there are ways to separate the latter and then move the pdf back to the place where TeXShop can find it. – Alan Munn Sep 16 '11 at 1:42
  • Thanks for the support. I see, so it's hardcoded in TeXShop that it should look for the PDF in the current folder. LaTeX does generate a lot of files which make the current folder quite a mess. My goal was to keep the PDFs in a clean folder, but by putting it in one other than the current, TeXShop can't find it. Therefore, a viable solution would be the one you suggested. I believe this involves some bash scripting quite outside the LaTeX topic domain? If so, I'll do some research or ask another question if I get stumped. – Kit Sep 16 '11 at 8:12
  • @Kit I'll modify my solution to do that. – Alan Munn Sep 16 '11 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.