GeneralRedefining the \label macro doesn't work

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
littlegolem
Posts: 4
Joined: Sun Apr 29, 2012 3:17 pm

Redefining the \label macro doesn't work

Post by littlegolem »

Hi! For making it easier to find specific equations in the code, I have created a flag PRINTLABELS that enables the equation labels to be written above the equations and put this code in the preamble:

Code: Select all

\robustify{\label}
\iftoggle{PRINTLABELS}{
    \let\oldLabel\label
    \renewrobustcmd{\label}[1]{\oldLabel{#1}\text{Equation #1}\\}
}{}
where robustify and renewrobustcmd are macros defined in the etoolbox package, and PRINTLABELS is either false or true. I'm creating PRINTLABELS before this code is processed, and even though I set PRINTLABELS to true, the code has no effect. I also tried to put

Code: Select all

\renewcommand{\label}[1]{}
in the preamble, but even this simple code doesn't have any effect. Why isn't anything happening?
Last edited by Stefan Kottwitz on Mon Jun 11, 2012 9:17 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Redefining the \label macro doesn't work

Post by localghost »

Before you try to reinvent the wheel you should take a look at the showlabels package. Furthermore there are editors which support keeping track of used labels (at least Kile and TeXnicCenter if I remember right).


Thorsten
littlegolem
Posts: 4
Joined: Sun Apr 29, 2012 3:17 pm

Redefining the \label macro doesn't work

Post by littlegolem »

Thanks, the showlabels package was just right for me. However, I still wonder why my small invention did not work. Isn't it possible to use \renewcommand on the \label macro?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Redefining the \label macro doesn't work

Post by localghost »

littlegolem wrote:[…] Isn't it possible to use \renewcommand on the \label macro?
The package does nothing else. Just take a look at its (very short) source code.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Redefining the \label macro doesn't work

Post by cgnieder »

littlegolem wrote:... However, I still wonder why my small invention did not work...
It does. You probably haven't set the toggle to true. Or maybe you've set it only to true after the possible definition? This works:

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\usepackage{etoolbox}

\newtoggle{PRINTLABELS}
\toggletrue{PRINTLABELS}% <= this enables the new definition
\robustify{\label}

\iftoggle{PRINTLABELS}{% <= you should set the % here if you want to avoid spurious spaces.
    \let\oldLabel\label
    \renewrobustcmd{\label}[1]{\oldLabel{#1}\text{~Equation #1}}%
}{}
\begin{document}
Text\label{bla}
\end{document}
A more flexible definition would ask for the toggle inside the new definition (besides there being the handy showlabels package):

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\usepackage{etoolbox}

\newtoggle{PRINTLABELS}
\let\oldLabel\label

\renewrobustcmd\label[1]{%
  \iftoggle{PRINTLABELS}{~Equation~#1}{}\oldLabel{#1}}

\begin{document}
Text\label{bla}

\toggletrue{PRINTLABELS}
Text\label{blub}
\end{document}
Regards
site moderator & package author
Post Reply