[Tex/LaTex] AucTeX (in Emacs) doesn’t seem to find certain packages

auctexemacspackagespdftex

I am using AucTeX in emacs v23 in PFD-mode. When invoking the pdflatex compiler through C-c C-c LaTeX I get a [...] file <some_package>.sty not found error for some particular packages. For example, consider the following foo.tex file:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{Some advanced stuff here.}
  \begin{algorithmic}
    \Function{Increment}{$x$}
    \State \Call{Return}{$x+1$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

Compiling the file using AucTeX (C-c C-c LaTeX) yields:

ERROR: LaTeX Error: File `algpseudocode.sty' not found.

However, if I invoke pdflatex foo.tex via the terminal, everything works alright.

The only packages I have so far experienced this behavior with are algpseudocode and fmtcount, included in the Linux packages texlive-science and texlive-latex-extra, respectively. The packages are, apparently, there; for example, I get the following in the terminal:

$ locate -b fmtcount.sty
/usr/share/texmf-texlive/tex/latex/fmtcount/fmtcount.sty
$ dpkg -l | grep texlive-latex-extra
ii  texlive-latex-extra      2009-10ubuntu1           TeX Live: LaTeX  supplementary packages
ii  texlive-latex-extra-doc  2009-10ubuntu1           TeX Live: Documentation files for texlive-latex-extra

What I find particularly confusing is that I get no such error for package footmisc, which is also part of texlive-latex-extra.

(Please bear in mind that I do not have root access in the machine I'm experiencing this problem at. If it is needed, I can ask the administrators to do the fix – once I know what that might be – but I doubt they'd be willing to start attempting various approaches to it.)


Update

$ kpsewhich algpseudocode.sty
/auto/pkg/texlive-2009/texmf-dist/tex/latex/algorithmicx/algpseudocode.sty

It appears that the problem is caused by the pdflatex program invoked by AucTeX. As I mentioned above, if I run pdflatex foo.tex manually through the terminal, it works, and the first line of foo.log is:

This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009) (format=pdflatex 2010.6.1)  15 FEB 2012 17:15

If, on the other hand, I invoke the compiler through C-c C-c LaTeX in AucTex, the foo.log file is as follows (notice the difference between the TeX distributions seen in the first line):

This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4) (format=pdflatex 2011.4.5)  15 FEB 2012 13:24
entering extended mode
**\input foo.tex
(./foo.tex (/auto/pkg/tetex-3.0/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2004/02/16 v1.4f Standard LaTeX document class
(/auto/pkg/tetex-3.0/share/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2004/02/16 v1.4f Standard LaTeX file (size option)
)
\c@part=\count79
\c@section=\count80
\c@subsection=\count81
\c@subsubsection=\count82
\c@paragraph=\count83
\c@subparagraph=\count84
\c@figure=\count85
\c@table=\count86
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
)
(/auto/pkg/tetex-3.0/share/texmf-dist/tex/latex/algorithms/algorithm.sty
Package: algorithm 

Document Style `algorithm' - floating environment
(/auto/pkg/tetex-3.0/share/texmf-dist/tex/latex/float/float.sty
Package: float 2001/11/08 v1.3d Float enhancements (AL)
\c@float@type=\count87
\float@exts=\toks14
\float@box=\box26
\@float@everytoks=\toks15
\@floatcapt=\box27
)
(/auto/pkg/tetex-3.0/share/texmf-dist/tex/latex/base/ifthen.sty
Package: ifthen 2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
)
\@float@every@algorithm=\toks16
\c@algorithm=\count88
)

! LaTeX Error: File `algpseudocode.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

Enter file name: 
! Emergency stop.
<read *> 

l.5 ^^M

*** (cannot \read from terminal in nonstop modes)


Here is how much of TeX's memory you used:
 346 strings out of 94499
 4143 string characters out of 1173445
 49441 words of memory out of 1000000
 3593 multiletter control sequences out of 10000+50000
 3640 words of font info for 14 fonts, out of 500000 for 2000
 580 hyphenation exceptions out of 1000
 23i,0n,17p,157b,36s stack positions out of 1500i,500n,5000p,200000b,5000s
PDF statistics:
 0 PDF objects out of 300000
 0 named destinations out of 131072
 1 words of extra memory for PDF output out of 65536
No pages of output.

I'm afraid I can't figure out how to point AucTeX to the correct pdflatex installation, though.

In case it's useful, I have the following AucTeX-related lines in my .emacs file:

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(add-hook 'LaTeX-mode-hook
          (lambda ()
            (TeX-fold-mode 1)))
(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook 'TeX-PDF-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTeX t)

Best Answer

The problem was being caused by invoking Emacs through the Gnome panel, which results in Emacs being oblivious of the PATH as it is set through .bashrc.

A way to fix this is to have Emacs set its PATH to be the one seen by bash, by putting the following in the .emacs file (credit to Shane in here):

(setenv “PATH” (shell-command-to-string “bash -i -c ‘echo -n $PATH’”))