TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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 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:

Here is some python code:
def f():
    """This is a function."""
    return 1
This uses the doctest format, but has extra indentation:
>>> def f():
...     """This is a function."""
...     return 1
>>> f()
This is even a problem without any output:
>>> def f():
...     """This is a function."""
...     return 1
>>> f()
share|improve this question
up vote 5 down vote accepted

This is how to work around it:


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

Listings code with literate alteration

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

Your Answer


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.