Is there a general way to get listings package to
- typeset a hyphen as a minus character in the rest of the code (the default behavior), but
- typeset a hyphen as a true hyphen if it occurs in comments (using roman typeface)?
Here is relatively minimal example demonstrating my concern. Note the minus sign character in the second comment.
\documentclass{article}
\usepackage{listings}
\lstset{language=C, commentstyle=\rmfamily, columns=flexible}
\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:
using
literatefor replacements. As best I can tell, those affect bothbasicstyleandcommentstyle, so they would not differentiate as desired.using
escapecharto 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.


\usepackage{breqn}– brillon Aug 29 '18 at 02:17