5

How can I automatically break the following inline code at a whitespace?

The problem is that I have to use breaklines=false.

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{blindtext}

\lstset{breaklines=false,
        literate={,}{}{0\discretionary{,}{}{,}}}

\begin{document}
\lstinline{ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC}\\
\blindtext
\end{document}

edit

I adapt the literate parameter from another example but it doesn't work with spaces.

  • You could use \lstinline[breaklines=true]{ABC, ABC ...} for the one case where you actually want to break the lines. – Peter Grill Jan 22 '14 at 5:22
  • @PeterGrill: Thx, maybe there is a solution using literate. – user4811 Jan 22 '14 at 6:00
  • You can always \newcommand{\lstinlinebrk}{\lstinline[breaklines=true]} and then use \lstinlinebrk{ABC, ABC, ...}... – Werner Jan 22 '14 at 6:12
5

This works:

literate={\ }{}{0\discretionary{}{}{\mbox{ }}}

but it's better to insert the \lstinline in a sloppypar environment to avoid the text going beyond the margins.

MWE:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{blindtext}

\lstset{breaklines=false,
        literate={\ }{}{0\discretionary{}{}{\mbox{ }}}}

\begin{document}
\begin{sloppypar}
\lstinline{ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC, ABC}
\end{sloppypar}
\blindtext
\end{document} 

Output:

enter image description here

Anyway, I wonder why you can't use the options

breaklines=true,breakatwhitespace=true

for the specific \lstinline.

  • Thanks, this works! I need breaklines=false because I specify other line breaks than ', ' manually. If breaklines=true listings makes mistakes separating words. – user4811 Jan 22 '14 at 14:00

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.