Generalproblem with /chapter* and /ref

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
schrader
Posts: 2
Joined: Wed Jul 15, 2009 4:29 pm

problem with /chapter* and /ref

Post by schrader »

Hi,

When I am using \chapter* command (to generate chapter without number in title) \ref command points to the last standard chapter (i.e. without *)

This is reproduction of my problem:

Code: Select all

\documentclass[a4paper]{book}
\begin{document}
\chapter{chap1}\label{lab1}
xxx

\addtocounter{chapter}{1}
\chapter*{chap2}\label{lab2}
yyy

\chapter{chap3}\label{lab3}
zzz \ref{lab2}

\end{document}
It gives:
Chapter 1
chap1
xxx
---
chap2
yyy
---
Chapter 3
chap3
zzz 1 (sic!)

Recommended reading 2024:

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

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

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

problem with /chapter* and /ref

Post by kaiserkarl13 »

\label refers to the last generated number, not the last sectioning command. For example, putting \label after a \caption command refers to the caption number of that float.

Since \chapter* doesn't generate a number, you can't define cross-references to it (what would this look like? Chapter [null]?), so \label grabs the number generated by the last numbering command (in this case, \chapter with no asterisk).

If you're looking to generate a \pageref cross-reference, try something like

Code: Select all

\chapter*{Chapter name}
\addtocounter{chapter}{-1}
\refstepcounter{chapter}
\label{chapterlabel}
This will leave the value of the chapter counter unchanged, but create a reference to that page in the .aux file. Just don't try to use \ref on it; that wouldn't make any sense.
schrader
Posts: 2
Joined: Wed Jul 15, 2009 4:29 pm

Re: problem with /chapter* and /ref

Post by schrader »

Thank You for help and explanation,

\refstepcounter{chapter} did that trick.
Post Reply