# Listings, literate, and space at the start of the line.

I am trying to use the literate option to replace the prompt (>>>) and continuations (...) in python input with symbols that "hang" in the margin. I can almost do this with something like literate=*{>>>}{\llap{$\ggg$\hspace{5pt}}}{0} etc. but am running into a few problems: If I use a length of 0 as above I get errors and, extra space is inserted before the listing creating an undesired indent (though as demonstrated below, I think setting the length to 0 will not help, even if I can prevent the errors).

I would appreciate any suggestions. Here is a minimal example that almost almost works:

\documentclass{article}
\usepackage{MnSymbol}
\usepackage{listings}
\lstset{%
language=python,
columns=flexible,
frame=leftline,
framexleftmargin=0pt,
framesep=4pt,
xleftmargin=5pt,
literate=*{>>>}{\llap{\footnotesize$\ggg$\hspace{8pt}}}{1}
{...}{\llap{$\cdots$\hspace{8pt}}}{1}}
\begin{document}
\noindent
Here is some python code:
\begin{lstlisting}
def f():
"""This is a function."""
return 1
f()
\end{lstlisting}
This uses the doctest format, but has extra indentation:
\begin{lstlisting}
>>> def f():
...     """This is a function."""
...     return 1
>>> f()
1
\end{lstlisting}
This is even a problem without any output:
\lstset{%
literate=*{>>>}{}{0}
{...}{}{0}}
\begin{lstlisting}
>>> def f():
...     """This is a function."""
...     return 1
>>> f()
1
\end{lstlisting}
\end{document}
-

This is how to work around it:

...
literate=*{>>>}{\llap{\footnotesize$\ggg$\hspace{8pt}}}{-1}
{...}{\llap{$\cdots$\hspace{8pt}}}{-1}}
...

Used a negative space as the third argument to literate={<replace>}{<replacement text>}{<width>}.

-
Ugh! But a clever ugh none the less:-) Since I am using flexible spacing the slight indent alignment is not much of an issue. (Actually, on my machine it seems to even give the correct indent on these lines too, so I call this a solution.) I am not really sure why using -1 instead 0 works though... –  mforbes Jul 27 '11 at 2:18
Agreed. I tried to figure out something from the listings package code, in terms of what it does with the <width> parameters, without success. Quickly stopped searching after this work-around did the job. –  Werner Jul 27 '11 at 2:23
It seems that -1 is enough. The extra space after >>> etc. is not needed (and actually seems to mess things up a bit). Thanks! (I retract my ugh.) –  mforbes Jul 27 '11 at 2:29
Is there any reason for the space >>>{ }? It seems to mess up the spacing (between the f and () for example). On my system it is not needed. –  mforbes Jul 27 '11 at 2:41
You're right - I didn't notice it at first; was more focussed on the margin than the actual code! I'll modify my answer to reflect that. –  Werner Jul 27 '11 at 2:45