Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

enter image description here

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.

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 18:55
add comment

3 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}

enter image description here

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.

share|improve this answer
    
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.

share|improve this answer
add comment

The following approach doesn't require altering the listing or changing any catcode.

output

\documentclass{article}

\usepackage{listings}

\lstset
{
    language     = C,
    commentstyle = \rmfamily,
    columns      = flexible,
    literate     = {-}{{\Processdash}}1,
    keepspaces,
}

\makeatletter
\newcommand\Processdash%
{%
    \lst@ifLmode% if in one-line comment mode
        -%
    \else%
        $-$%
    \fi%
}
\makeatother

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

\begin{lstlisting}[frame=single]
x = a - b // perform listings sanity-check
\end{lstlisting}

\begin{lstlisting}[frame=single]
x = a - b // perform $5 worth of listings sanity-check [ideally]
\end{lstlisting}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.