tags:

views:

445

answers:

1

Hi everyone

I have a Latex code similar to the following:

\usepackage{listings}
\lstset{
    breaklines     = true,
    numbers        = left,
    stepnumber     = 5,
}
\begin{lstlisting}
for (int i = 0, j = 0, k = 1; i <= 10, j < foo; i++, j *= factor, k--) { // a comment here
    % something code here ...
}
\end{lstlisting}

The long line with the for-loop gets automatically broken somewhere in the middle because the paper is too narrow. That's just what I want, otherwise the line would disappear at the end of the paper. But now my question is whether there is a way to mark or indicate that this line was broken. Otherwise it looks like a new line was created.

Something like that is what I want (with line numbers, notice the arrow):

10   for (int i = 0, j = 0, k = 1; i <= 10, j < foo;
   ↳     i++, j *= factor, k--) { // a comment here
11       % something code here ...

Thanks & hope for an answer soon :)

+5  A: 

Apparently this can be accomplished with prebreak / postbreak options. See the listings package's documentation, linked to from its CTAN page (it's the bottom of page 33 and the top of page 34 in the pdf).

Edit: a direct link to the Listings Package's manual. Also, I guess I can reproduce the relevant bit here -- so here goes:

prebreak=<tokens>   (default {})
postbreak=<tokens>  (default {})

<tokens> appear at the end of the current line respectively at the beginning of the next (broken part of the) line.

You must not use dynamic space (in particular spaces) since internally we use \\discretionary. However \\space is redefined to be used inside <tokens>.

Edit 2: As for the placement of the linebreak marker, something close to the appearance of the example provided in the question can be achieved by setting breakindent to 0pt, breakautoindent to false and hand-padding the marker with, say, \\space.

For example, use postbreak={\textbf{marker}\\space\\space\\space\\space} for a nice bold marker typeset where a new line would normally start, then four spaces, then the remaining contents of the broken line. These options are documented right after prebreak / postbreak in listings' manual. :-)

Michał Marczyk
Thanks, "postbreak" seems to be /nearly/ what I've been looking for, guess it's the best way. Might there be a way to position the token of "postbreak" to the beginning of the line? Maybe reset its position?
watain
Yes, you can manipulate the indent and include hard spaces in the linebreak marker to push the actual contents of the broken line to the right. I've edited my answer to include the relevant details. Hope it works for you. :-)
Michał Marczyk
Thank you very much Michał. That's just want I wanted :) I should've read the whole Listings manual first ;)
watain