# Avoiding “minus sign” dash in listings commentstyle

Is there a general way to have listings package typeset commentstyle in roman using a true hyphen, while maintaing the default behavior that hyphen in basicstyle is changed to a minus character?

Here is relatively minimal example demonstrating my concern. Note the minus sign character in the second comment.

\documentclass{article}
\usepackage{listings}

\begin{document}
\noindent
$x = a - b$  // perform standard sanity-check
\begin{lstlisting}
x = a - b // perform listings sanity-check
\end{lstlisting}
\end{document}


Techniques that do not seem to suffice:

1. using literate for replacements. As best I can tell, those affect both basicstyle and commentstyle, so they would not differentiate as desired.

2. using escapechar to render comment differently. If at all possible, I would like a general solution that does not require any change to the original program source code (presuming use of lstinputlisting).

Revision (in response to Marco's answer below). It would be ideal if it could properly handle any form of legal comment without needing to edit source code, for example:

\begin{lstlisting}
x = a - b // perform $5 worth of listings sanity-check [ideally] \end{lstlisting}  Note: my question is related to past question about hyphens and minus sign however in my case, I like the use of the minus sign in the actual code, just not in the comment. - add comment ## 2 Answers You can use the option texcl. For more information have look to the documentation. EDIT Based on the edit of Michael -- A simple solution is to change the catcode of special characters: \documentclass{article} \usepackage{listings} \lstset{language=C, commentstyle={\rmfamily\catcode\$=11}, columns=flexible,texcl}

\begin{document}
\noindent
$x = a - b$  // perform standard sanity-check
\begin{lstlisting}
x = a - b // perform listings sanity-check
\end{lstlisting}

\begin{lstlisting}
x = a - b // perform $5 worth of listings sanity-check [ideally] \end{lstlisting} \end{document}  EDIT 2 (thanks to egreg) Michael requests a solution without using texcl. AFAIK this isn't possible. If you don't say texcl, the - is always interpreted as $-$; this has nothing to do with the catcode of - One should change the interpretation of -; but, AFAIK, it's not possible to change it in comments and not in the code proper. - That is excellent solution for this simple example. Thanks. But I'd be in trouble if my native source code files contain any special tex characters (e.g.,$[]{}/). For example, what if the comment in the source code had been "// perform $5 worth of sanity-check". In the purest sense, this solution doesn't satisfy the goal of not requiring change to the native source code. – Michael Jan 8 '12 at 15:27 much thanks. So in that way, I won't need to edit source code, but will just need to alter commentstyle for those listings that I notice to have such a character. Could such a catcode be used directly as solution for the original problem (without use of texcl). That is, is there a pure solution without texcl by changing commentstyle with catcode to force '-' character to be hyphen rather than minus? – Michael Jan 8 '12 at 16:03 @Michael: Edited my answer. – Marco Daniel Jan 8 '12 at 18:34 add comment As a followup for the archive, I have settled on the following solution that provides minus signs in the source code and hyphens in comments (but without use of texcl). \documentclass{article} \usepackage{listings} \makeatletter \lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}} \@empty\z@\@empty \makeatother \lstset{language=C, commentstyle=\rmfamily, columns=flexible, literate=*{-}{$-$}1} \begin{document} \noindent$x = a - b$// perform \$5 of standard sanity-check
\begin{lstlisting}
x = a - b // perform \$5 of listings sanity-check [ideally]
\end{lstlisting}
\end{document}


This relies on a technique suggested by Alan Munn (for a related question) to override the use of a minus-sign replacement throughout listings, and then the fact that literate*` syntax will add the substitution back for the source code, but not comments/strings.

-