Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Gonzalo Medina Mar 12 '13 at 15:11
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
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 :-) –  Joachim Breitner Jul 7 '13 at 12:14
add comment

Your Answer

 
discard

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.