\section{Foo\label{Foo}}
:
:
\subsection{Bar\label{Bar}}
I wanted to redefine \section, \subsection, etc. so that \section{Foo}
would automatically perform the \label'ing.
Now, knowing that \section can take an optional argument, I thought
something as simple as
\let\old@section=\section
\renewcommand{\section}[2][]{\old@section[#1]{#2}\label{#2}}
would work. It *almost* does. Apparently a starred-form of the
\section command exists and is used by the \bibliography command.
Could someone advise me what I need to do to make the redefinition work
for all possible uses of \section, \subsection, etc?
Also, I would appreciate any comments on the pros/cons of such a macro.
I.e., could a lengthy document (like a dissertation) exhaust TeX's
memory if too many unused cross-referencing commands get defined?
Thanks,
Jason
a) \section[]{title} is not the same as \section{title}!
b) You shouldn't use the whole title as a reference tag because
it will fail on all but simple text.
Donald Arseneau as...@triumf.ca
> \let\old@section=\section
> \renewcommand{\section}[2][]{\old@section[#1]{#2}\label{#2}}
>
> would work. It *almost* does. Apparently a starred-form of the
> \section command exists and is used by the \bibliography command.
> Could someone advise me what I need to do to make the redefinition work
> for all possible uses of \section, \subsection, etc?
I had recently the same problem. Here is the solution given by
Heiko Oberdiek <ober...@ruf.uni-freiburg.de>:
\documentclass{book}
\newcommand{\foo}{--Foo}
\makeatletter
\newcommand{\old@chapter}{} % Test if \old@chapter can be used.
\let\old@chapter\chapter
\renewcommand{\chapter}{%
\@ifstar{\new@chapterstar}{%
\@ifnextchar[{\new@chapteropt}{\new@chapter}%
}%
}
\newcommand{\new@chapterstar}[1]{%
\old@chapter*{#1\foo}%
}
\newcommand{\new@chapteropt}[2][]{%
\old@chapter[#1\foo]{#2\foo}%
}
\newcommand{\new@chapter}[1]{%
\old@chapter{#1\foo}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Normal chapter}
\chapter*{Chapter with star}
\chapter[Optional]{Chapter with optional argument}
\end{document}
-- François Patte. UFR de mathématiques et informatique.
45 rue des St Pères. 75270 Paris Cedex 06
Tel: 01 44 55 35 59 -- Fax: 01 44 55 35 35
http://www.math-info.univ-paris5.fr/~patte