# Tweak showlabels/showkeys - wrap the label

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:

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

So, is there a way to fix it?

• Why would you ever want a label that long? I've used latex for 25 years and never put a space in a label? – David Carlisle Dec 6 '13 at 0:08
• @DavidCarlisle: I guess this will turn into a new question... – Dror Dec 6 '13 at 7:47
• Did you find any optimum solution for this? :) I have the same problem for labeling equations. :) – H. R. Dec 5 '16 at 21:21

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

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

• Can this also work for the following \label{fig:a-very-very-very-long-and-detailed-label}? In particular treat the - as spaces. I guess it can be tweaked. – Dror Dec 6 '13 at 7:47
• @Dror \usepackage[hyphens]{url} then define the command as \fbox{\parbox[t]{\marginparwidth}{\raggedright\normalfont\small\url{#1}}}} – David Carlisle Dec 6 '13 at 9:22
• The analogous command in the showlabels package is \showlabelsetlabel, but, like David, I'm sceptical that a label this long is a good idea. – Norman Gray Dec 6 '13 at 11:04
• @DavidCarlisle your solution does not work in 2-column mode eg. with the IEEEtran class – Krishna Sep 2 at 16:54
• @Krishna I just changed article to IEEEtran in the above and it worked for me, If something gives an error make a test fie and post a new question. – David Carlisle Sep 2 at 18:44

In order to split a long label without spaces, you can use the \seqsplit command from the seqsplit package. The following is an adaption of David's answer and also shows the necessity of wrapping long labels:

\documentclass{article}
\usepackage[%showframe, % show helper lines
paperwidth=10cm, paperheight=7cm,
hmargin=24mm,vmargin=4mm, marginparwidth=17mm,
]{geometry}
\usepackage{seqsplit}
\usepackage{showkeys}
\usepackage{xstring}

\renewcommand*\showkeyslabelformat[1]{%
\noexpandarg%
% instead of \textvisiblespace you can also put in ~
% if you want to keep a plain space at space characters
\StrSubstitute{$$\{$$#1$$\}$$}{ }{\textvisiblespace}[\TEMP]%
\parbox[t]{\marginparwidth}{\raggedright\normalfont\small\ttfamily\expandafter\seqsplit\expandafter{\TEMP}}}

\begin{document}
$$(a\cdot b ) \cdot c = a\cdot (b\cdot c) \label{eq:groupassociativity}$$
\begin{figure}[ht]
\centering
\rule{1cm}{1cm}
\caption{default}
\label{fig:a very very very long and detailed label}
\end{figure}
\end{document}


For the spaces, one needs to replace the spaces within the seqsplit, either by ~ for keeping a space or by something else like \textvisiblespace` to visualize it better.

• Is it possible to extend this to accommodate labels containing spaces as well?---I mean just like treating the spaces as any other character and break wherever the line width is exhausted. – Fang Jing Dec 16 '16 at 17:31
• Yes, it's possible after a slight modification. I've extended the answer above. – Thorsten Feb 6 '17 at 11:08