# Alignment problem if line breaks are used in listings

I am using a monospace font and I want that all lines are horizontally aligned ("a" should be directly under "a"). How can I do this?

I have noticed that also the pagebreak symbol shifts the text. How can I draw a symbol e.g. in Tikz which has a maximum width of one character?

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{mathabx}

\lstset{
breaklines=true,
frame=lines,
basicstyle=\ttfamily,
numbers=left,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\drsh\space}},
showlines=true
}

\begin{document}

\begin{lstlisting}
a = a a a a a a a a a a a a a a a a a a a a a a a ab a a a a a a a a a a a a a a a a a
\end{lstlisting}

\end{document}


You can adjust the spacing of the math inside \raisebox in the postbreak option through \mkern:

postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\mkern-12mu\drsh\mkern-4mu}},


MWE

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{mathabx}

\lstset{
breaklines=true,
frame=lines,
basicstyle=\ttfamily,
numbers=left,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\mkern-12mu\drsh\mkern-4mu}},
showlines=true
}

\begin{document}

\begin{lstlisting}
a = a a a a a a a a a a a a a a a a a a a a a a a ab a a a a a a a a a a a a a a a a a
\end{lstlisting}

\end{document}


Output

## EDIT

If you need the symbol \drsh but you can't load mathabx in your document, you can simply "extract" the definition of the symbol from the package and insert it in your document, that is substitute the line

\usepackage{mathabx}


with

\DeclareFontFamily{U}{mathb}{\hyphenchar\font45}
\DeclareFontShape{U}{mathb}{m}{n}{
<5> <6> <7> <8> <9> <10> gen * mathb
<10.95> mathb10 <12> <14.4> <17.28> <20.74> <24.88> mathb12
}{}
\DeclareSymbolFont{mathb}{U}{mathb}{m}{n}
\DeclareMathSymbol{\drsh}{3}{mathb}{"EB}


MWE that produces the same output as above:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{listings}
%\usepackage{mathabx}
\DeclareFontFamily{U}{mathb}{\hyphenchar\font45}
\DeclareFontShape{U}{mathb}{m}{n}{
<5> <6> <7> <8> <9> <10> gen * mathb
<10.95> mathb10 <12> <14.4> <17.28> <20.74> <24.88> mathb12
}{}
\DeclareSymbolFont{mathb}{U}{mathb}{m}{n}
\DeclareMathSymbol{\drsh}{3}{mathb}{"EB}

\lstset{
breaklines=true,
frame=lines,
basicstyle=\ttfamily,
numbers=left,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\mkern-12mu\drsh\mkern-4mu}},
showlines=true
}

\begin{document}

\begin{lstlisting}
a = a a a a a a a a a a a a a a a a a a a a a a a ab a a a a a a a a a a a a a a a a a
\end{lstlisting}

\end{document}

• Thx! Is it possible to draw the small arrow in Tikz because I am using Luatex and mathabx is not supported? I have tried \tikz ... in postbreak but I always get error messages. If I can use Tikz, do you know how I can find out the exact width of one monospace character? Jan 15, 2014 at 6:26
• @user4811 see the edit. Jan 15, 2014 at 6:39
• Thx, works great... Jan 15, 2014 at 15:27