2

When I'm writing a paper and want to refer to a section, figure or equation in a different part of the document, I end up spending a lot of time scrolling through my source code so that I can see what label I gave it.

Since it's easier to scan through my pdf output than the text source, I had the idea to make this task a lot easier by temporarily overriding the \label command to display the label in the margin. This would save me a lot of cognitive load when working on the draft, and I could change it back once it's time to submit the final version.

The marginnote package can put notes in the margin even within math mode, so I tried this:

\usepackage{marginnote}

\let\oldlabel\label
\renewcommand{\label}[1]{\marginnote{\scriptsize #1}\oldlabel{#1}}

This works great for section titles and figures, but for equations I don't see any note in the margin. Moreover, if I do

\renewcommand{\label}[1]{HELLO}

cross-references to equations are still correctly generated, and the "HELLO" doesn't appear in the equation. I tried putting this \renewcommand after the \begin{document}, but this doesn't make any difference.

So what's going on here? Why can't I redefine \label in math mode, and is there a way to get around it?

Addendum

As @Werner points out in a comment, there are packages showkeys and showlabels that do what I was trying to do here. However, neither package offers the control over the labels' appearance that I would like, so I'm still interested in a way to roll my own version.

7
  • 7
    amsmath redefines \label inside the equation environment. Have you considered showkeys (see Printing labels along with equation numbers)?
    – Werner
    Commented Jan 27, 2014 at 7:56
  • @Werner I didn't know about showlabels. It's what I'm looking for - many thanks, and please feel free to post as an answer if you'd like the points. Although it's not important, I'd quite like to understand why my \label doesn't override amsmath's one, if you happen to know.
    – N. Virgo
    Commented Jan 27, 2014 at 11:53
  • Also not particularly important in the general scheme of things, but do you know any way to prevent showlabels from displaying curly braces around the labels? I don't think they look very good, but I can't find a way to turn them off.
    – N. Virgo
    Commented Jan 27, 2014 at 12:04
  • Showkeys seems a better option than showlabels for me, since it doesn't display the braces. However, I'm now left with the problem that neither package displays underscores correctly, which is annoying because I use them in virtually all my labels.
    – N. Virgo
    Commented Jan 27, 2014 at 12:16
  • To possibly answer the query re. why your \label doesn't override, I understood @Werner to be saying that amsmath redefined the command for the equation environment. So when you write \begin{equation} or whatever, that triggers the temporary redefinition of \label. So the only way to override it would be to override amsmath's configuration of that environment or to redefine \label every time you need to use it in such an environment. That is, the redefining by amsmath isn't all at once. It is setup to be triggered on use of that environment. [But I've not checked the code.]
    – cfr
    Commented Jan 28, 2014 at 2:21

2 Answers 2

3

There's no hope to make your code cooperate with amsmath. But there's already a package that does what you want.

\documentclass{article}
\usepackage{amsmath}
\usepackage[right]{showlabels}

%% remove the braces around the labels
\makeatletter
\def\SL@margintext#1{{\showlabelsetlabel{\SL@prlabelname{#1}}}}
\makeatother

\begin{document}
\section{First}\label{sec_first}

Some text
\begin{align}
a&=b\label{eq:a_b}\\
c&=d\label{eq:c_d}
\end{align}
\end{document}

enter image description here

2
  • Thanks, the hack to remove braces around the labels is what I was looking for, so +1. However, on my system this still doesn't typeset underscores correctly.
    – N. Virgo
    Commented Mar 31, 2014 at 10:17
  • @Nathaniel It should, as you can see. You should open a new question with an example of incorrect printing. It could depend on package you're loading.
    – egreg
    Commented Mar 31, 2014 at 11:42
0

As a temporary measure, I'm using the showkeys package with the following code:

\usepackage{url}
\usepackage[notref]{showkeys}

\DeclareUrlCommand\escapeus{\urlstyle{rm}}
\renewcommand{\showkeyslabelformat}[1]{\scriptsize \escapeus{#1}}

The url package is necessary because I use underscores in my labels, which showkeys doesn't display correctly by default.

However, this isn't 100% satisfactory because for some reason it puts equation labels in the right margin and section labels in the left. Since I don't want this and there seems to be no way to change it, I'm still looking for an answer to my original question.

2
  • There isn't an answer to your original question: if you load amsmath there are several problems with labels. Why don't you try \usepackage[right]{showlabels}?
    – egreg
    Commented Jan 28, 2014 at 8:32
  • @egreg if there is no answer to my original question then how are showkeys and showlabels implemented? Showlabels can display everything on the right, but it puts curly braces around the labels, which I don't want.
    – N. Virgo
    Commented Jan 28, 2014 at 14:22

You must log in to answer this question.

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