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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the listings package to typeset some code. I've defined a lstlisting environment, based upon the standard Haskell environment, and added a number of literate replacements, including:

literate={->}{{$\rightarrow{}$}}2

When the symbol -> appears in a listing environment, my spacing around the symbol is preserved. However, when it appears in an inline listing, the space after the \rightarrow gets eaten, resulting in T a -> a -> a looking like T a ->a -> a. Does anyone know how I can avoid this behavior in the inline listings without also confusing the listing environment?

share|improve this question
    
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. I realize that it does not sound too complicated in this case, but any time you can save of those trying to help would be appreciated. – Peter Grill Feb 22 '12 at 5:45
up vote 4 down vote accepted

I would suggest using literate to replace the "symbol" -> with -> (note the addition of spaces surrounding the arrow. The reason being that spaces are treated differently in inline as opposed to display listings. Here is a minimal example:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{%
  literate={->}{{ $\rightarrow$ }}3
}
\begin{document}
Here is a display listing:
\begin{lstlisting}
T a->a->a
\end{lstlisting}
Here is some inline listing \lstinline!T a->a->a!.
\end{document}

Note the difference in spacing between T and a in the respective listing styles.

share|improve this answer

@Werner's solution is fine, but adding spaces to the replacement will have different results in both lstlisting and lstinline. For example, if you replace -> by, say, -> and you have the following snippet:

a -> a -> a -> a

it will show up normally in lstinline, but it will show up with two spaces after the arrows in lstlisting, like this:

a ->  a ->  a ->  a

My solution to this was to define a replacement for -> and another one for ->:

{->}{{{\color{magenta}->}}}2
{->\ }{{{\color{magenta}-> }}}3

Note that the smallest replacement must come first, otherwise it won't work.

share|improve this answer

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.