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}
Ignasi
  • 136,588
Shahzad
  • 1,297
  • 14
  • 19
  • 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

2 Answers2

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

egreg
  • 1,121,712
  • 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
  • @ArashEsbati Nice to know; is it a recent addition to AUCTeX? As far as I know, those .el files are not automatically loaded. – egreg Apr 28 '15 at 16:40
  • @ArashEsbati I tried with a standard Emacs, with AUCTeX loaded and no, those .el files are not loaded. – egreg Apr 28 '15 at 16:45
  • Actually not. AUCTeX is supporting styles for a long time. I presume something is wrong with your configuration. – Arash Esbati Apr 28 '15 at 16:46
  • @ArashEsbati On two different flavors of Emacs both using AUCTeX? Sorry, I can't accept this. – egreg Apr 28 '15 at 16:47
  • I'm not on Mac, so please bear with me. You could try the following in standard Emacs: Install AUCTeX from elpa, comment out (load "auctex.el" nil t t) in your init file, restart emacs and load a tex-file. – Arash Esbati Apr 28 '15 at 16:58
  • @ArashEsbati Did what you said. No listings.el loaded, sorry. – egreg Apr 28 '15 at 17:17
  • This is strange, it should work out of the box. Does it help if you open your tex-file an hit C-u C-c C-n with the newly installed AUCTeX? – Arash Esbati Apr 28 '15 at 17:28
  • @ArashEsbati Yes, after the magic it works. And it seems permanent. Please, add an answer, with all details. – egreg Apr 28 '15 at 17:32
  • 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
  • @giordano That's not what I understand from the documentation. Probably my fault, or maybe not? – egreg Apr 28 '15 at 19:34
  • 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
  • @egreg In the AUCTeX manual LaTeX-verbatim-environments-local is not even mentioned (for a reason), where did you get that idea? Arash is right, looking at the doc-strings you can see LaTeX-verbatim-environments is customizable and LaTeX-verbatim-environments-local is not. – giordano Apr 28 '15 at 20:13
  • @giordano I understood the exact contrary. What the arcane “doc-strings” are, I don't know. Don't be surprised if people thinks Emacs is too arcane. It wouldn't be if the developers used a clearer language. – egreg Apr 28 '15 at 20:14
  • 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
  • @giordano That's what I saw: Programs should not use this variable directly; which is now clearer. But it shows that one should buy a dictionary Emacs-English for understanding what's the matter here. – egreg Apr 28 '15 at 20:36
  • @egreg - I agree with you that some doc-strings are not that self-explanatory. – Arash Esbati Apr 28 '15 at 20:47
  • For current Ubuntu versions, this line has to go into ~/.emacs.d/init.el. – mat Apr 12 '17 at 11:48
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.

Arash Esbati
  • 7,416