Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to get the target name of the first label in a chapter. For example, if my document contains this:

\chapter{Introduction} \label{intro}

I would like a command/macro that returns "intro" when run inside that chapter. Preferably this also returns "intro" after other labels have been defined in sections and subsections. This command is needed in another macro that fetches specific resources from subfolders that have the same name as the chapter label.

I tries messing around with macros such as currentHref, currentlabel and currentlabelname but was not able to get what I want. Any pointers that can help me?

share|improve this question
    
Does \newcommand*{\chaplabelname}{intro}\label{\chaplabelname} fit your needs? –  Heiko Oberdiek Aug 4 '12 at 22:16

1 Answer 1

up vote 6 down vote accepted

One way to do this would be to redefine the \chapter and \label commands such that the very first use \label following a \chapter sets the desired value.

enter image description here

At the invocation of a \label, the value of the parameter to \label is stored in \CurrentChapterLabel if the toggle isFirstLabel is set. This toggle is set to false upon the first invocation of \label, so that subsequent uses of \label will not overwrite the value of \CurrentChapterLabel.

At the beginning of each \chapter, the value of toggle isFirstLabel is set so that the next use of \label will be stored.

Notes:

  • I have used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.

  • The options [openany] and \usepackage[paperwidth=11.0cm]{geometry} were only used to obtain a better image to illustrate the result.

  • The value defined in \DefaultChapterLabel is what is returned if an attempt to use \CurrentChapterLabel prior to using the \label macro within a \chapter:

    enter image description here

Code:

\documentclass[openany]{book}
\usepackage[paperwidth=11.0cm]{geometry}
\usepackage{etoolbox}

\newtoggle{isFirstLabel}
\toggletrue{isFirstLabel}

\newcommand*{\DefaultChapterLabel}{UNDEFINED}%
\newcommand*{\CurrentChapterLabel}{\DefaultChapterLabel}%

\let\OldChapter\chapter
\let\OldLabel\label

\def\chapter{%
    \renewcommand{\CurrentChapterLabel}{\DefaultChapterLabel}% reset
    % Set so that the next usf \label will store its 
    % value in \CurrentChapterLabel
    \global\toggletrue{isFirstLabel}% 
    \OldChapter% let \chapter do it's original job
}%
\renewcommand*{\label}[1]{%
    \iftoggle{isFirstLabel}{%
        % This is the first use of \label in this chapter
        % so store its value, and reset the toggle so we don't
        % store the next time \label is used.
        \renewcommand{\CurrentChapterLabel}{#1}%
        \global\togglefalse{isFirstLabel}%
    }{}%
    \OldLabel{#1}% let \label do it's original job
}%

\begin{document}
\chapter{Introduction} \label{intro}
\section{Intro Section One} \label{intro-section-one}
Current chapter label is: \CurrentChapterLabel

\chapter{Second Chapter} \label{second}
\section{Second Chapter Section One} \label{second-section-section-one}
Current chapter label is: \CurrentChapterLabel

% The following is to test what happens if we attempt to access
% the value of \CurrentChapterLabel prior to setting a \label
% within this chapter
\chapter{Third Chapter}
Current chapter label is: \CurrentChapterLabel

\end{document}
share|improve this answer
    
You sir, are awesome. Exactly what I wanted! Thanks a lot! It works fine in an empty document, but for some reason it does not work in a document I'm currently working on. Seems that CurrentChapterLabel never gets set. Anyway, not your problem, I'll figure it out! Thanks! –  peanutman Aug 4 '12 at 22:45
    
Have updated to handle case where you attempt to access \CurrentChapterLabel prior to an invocation of \label. –  Peter Grill Aug 4 '12 at 22:51
    
I have found that the usage of "\usepackage{hyperref}" breaks your code. If hyperref is used the value is never set. I'll see if I can solve this. –  peanutman Aug 4 '12 at 23:06
    
Fixed it! To make it work you have to to do \usepackage{nameref}, \usepackage{hyperref}, before your macro. Note that the nameref is also important as it is a package referenced by hyperref that will overwrite your new label definitions if not loaded beforehand. Source. Thanks for the macro, it's awesome! –  peanutman Aug 4 '12 at 23:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.