0

The issue is indicated in red below.

enter image description here

I can guess the right indent with breakindent in pt, but there has to be a better way.

\documentclass{article}
\usepackage{listings}

\lstset{basicstyle=\ttfamily,
  breaklines=true,
  breakatwhitespace=true,
  columns=flexible,
  keepspaces=true,
  %breakindent=20.9pt,
}

\begin{document}
\begin{lstlisting}
IIII IIII IIII IIII IIII IIII
IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII
    IIIIII IIIIII IIIIII
\end{lstlisting}
\end{document}
1

I'd use a value expressed in 'em' units: that unit is directly related to the width of the uppercase 'M' (at least in theory), and, as listings uses a monospaced font, that's also the width of the 'I' (+ letterspace). (An alternative definition makes 1em equal to the font size, but in both cases you have a unit that changes with the selected font size).

1

enter image description here

\documentclass{article}
\usepackage{listings}

\settowidth{\dimen0}{\ttfamily I}%
\lstset{basicstyle=\ttfamily,
  breaklines=true,
  breakatwhitespace=true,
  breakindent=4\dimen0,
  columns=flexible,
  keepspaces=true,
}

\begin{document}
\begin{lstlisting}
IIII IIII IIII IIII IIII IIII
IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII
    IIIIII IIIIII IIIIII
\end{lstlisting}
\end{document}

assuming you want 4 "spaces" as the indent. It would be different if you use, say \small in your listings style, then you need to recalculate the width of a \small tt "I", for example.

  • I'm accepting @remco's answer because its implementation is shorter. I didn't know about \settowidth, which may be very useful in other contexts. Thanks! – Frank Seifert May 9 '18 at 17:19

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.