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 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

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer

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.