8

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:

listings and breqn

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 Stacey
  • 153,724
  • 43
  • 389
  • 751
  • Andrew reverse order worked for me. – yannisl Dec 09 '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. – Andrew Stacey Dec 09 '10 at 20:01
  • It doesn't even compile with TeXLive 2009 (as packaged on ubuntu). – Harald Hanche-Olsen Dec 09 '10 at 20:10
  • Reverse order doesn't work for me (pdflatex, updated MiKTeX 2.8). – lockstep Dec 09 '10 at 20:14
  • @Harald: what goes wrong on TL2009?? – Andrew Stacey Dec 09 '10 at 21:46
  • @Andrew: It begins with “Missing number, treated as zero” (while loading listings.sty) and goes downhill from there. I didn't have the time nor inclination to find out more, but just moved to my Mac (which has TL2010) instead. – Harald Hanche-Olsen Dec 09 '10 at 22:13
  • 1
    I get regular minus signs. – egreg Mar 20 '20 at 09:46
  • 2
    Please provide a full minimal example. Your choice of document class and preamble may affect your results, so don't leave them out. – daleif Mar 20 '20 at 09:47
  • 1
    Make it into a single example, not these sniplets (single example make it s alot easier for others to help) – daleif Mar 20 '20 at 10:05
  • 1
    Erh, the image you are showing does not match your example above. Please don't waste our time like this. But I see your problem. – daleif Mar 20 '20 at 10:08
  • 1
    It is caused by breqn, they come back when breqn is removed. In general, breqn should not be used. breqn messes with a lot of things, and if you notive when breqn is not used those are not being typeset as hyphens but rather as real math minuses, and breqn messes with those. – daleif Mar 20 '20 at 10:14

4 Answers4

10

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 09 '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.) – Andrew Stacey Dec 09 '10 at 21:45
  • 1
    Nope, this isn't working for me. – Andrew Stacey Dec 10 '10 at 09:55
  • 2
    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 07:38
5

By default listings uses a math minus here and this clashes with breqn. You can change the symbol listings uses for the hyphen. For example this uses \textminus and will work also with breqn:

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

\begin{document}


\lstinline +ABC-ABC+ 

\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"2D}{%
      \lst@ttfamily
         {\textminus}% used with ttfamily 
         \textminus}% used with other fonts
    \@empty\z@\@empty
\makeatother

\lstinline +ABC-ABC+

\lstset{basicstyle=\ttfamily}
\lstinline +ABC-ABC+

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
4

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.

yannisl
  • 117,160
0

Any minus operator (-) I had in my code sample under lstlisting was not showing up. I would get an "Undefined control sequence. n--" error.

Code before, missing minuses

For whatever reason, this fixed it (it'll shows a big dash but that'll do it for a quick solution):

1- Adding the package minted (without using it):

\usepackage{minted}

2- Invoking LaTeX with the -shell-escape flag at each build.

Following the steps mentioned in this answer by hpesoj626:

In TeXstudio

Options > Configure TeXStudio > Build

in the User Commands box, click +Add button and add a name for your command in the first cell, say pdflatex -shell-escape and the command pdflatex -synctex=1 -interaction=nonstopmode --shell-escape %.tex in the second cell.

You can then see the command listed in the menu

Tools > User

Click on the command to run.

When I ran my code the minuses in the lstlisting sections showed up.

Code after, showing minuses