Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redefining \section, \subsection, etc. to automatically \label

116 views
Skip to first unread message

Jason Alexander

unread,
Oct 16, 1998, 3:00:00 AM10/16/98
to
In my documents, I often need to cross-reference sections. Since it
becomes tiresome to always have to write

\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

Donald Arseneau

unread,
Oct 16, 1998, 3:00:00 AM10/16/98
to
In article <36278B1D...@ea.oac.uci.edu>, Jason Alexander <ja...@ea.oac.uci.edu> writes...

> \renewcommand{\section}[2][]{\old@section[#1]{#2}\label{#2}}

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

Francois Patte

unread,
Oct 17, 1998, 3:00:00 AM10/17/98
to Jason Alexander
Jason Alexander wrote:

> \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

0 new messages