5

I am using $ and _ symbols within lstlisting environment and the output pdf looks OK but I am having an issue within emacs editor where the occurrence of $ changes the colour of the whole document and the occurrence of _ (after $) is also producing undesired results within the editor.

How can I suppress the interpretation of $ as start of math expression? I have tried escaping $ as \$ but in the output pdf, I am getting \$ which is not required.

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}
  a$ = b$ + c$;
  a_{d}=a_{c}
\end{lstlisting}
\end{document}
  • 1
    I have (setq LaTeX-verbatim-environments-local '("Verbatim" "lstlisting")) in my .emacs file, so both Verbatim and lstlisting are considered like verbatim. – egreg Apr 28 '15 at 15:09
  • 5
    I think you should change the title, the problem is not with lstlisting but with emacs and its coloring system. – Ignasi Apr 28 '15 at 15:09
3

I use Aquamacs, so AUCTeX is automatically loaded.

After adding

(setq LaTeX-verbatim-environments '("Verbatim" "lstlisting"))

to my Preferences.el file (the analog of .emacs or whatever initialization file you use), the lstlisting environment is considered the same as verbatim as far as coloring and fonts are concerned. Since I often use Verbatim from fancyvrb, I added that as well.

enter image description here

| improve this answer | |
  • Your AUCTeX should load listings.el automatically which contains (add-to-list 'LaTeX-verbatim-environments-local "lstlisting"). Same goes for fancyvrb.el; your addition to Preferences.el should not be necessary. – Arash Esbati Apr 28 '15 at 16:39
  • 1
    Puh, good to know that it works :-) I presume you don't have (setq TeX-parse-self t) in your init file. If you add it, AUCTeX loads all used styles when you load a tex file, no C-c C-n needed then. – Arash Esbati Apr 28 '15 at 17:40
  • 1
    LaTeX-verbatim-environments is meant to be customizable (and so modified by the user), LaTeX-verbatim-environments-local is not ;-) – giordano Apr 28 '15 at 19:21
  • 1
    @egreg - If you are talking about the AUCTeX manual, it doesn't help in this case; you have to go through the doc-strings. – Arash Esbati Apr 28 '15 at 19:49
  • 1
    @egreg C-h v followed by a variable name shows you the help of that variable. The "doc-string" (or "documentation string") is what follows the "Documentation:" line. If there is a "You can customize this variable." line, it means that variable is user-exposed and can be easily customized also using a graphic interface. Try with C-h v LaTeX-verbatim-environments RET and C-h v LaTeX-verbatim-environments-local RET. – giordano Apr 28 '15 at 20:25
2

Upon @egreg's request, I try to be more verbose on this. I assume you are using GNU Emacs; if so, your OS shouldn't make a difference.

Installation

I suggest you install the latest version of AUCTeX from ELPA by doing M-x list-packages RET, move the cursor on auctex (version 11.88.4 as writing), hit i followed by x and you have the latest version installed (probably under ~/.emacs.d/elpa/)

Activation

If installed from ELPA, you must not, I repeat, must not have this in your init file:

(load "auctex.el" nil t t)

AUCTeX is installed and should start as soon as you load a file with .tex extension.

Basic customization

AUCTeX's strength is in supporting basic LaTeX and contributed packages. Currently, more than 180 style files (jargon for "name-of-supported-LaTeX-package.el") are available. AUCTeX can parse your input file and load the right style if you put this in your init file:

(setq TeX-parse-self t)

Otherwise, you have to hit C-c C-n afterwards. Keep that in your init file.

AUCTeX manual suggests also:

(setq TeX-auto-save t)
(setq-default TeX-master nil)

It's up to you; I have only the second line in my .emacs.

Usage

Now, you can hit C-c C-e and AUCTeX offers lstlisting in completion and asks you for key-value options. Further, note that Emacs doesn't indent the content since spaces are significant:

\begin{lstlisting}
a$ = b$ + c$;
a_{d}=a_{c}
\end{lstlisting}

The fontification (aka syntax highlighting) should work as well.

| improve this answer | |

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.