# Minus signs vanish with listings and breqn - any solutions?

When using listings and breqn, the minus signs (or dashes, according to the unix.SX site then there's some controversy about which it is) disappear from code listing. Here's an example:

\documentclass{article}
\pagestyle{empty}
\usepackage{breqn}
\usepackage{listings}

\begin{document}

\lstset{language=Perl}
\begin{lstlisting}
#! /usr/bin/perl -w

if ($ARGV[0] =~ /^-/) { print "Option given"; } \end{lstlisting} \end{document}  with result: Changing the order of package loading doesn't help. breqn does warn that it might break other packages, but it would be really useful to have both working. If it helps, the code listings are in appendices so happen after the equations which breqn is meant to help with so I can happily reset anything that got changed. - Andrew reverse order worked for me. – Yiannis Lazarides Dec 9 '10 at 19:52 @Yiannis: that's strange, it doesn't for me. I'm compiling with pdflatex using the default packages from TeXLive 2010. – Loop Space Dec 9 '10 at 20:01 It doesn't even compile with TeXLive 2009 (as packaged on ubuntu). – Harald Hanche-Olsen Dec 9 '10 at 20:10 Reverse order doesn't work for me (pdflatex, updated MiKTeX 2.8). – lockstep Dec 9 '10 at 20:14 @Harald: what goes wrong on TL2009?? – Loop Space Dec 9 '10 at 21:46 ## 2 Answers Your problem comes from the fact that \lstlisting likes to typeset a hypen-minus as $-$, and the the breqn package plays around with math so much. In particular, it sets the mathcode of the minus to "8000, which makes it active and seems to break things. \documentclass{article} \pagestyle{empty} \mathchardef\hyphenmathcode=\mathcode\- \usepackage{breqn} \usepackage{listings} \let\origlstlisting=\lstlisting \let\endoriglstlisting=\endlstlisting \renewenvironment{lstlisting} {\mathcode\-=\hyphenmathcode \everymath{}\mathsurround=0pt\origlstlisting} {\endoriglstlisting} \begin{document} \lstset{language=Perl} \everymath{} \begin{lstlisting} #! /usr/bin/perl -w if ($ARGV[0] =~ /^-/) {
print "Option given";
}
\end{lstlisting}
\end{document}


(The \everymath{}\mathsurround is not necessary for this, but it should be there for good measure.)

Edit: Added the mathcode bit. I'll say more on this edit in the comments.

-
I just noticed that the lstlisting environment takes an optional parameter. If you want to support that, you may need a slightly more sophisticated redefinition. –  Harald Hanche-Olsen Dec 9 '10 at 21:17
Thanks! I'll try this tomorrow and report back. (Actually, I'm using the \lstinputlisting command since it's a "live program" but I reckon I can figure out how to hack that from your hack.) –  Loop Space Dec 9 '10 at 21:45
Nope, this isn't working for me. –  Loop Space Dec 10 '10 at 9:55
How bizarre! Now, my first fix is no longer working for me either. But I am sure it did, last night? I must have been doing something slightly different so as to avoid triggering the problem. Anyhow, from a joint session with Andrew we found that the mathcode of - is getting changed, and changing it back to its normal (non-active) meaning cures the disease. For now. –  Harald Hanche-Olsen Dec 10 '10 at 11:28
I'd simply use \preto{\lstlisting}{\mathcode\=\hyphenmathcode} (after loading etoolbox). I wouldn't be surprised if other quirks like this one pop out. In my opinion breqn makes more problems than it solves. –  egreg Feb 11 '14 at 7:38

Here is a solution:

\documentclass{tufte-book}
\pagestyle{empty}
\usepackage{listings}
\usepackage{breqn}
\begin{document}

\normalfont

\makeatletter
\texttt{\meaning\lst@InputCatcodes\relax}
\makeatother

\lstset{language=Perl,escapeinside={(*@}{@*)}}
\begin{lstlisting}
#! /usr/bin/perl -w

if (\$ARGV[0] =~ /^(*@-@*)/) {
print "Option given";
}
\end{lstlisting}

\end{document}


It is really a font problem. The - sign in your font is an active character in listings. (See the meaning). If you escape it though it gets the right catcode and the error disappears. You escape using funny eyes:) (*@ @*) and you can insert anything you want. Perhaps someone else can come with an easier solution as I guess it is a hassle to have to escape the sign always.

I use MikTeX for compiling.

Edit

Another hack would be to define a new environment using the listings lstnewenvironment command to protect the definitions from other packages:

\lstnewenvironment{perl}[1][]
{\lstset{language=Perl}\lstset{%
#1
}}
{}


This also worked,

Use: \begin{perl}...\end{perl}

This solution is very similar to that given by Harald.

-
Escaping isn't an option as I'm actually importing a "live" program via the \lstinputlisting so adding the escapes every time would be a real hassle. The other solution looks promising, thanks. –  Loop Space Dec 9 '10 at 21:46
This isn't working for me. Also, I don't see where listings makes - active in that meaning` string. –  Loop Space Dec 10 '10 at 9:59
I'm also unable to use the "Another hack" solution. –  Peeter Joot Sep 30 '14 at 2:03