1

I'm using listings to highlight some PHP code. My problems comes when the code to insert contains the '$' symbol, for example:

\begin{lstlisting}[style=PHP]
Select * FROM users WHERE user_name = $_POST['user'];
\end{lstlisting}

The $ set maths mode and the text after the lstlisting is included until I type another $. I've been looking for solution but unluckily. Using '\$' to escape the symbol $ does not work inside the lstlisting (in my pdf it would appear '\$' and obviously I don't want that to happen).

The PHP lst style is set like follows:

\lstdefinestyle{PHP}{
 frame=tb, to wrap the text with black horizontal lines
 language=php,
 aboveskip=3mm,
 belowskip=3mm,
 showstringspaces=false,
 basicstyle      = \small\ttfamily,
 keywordstyle    = \color{cyan},
 stringstyle     = \color{gray},
 identifierstyle = \color{blue},
 commentstyle    = \color{green},
 emph            =[1]{php},
 emphstyle       =[1]\color{black},
 emph            =[2]{if,and,or,else},
 breaklines=true,
 breakatwhitespace=true,
 tabsize=3,
 %emphstyle       =[2]\color{dkyellow}
}
  • Welcome to TeX.SX! If I prepare a minimal document with your data, I get $ working as expected, thus not starting math mode. – egreg Nov 19 '13 at 12:58
  • But the text after the lstlisting appears to me in green, until I write another $ ("pointing out" that im leaving the match mode?) that comes back to its normal color – Rafag Nov 19 '13 at 13:06
  • Did you tried to compile the document without the second $ ? I think it is only the misbeheaving of the editors syntax highlighting. Some editors do not recognize listings well. – Cv4 Nov 19 '13 at 13:11
  • 1
    @Rafag Please, make a minimal working example (MWE) – egreg Nov 19 '13 at 13:19
4

If you're using TeXnicCenter, and it's only the coloring inside the editor:

This is a listed bug.

The issue also effects the verbatim environment: http://sourceforge.net/p/texniccenter/bugs/318/.

A "not really nice workaround": if it is really annoying (pages of code colored in a wrong way) you can put this after a wrong verbatim or listing environment: \ifx{\verb+$+}!\fi. It can help when you really need the coloring to be right.

  • Thats right, Im using TexnicCenter! – Rafag Nov 20 '13 at 11:17
  • @Rafag This only effects the coloring. I've added a workaround which doesn't affect the output but fixes the issue. Not a diamond but it can actually help. – masu Nov 20 '13 at 11:52
2

Maybe a clearer explanation.

"Text after the dollar sign appears in green" means you are talking about the editor you use for editing the TeX source. This has nothing to do with TeX math mode.

TeX is a completely separate piece of software from the editor. TeX has absolutely no graphical user interface, but a command line interface instead. Editors run TeX in the background the same way you could if you opened a command prompt and typed pdflatex myfile.tex.

The colored stuff you see on the screen is only for your eyes. TeX doesn't care if you edit the source code in Notepad or whatever.

  • Finally! I was wondering if any one is going to write this answer or not +1 – Pouya Jan 25 '14 at 10:47

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.