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?

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

2 Answers 2

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.

2
  • 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, 2016 at 17:31
  • 1
    Yes, it's possible after a slight modification. I've extended the answer above.
    – Thorsten
    Feb 6, 2017 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}
7
  • 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, 2013 at 7:47
  • @Dror \usepackage[hyphens]{url} then define the command as \fbox{\parbox[t]{\marginparwidth}{\raggedright\normalfont\small\url{#1}}}} Dec 6, 2013 at 9: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. Dec 6, 2013 at 11:04
  • @DavidCarlisle your solution does not work in 2-column mode eg. with the IEEEtran class Sep 2, 2019 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. Sep 2, 2019 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .