I'm using moredelim (with prefix il) to typeset one-line "comments" in a very specific way:

  • delimiter %% in red,
  • rest of the line in blue.

My approach works mostly as expected, aside from some very weird spacing issue, which I can't explain. For instance, on the second line of the listing below, even though there is only one space character between %% and the following word (in the input file), that line gets typeset in a completely different way; more specifically, the spacing before my delimiter in the input file seems to end up after it in the output file.

I'm pretty sure this isn't a problem related to spurrious spaces left in my code. What the hell...?

enter image description here



    basicstyle  = \ttfamily,
    commentstyle= \color{blue},
    moredelim   =**[il][\processDelimiter]{\%\%},

\def\processDelimiter% auxiliary macro for delimiters
    \textcolor{red}{\%\%}%     % Typeset the delimiter 
    \lst@commentstyle%         % Apply the comment style.


x:=1000; %% a first comment
x:=x/2;        %% why so much space on this side?

  • 1
    I cannot really explain right now but your example works for me when I add \lst@CalcLostSpaceAndOutput before \textcolor.... – Robert Dec 8 '13 at 2:03
  • @Robert I'll have to test your fix more extensively than I have, but you're right: it seems to do the trick!. Thanks a lot. But I wonder... how did you come up with that fix?! Anyway, you should post it as an answer... – jub0bs Dec 8 '13 at 2:06
  • Wading through the log, basically, with \tracingmacros1... I'll post an answer (tomorrow) when I can add an explanation. – Robert Dec 8 '13 at 2:08

listings delays outputting space characters until the next entity is typeset. In the case of moredelim, the preceding spaces are only typeset when the %% delimiter is encountered. Now, while the "style" (ie. the second argument in square brackets) will of course be applied before printing the delimiter, the actual test whether any spaces are still to be output will only take place after that. (I don't think this could be called a bug, rather, your use of the style argument to actually typeset something is probably somewhat outside the specification).

But you can force listings to check for, and print, any spaces still on the stack with \lst@CalcLostSpaceAndOutput. Therefore,

\def\processDelimiter% auxiliary macro for delimiters
    \lst@CalcLostSpaceAndOutput % Typeset spaces
    \textcolor{red}{\%\%}%      % Typeset the delimiter 
    \lst@commentstyle%          % Apply the comment style.

should work.

Another possibility might be to load listings with keepspaces=true.

| improve this answer | |
  • I just get "undefined control sequence" when I do this; but only when it is used in this macro, the "\lst@CalcLostSpaceAndOutput" seems to run just fine if I use it manually. – Ben Farmer Jun 17 '16 at 9:37
  • @BenFarmer which control sequence is undefined? Are you sure it's not just a typo? – Robert Jun 17 '16 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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