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 ...
Essentially, line breaks denote the end of one line and the start of a new one. With format marks enabled, line breaks are represented by the symbol of an arrow pointing down and to the left (shown right). In HTML, you may create a line break using the <br> tag.
The \linebreak command tells LaTeX to break the current line at the point of the command. With the optional argument, number, you can convert the \linebreak command from a demand to a request. The number must be a number from 0 to 4. The higher the number, the more insistent the request is.
To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.
Press ALT+ENTER to insert the line break.
Configure listings as follows:
\lstset{prebreak=\raisebox{0ex}[0ex][0ex] {\ensuremath{\rhookswarrow}}} \lstset{postbreak=\raisebox{0ex}[0ex][0ex] {\ensuremath{\rcurvearrowse\space}}} \lstset{breaklines=true, breakatwhitespace=true} \lstset{numbers=left, numberstyle=\scriptsize}
Example source code in document:
\begin{lstlisting}[language=C++, numbers=left, linewidth=5.1cm] if (line.length() > line.max_length()) { line.wrap(); } \end{lstlisting}
Example result:
Note: you have to \usepackage{MnSymbol}
to be able to use \rhookswarrow
and \rcurvearrowse
. To prevent the extra dependency you can use \hookrightarrow
and \hookleftarrow
which are included in the default packages.
Source: http://www.bollchen.de/blog/2011/04/good-looking-line-breaks-with-the-listings-package/. Read there for more detailed explanation. Also pdf.
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. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With