[Tex/LaTex] Tweak showlabels/showkeys – wrap the label

line-breakingmarginparshowkeysshowlabels

Can showlabels wrap the label in the margin? A very similar question is showlabels: wrap labels in marginpar, but the solution of @DavidCarlisle does not solve the more general case. Consider the following MWE:

\documentclass{article}
\begin{document}
\begin{figure}[ht]
  \centering
  \rule{1cm}{1cm}
  \caption{default}
  \label{fig:a very very very long and detailed label}
\end{figure}
\end{document}

When adding the package showkeys the output is:
enter image description here

Similar problem occurs with the showlabels package (whether the hack of @DavidCarlisle is used or not):
enter image description here

So, is there a way to fix it?

Best Answer

Having labels like this is really a misuse of the label system, labels are intended to be short symbolic names, but anyway

enter image description here

\documentclass{article}
\usepackage{showkeys}
\renewcommand*\showkeyslabelformat[1]{%
  \fbox{\parbox[t]{\marginparwidth}{\raggedright\normalfont\small\ttfamily#1}}}

\begin{document}
\begin{figure}[ht]
  \centering
  \rule{1cm}{1cm}
  \caption{default}
  \label{fig:a very very very long and detailed label}
\end{figure}
\end{document}