1

I am writing a book and hope to create automatic labels for every Chapters and Sections in the form \label{AutoChap:"ChapTitle") and \label{AutoSec:SecTitle). Learning from "Automatic" section labeling?, I have got a small progress as showed in my WME.

\documentclass{memoir}
  \usepackage{letltxmacro}
  \LetLtxMacro{\oldsection}{\section}
  \renewcommand{\section}[2][]{\oldsection[#1]{#2}\index{#1}\label{sec:#1}}
  \LetLtxMacro{\oldchapter}{\chapter}
  \renewcommand{\chapter}[2][]{\oldchapter[#1]{#2}\index{#1}\label{chap:#1}}
\begin{document}
  \chapter[ABC]{ABC}
    \section[One]{One}
    \section{Two}
    \section*[Three]{Three}
     Test reference \ref{chap:ABC}
     Test reference \ref{chap:XYZ}
     Test reference \ref{sec:One}
     Test reference \ref{sec:Two}
   \chapter{DEF}
   \chapter*[XYZ]{XYZ}
\end{document}

However, there are still 2 problems:

  1. This code requires an assigned name ([]) for each Chapter and Section. For example, in my WME, it is working for Chapter ABC, Section One, but not for Chapter DEF, Section Two. Almost (but not all) my Chapters and Sections have not been assigned any Name, then I am seeking for a solution which could be working for both situations (assigning or not).

  2. It causes error for starred Chapters and Sections, for example Chapter XYZ or Section Three in my example. (And as a result, it creates troubles to my table of contents or Bibliography...) .

Please kindly advise me any possible solution for getting auto label without such problems. Many thanks.

EDIT. I have also tried the other suggestion (as showed below) in the same thread, "Automatic" section labeling?, which is:

\let\orisectionmark\sectionmark
\renewcommand\sectionmark[1]{\label{autosection::\thesection}\orisectionmark{#1}}

It might be better working for Section label, but still can not apply the same idea for Chapters' label. Please kindly help me.

3

I don't recommend using label names generated from chapter or section names, since those

  • names can change during the writing process of a document → the reference names have to be changed as well
  • names can occur multiple times → the multiply defined labels

However, the main reason the code from the O.P. does not work is memoir's 'weird' 2nd optional argument for the \chapter and \section macros.

This can be catched easily with xparse and \RenewDocumentCommand. I've have provided for an \@currentlabel approach that defaults to the chapter or section title for starred macros.

This approach works with hyperref as well.


\documentclass{memoir}
\usepackage{letltxmacro}

\usepackage{xparse}

\LetLtxMacro{\oldsection}{\section}
\LetLtxMacro{\oldchapter}{\chapter}

\makeatletter

\RenewDocumentCommand{\section}{sO{#4}O{#4}m}{%
  \index{#2}%
  \IfBooleanTF{#1}{%
    \addcontentsline{toc}{section}{#2}%
    \oldsection*{#4}\protected@edef\@currentlabel{#3}%
  }{%
    \oldsection[#2][#3]{#4}%
  }%
  \label{sec:#2}%
}


\RenewDocumentCommand{\chapter}{sO{#4}O{#4}m}{%
  \index{#2}%
  \IfBooleanTF{#1}{%
    \oldchapter*[#2]{#4}\protected@edef\@currentlabel{#2}%
  }{%
    \oldchapter[#2][#3]{#4}%
  }%
  \label{chap:#2}%
}

\usepackage{hyperref}


\makeatother
\begin{document}
\tableofcontents
\chapter[ABC]{ABC}
\section[One]{One}
\section{Two}

\section*[Three]{Three}


Test reference \ref{chap:ABC}

Test reference \ref{chap:XYZ}

Test reference \ref{sec:One}

Test reference \ref{sec:Two}
\chapter{DEF}
\chapter*[XYZ]{XYZ}

\printindex
\end{document}

enter image description here

  • I don't think that memoir's second optional argument for \chapter and \section is "weird". It caters for those who need different length entries in the ToC, header and text. This met requests by many users. It seems to me to be no more "weird" than provision for different entries in the ToC and text; why not just the text entry? – Peter Wilson Mar 20 '18 at 19:55
  • @PeterWilson: 'Weird' in the sense that users that redefine memoir's structure units don't take that into account – user31729 Mar 20 '18 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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