6

I’m using the breaklines and prebreak option with listings, but I find it visually not very pleasing that the prebreak-symbols are not vertically aligned. I’d prefer if they were all on the very right of the listing.

Here is example code.

\documentclass{standalone}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[breaklines,breakatwhitespace,prebreak=!,basicstyle=\ttfamily]
This is a long line This is a long line This is a long line This is a long line This is a long line
This is another long line This is another long line This is another long line This is another long line
\end{lstlisting}
\end{document}

Which yields:

enter image description here

while I want something like

enter image description here

1
  • The listings documentation warns against using spaces (other than \space) in the token list for prebreak and postbreak: "You must not use dynamic space (in particular spaces) since internally we use \discretionary. However \space is redefined to be used inside tokens." The required alignment might be hard to achieve. Mar 12, 2013 at 15:11

2 Answers 2

5

A more or less ugly hack is the following. Instead of putting the prebreak symbol in the current line you can put the symbol inside the margin. The distance between the margin and the text body is given by marginparsep. So you can simple adjust it. The example needs two compilation steps.

\documentclass{article}
\usepackage{listings}

\usepackage{marginnote}
\begin{document}
\begin{lstlisting}[breaklines,breakatwhitespace,prebreak={\def\marginnotevadjust{-\baselineskip}\marginnote{\llap{!\hspace*{\marginparsep}}}},basicstyle=\ttfamily]
This is a long line This is a long line This is a long line This is a long line This is a long line
This is another long line This is another long line This is another long line This is another long line
\end{lstlisting}
\end{document}

enter image description here

2
  • Hmm, I don’t remember for what document I needed this, but I’ll keep it in mind for next time; it seems to be un-hacky enough :-) Jul 7, 2013 at 12:14
  • Great, but apparently it does not work with e.g. frame=single though.
    – phk
    Sep 29, 2016 at 21:39
2

Just stumbled upon this thread because I had the same question. Although, the thread is old, I thought about sharing this: By accident I found the following solution which works quite perfect for me:

\documentclass{article}

\usepackage{inconsolata}
\usepackage{listings}
\usepackage{MnSymbol}

\lstset{
  basicstyle=\ttfamily,
  breaklines=true,
  prebreak={\mbox{\quad$\rhookswarrow$}},
  numbers=right,
  numberstyle={\ttfamily\scriptsize},
}

\begin{document}

\begin{lstlisting}
RewriteRule ^/wms/(.+)$ /cgi-bin/qgis_mapserv.fcgi?map=/srv/www/assip/data/mapdb/$1.qgs [QSA,PT]
RewriteRule ^/maps/([^\.]+)$ /qgis-web-client/site/qgiswebclient.html [PT]
RewriteRule ^/maps/(.*) /qgis-web-client/site/$1 [PT]
\end{lstlisting}

\end{document}

This is an example I just took from a document I am working on:

listing with break character being aligned right

The symbol I used is from \usepackage{MnSymbol}. I am not sure why this works but I guess that \quad simultaneously forces the break earlier and 'pushes' the break character far right. At least it looks like it's right-aligned.

3
  • Unfortunately, I just found out that it's not working in every case.
    – niklaas
    Jul 10, 2014 at 17:28
  • 2
    Please post complete examples always
    – user31729
    Jul 10, 2014 at 17:38
  • @ChristianHupfer Sorry, I updated my answer. The above example is now "working" in a way that compiling it with pdflatex generates a valid PDF file but it is not a solution to the original answer as already stated in my previous comment.
    – niklaas
    Jul 10, 2014 at 20: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.