7

I have the problem that automatic hyphenation in listings caption doesn't work if automatic linebreaking is activated. It works if I set breaklines=false or captionpos=t, which is weird. Is there a workaround? I want to have captions at the bottom and I need automatic line-breaking.

\documentclass{article}
\usepackage[english]{babel}

\usepackage{listings}

\lstset{
   breaklines=true,
   captionpos=b
}

\begin{document}

\begin{lstlisting}[caption={information information information information information information information}]
A = B
C = D
\end{lstlisting}

\end{document}

captionpos=b (WRONG):

enter image description here

captionpos=t (correct):

enter image description here

0

1 Answer 1

8

The problem is the line

\hbadness\@M \pretolerance\@M

of lstmisc and the macro \lst@AddToHook{Init} which is set before the listings contents with breaklines=true. With captionpos=t it doesn't belong to the caption, but with captionpos=b it does. It prevents hyphenation. A simple fix is to reset \pretolerance:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{listings}
\lstset{
   breaklines=true,
}

\begin{document}

\begin{lstlisting}[caption=information  information  information information information information information,captionpos=t]
A = B
C = D
\end{lstlisting}

\begin{lstlisting}[caption={\pretolerance=100 information information information information 
information information information},captionpos=b]
A = B
C = D
\end{lstlisting}

\end{document}

A better solution is to reset the values inside \lst@MakeCaption which should be reported to the current maintainer of listings.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .