19

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?

Dror
  • 22,613

2 Answers2

8

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}
\begin{equation}
    (a\cdot b ) \cdot c = a\cdot (b\cdot c)
    \label{eq:groupassociativity}
\end{equation}
\begin{figure}[ht]
  \centering
  \rule{1cm}{1cm}
  \caption{default}
  \label{fig:a very very very long and detailed label}
\end{figure}
\end{document}

long labels with spaces

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.

Thorsten
  • 332
  • 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. – tvk Dec 16 '16 at 17:31
  • 1
    Yes, it's possible after a slight modification. I've extended the answer above. – Thorsten Feb 06 '17 at 11:08
7

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}
David Carlisle
  • 757,742
  • 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 06 '13 at 07:47
  • @Dror \usepackage[hyphens]{url} then define the command as \fbox{\parbox[t]{\marginparwidth}{\raggedright\normalfont\small\url{#1}}}} – David Carlisle Dec 06 '13 at 09:22
  • 1
    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 06 '13 at 11:04
  • @DavidCarlisle your solution does not work in 2-column mode eg. with the IEEEtran class – Dr Krishnakumar Gopalakrishnan Sep 02 '19 at 16:54
  • @Krishna I just changed articletoIEEEtran` 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 02 '19 at 18:44
  • @DavidCarlisle I don't know. I had to do \fbox{\parbox[t]{\marginparwidth}{\raggedright\path{#1}}}} to get it to work. The key thing missing here is \path. Actually, you were the one who wrote this other incantation in your answer to a duplicate question here. – Dr Krishnakumar Gopalakrishnan Sep 02 '19 at 19:16
  • @Krishna you might need \path if the label is not hyphenatable or, as here, has spaces, but that is completely unrelated to IEEEtran or double column – David Carlisle Sep 02 '19 at 19:27