AUCTeX, preview-latex, and Ghostscript (Emacs)

I'll keep this short and sweet; Emacs can't find gs, which has been installed via brew and works fine.

Error
PDF2DSC sentinel: Searching for program: No such file or directory, gs

MWE

\documentclass{article}
\begin{document}
This is a test.  $x^4$.
\end{document}


And my PATH after init: "/usr/local/bin:/usr/texbin:/usr/local:/usr/bin:/bin:/usr/sbin:/sbin"

brew placed a symlink to Ghostscript under /usr/local/bin -> /usr/local/Cellar/ghostscript/9.06/bin/gs. (Adding this directory doesn't help.)

Is this a problem with preview in some way, or is it a problem with Emacs itself? (I should note that running the shell command directly (M-! gs) works as expected, and running psf2dsc manually also seems to work as expected (no errors, mwe.dsc produced).)

Per @Charles' request, echo $PATH produces /usr/local/bin:/usr/texbin:/usr/local:/usr/local/Cellar/ghostscript/9.06/bin:/u‌​sr/bin:/bin:/usr/sbin:/sbin under M-x eshell. Per @Charles' request, M-x preview-report-bug produces Emacs : GNU Emacs 24.2.1 (x86_64-apple-darwin, NS apple-appkit-1038.36) of 2012-08-27 on bob.porkrind.org Package: 11.86 current state: ============== (setq AUCTeX-version "11.86" LaTeX-command-style '(("" "%(PDF)%(latex) %S%(PDFout)")) image-types '(png gif tiff jpeg xpm xbm pbm) preview-image-type 'png preview-image-creators '((dvipng (open preview-gs-open preview-dvipng-process-setup) (place preview-gs-place) (close preview-dvipng-close)) (png (open preview-gs-open) (place preview-gs-place) (close preview-gs-close)) (jpeg (open preview-gs-open) (place preview-gs-place) (close preview-gs-close)) (pnm (open preview-gs-open) (place preview-gs-place) (close preview-gs-close)) (tiff (open preview-gs-open) (place preview-gs-place) (close preview-gs-close)) ) preview-dvipng-image-type 'png preview-dvipng-command "dvipng -picky -noghostscript %d -o \"%m/prev%%03d.png\"" preview-pdf2dsc-command "pdf2dsc %s.pdf %m/preview.dsc" preview-gs-command "gs" preview-gs-options '("-q" "-dSAFER" "-dNOPAUSE" "-DNOPLATFONTS" "-dPrinted" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4") preview-gs-image-type-alist '((png png "-sDEVICE=png16m") (dvipng png "-sDEVICE=png16m") (jpeg jpeg "-sDEVICE=jpeg") (pnm pbm "-sDEVICE=pnmraw") (tiff tiff "-sDEVICE=tiff12nc")) preview-fast-conversion t preview-prefer-TeX-bb nil preview-dvips-command "dvips -Pwww -i -E %d -o %m/preview.000" preview-fast-dvips-command "dvips -Pwww %d -o %m/preview.ps" preview-scale-function 'preview-scale-from-face preview-LaTeX-command '("%%l \"\\nonstopmode\\nofiles\\PassOptionsToPackage{" ("," . preview-required-option-list) "}{preview}\\AtBeginDocument{\\ifx\\ifPreview\\undefined" preview-default-preamble "\\fi}\"%' %t") preview-required-option-list '("active" "tightpage" "auctex" (preview-preserve-counters "counters")) preview-preserve-counters nil preview-default-option-list '("displaymath" "floats" "graphics" "textmath" "sections" "footnotes") preview-default-preamble '("\\RequirePackage[" ("," . preview-default-option-list) "]{preview}[2004/11/05]") preview-LaTeX-command-replacements nil preview-dump-replacements '(preview-LaTeX-command-replacements ("\\\$$[^ ]+\$$\$$\\( +-\\([^ \\\\\"]\\|\\\\\\.\\|\"[^\"]*\"\$$*\\)*\\)\$$.*\$$\\'" "\\1 -ini -interaction=nonstopmode \"&\\1\" " preview-format-name ".ini \\5") ) preview-undump-replacements '(("\\\$$[^ ]+\$$ .*? \"\\\\input\" \$$.*\$$\\'" "\\1 -interaction=nonstopmode \"&" preview-format-name "\" \\2") ) preview-auto-cache-preamble 'ask preview-TeX-style-dir "/Users/sean/.emacs.d/elpa/auctex-11.86/latex" ) Output from running gs -h':  Followed by a message: Searching for program: No such file or directory, gs - I should add that I'm trying to emulate Aquamacs' behavior - it's prepackaged with AucTeX, but too bloated for my taste. preview-latex works fine under it. – Sean Allred Nov 23 '12 at 17:36 Could you run eshell (M-x eshell) in an instance of Emacs, and say what result you get from echo$PATH? –  Charles Stewart Feb 24 '13 at 14:02
/usr/local/bin:/usr/texbin:/usr/local:/usr/local/Cellar/ghostscript/9.06/bin:/u‌​sr/bin:/bin:/usr/sbin:/sbin –  Sean Allred Feb 24 '13 at 14:16
I might have a few questions. The next is, after getting this error, can you run M-x preview-report-bug RET, and paste the results (which might be quite long) into your question? –  Charles Stewart Feb 25 '13 at 6:35
@CharlesStewart, done. –  Sean Allred Feb 25 '13 at 6:49

The problem appears to be something related to the complicated way that looking for binaries works in Emacs. It is possible that each of the following will give a different list of bin directories:

1. H-v exec-path RET (the "internal" path that is derived from PATH at the time Emacs initialises) (this is where PDF2DSC looks for gs)
2. M-x eshell RET followed by echo $PATH (set to the init PATH, with possible some additional bin directories inserted; eshell is a program that is a bit like UNIX shell, but is implemented in Emacs Lisp) 3. M-x shell RET followed by echo$PATH, which runs a subordinate shell process, which may have additional bin directories inserted when it runs the .shrc file

Auctex path search is tricky, because it looks for executables in either of two ways, using TeX-run-command directly, which depends on exec-path, and by a invoking subordinate shell, which will depend on the value of PATH within that process. Additionally, when running Aquamacs, the application is launched directly using OSX's launchd services, and so the PATH Emacs starts with is independent of the PATH you see at the command line, since the program was not called from any shell.

Adding the gv bin directory to exec-path might help, or try using a distribution such as Texlive, which has a single bin directory containing both gs and the pdftex, etc., executables.

vermiculus' bounty award notice

After checking those directories and applying fixes as necessary, gs should start working properly. If after you apply these fixes and a 'Do-Not-Enter' sign appears where images should be, follow:

1. Do one of the following:

• .emacs Evaluate (setq preview-gs-options '("-q" "-dNOSAFER" "-dNOPAUSE" "-DNOPLATFONTS" "-dPrinted" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4")), tailoring to your setup. The important bit is changing "-dSAFER" to "-dNOSAFER".

• (GUI)

1. Preview -> Customize -> Extend this menu
2. Preview -> Customize -> Customize GS...
3. Change option "-dSAFER" to "-dNOSAFER" (I'm unsure of what this does, but it worked.)
2. Remove previews from the document and re-preview.

As mentioned in the comments, changing this option may be dangerous if you are working with documents that you don't trust; otherwise it is almost certainly safe.

-
Using gs with the -dNOSAFER option is potentially dangerous! –  Paul Gaborit Feb 25 '13 at 14:42
So I figured -- what exactly does this do? is there a better way? –  Sean Allred Feb 26 '13 at 10:02
@vermiculus: -dSAFER stops gs from doing certain things, most importantly from piping into shell commands. -dNOSAFER is the default; I'm surprised the option was set to -dSAFER given what preview-latex needs gs to do. It is almost certainly safe if you are only using preview-latex on files you are familiar with. –  Charles Stewart Feb 26 '13 at 10:16
It's certainly worth mentioning in the answer, so I'm glad you brought it up. Thankfully, I primarily use TeX (at the moment) to do my own homework. Do you know of a better way to go about this, or perhaps the dangers of using -dNOSAFER? –  Sean Allred Feb 26 '13 at 11:03