# Line breaks in theorem heads are removed by showkeys package

There are a couple of other questions [1|2] about theorem heads not wrapping properly. I've run across another situation where this happens:

\documentclass{article}

\usepackage{amsthm}
\usepackage{showkeys}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}[Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.]
\label{Thm1}
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{theorem}

\end{document}


With either \usepackage{showkeys} or \label{Thm1} commented out, this produces the expected output, with the theorem head ("Lorem …") wrapped across multiple lines. With both present, the whole head is placed on the same line, and wrapping starts only at the first space in the theorem body:

My main purpose is to record this observation, to save someone else the half hour or so it took me to narrow down the problem. But I do also have a question: Is there any way to work around this, other than just ignoring it? I find the showkeys package very useful, but this (apparent) bug reduces that by changing the layout of the output document.

Putting the label at the start of a paragraph is always slightly tricky (latex has to work quite hard to make that not change the spacing normally, and that interacts with showkeys somewhat badly.....)

Putting the \label more or less anywhere else is better (perhaps showkeys could catch this case and do better, but not today)

\documentclass{article}

\usepackage{amsthm}
\usepackage{showkeys}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}[Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.%
\label{Thm1}]
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{theorem}

\begin{theorem}[Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.%
]
\mbox{}\label{Thm2}Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{theorem}

zzz \ref{Thm1} zzz \ref{Thm2}
\end{document}

• Wow, that was fast. Thanks. (I'll accept shortly.) – Stephen Powell Feb 1 at 11:50
• @StephenPowell: You could also insert either \noindent or \leavevmode just before \label{…}. That will fix the problem by starting the paragraph before the \label, but you'll have to remember to this every time… – Circumscribe Feb 1 at 12:13