0

I have the following function attached to f2 key in latex mode:

(defun latex-compile ()
      "Runs pdflatex on current file"
      (interactive)
      (let* ((file-name (shell-quote-argument (buffer-file-name)))
            (process (start-process-shell-command
                           "pdflatex"
                           "*async pdflatex*"
                           (concat "pdflatex " file-name))))
            (set-process-sentinel process 'latex-sentinel)
            (setq new-window (split-window-below 30))
            (set-window-buffer new-window "*async pdflatex*")
            (other-window 1)
            (set-process-query-on-exit-flag process nil)
            (local-set-key (kbd "z") 'latex-kill-async-pdflatex)
            ))

pdflatex program is located in /usr/local/texlive/2016/bin/i386-linux. I have the following in .bash_profile and .bashrc:

PATH=$PATH:/usr/local/texlive/2016/bin/i386-linux

When I click <f2> in GUI Emacs, it says: "/usr/bin/bash: pdflatex: command not found".

But in console Emacs it works.

I have also added

(setq exec-path (append exec-path '("/usr/local/texlive/2016/bin/i386-linux")))

to .emacs, but it didn't help.

0

1 Answer 1

3

exec-path is how Emacs finds executables, but you're not asking Emacs to find pdflatex, you're only asking it to find a shell, and the shell is then trying to run pdflatex.

The shell will be using the PATH in its environment. Your problem is probably that the way in which you start your GUI Emacs does not involve setting that PATH value in its environment, whereas when you start terminal Emacs you're doing so from a bash session in which your bash init files have run.

(How do you start your GUI Emacs?)

You can either ensure that when GUI Emacs is started it has the desired PATH, or else you can set the value in the process-environment variable in Emacs using setenv.

You can do this temporarily for a command if you prefer. e.g.:

(let ((process-environment (copy-sequence process-environment)))
  (setenv "PATH" (concat (getenv "PATH") ":/usr/local/texlive/2016/bin/i386-linux"))
  (start-process-shell-command "path" "*path*" "echo $PATH"))

A modified version of the original function might look like:

(defun latex-compile ()
  "Runs pdflatex on current file"
  (interactive)
  (let ((process-environment (copy-sequence process-environment)))
    (setenv "PATH" "$PATH:/usr/local/texlive/2016/bin/i386-linux" t)
    (let* ((file-name (shell-quote-argument (buffer-file-name)))
           (process (start-process-shell-command
                     "pdflatex"
                     "*async pdflatex*"
                     (concat "pdflatex " file-name))))
      (set-process-sentinel process 'latex-sentinel)
      (setq new-window (split-window-below 30))
      (set-window-buffer new-window "*async pdflatex*")
      (other-window 1)
      (set-process-query-on-exit-flag process nil)
      (local-set-key (kbd "z") 'latex-kill-async-pdflatex))))
4
  • "(How do you start your GUI Emacs?)" - clicking the icon in KDE start panel.
    – user4035
    Mar 18, 2017 at 12:32
  • In which case I would start searching on KDE support forums for how to pass the environment configured by your bash init files to a process started from the KDE start panel, if you wish to pursue that approach.
    – phils
    Mar 18, 2017 at 12:57
  • I have already added it to environment system wide by adding (setenv "PATH" (concat (getenv "PATH") ":/usr/local/texlive/2016/bin/i386-linux")) to .emacs. Now it works.
    – user4035
    Mar 18, 2017 at 13:04
  • Is it possible to set it locally using let*? Can you modify my function latex-compile? I tried to add your code, but it didn't work
    – user4035
    Mar 18, 2017 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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