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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the proper way to reference a section with a counter in the title? The TOC generates correctly, but the cross references aren't so lucky.


\documentclass[letterpaper, 12pt, twoside]{exam}


        \section*{Lesson \arabic{KSRLessonNumberCounter}: #2}
        \addcontentsline{toc}{section}{Lesson \arabic{KSRLessonNumberCounter}: {#2}}



This is the first lesson. The second lesson is \nameref{sec:blah}.


This is the second section

\KSRSection{Lorem Ipsum}

Dollars are Set in \nameref{sec:blah}.


MWE: wrong section numbers

share|improve this question
up vote 10 down vote accepted

It is an expansion issue. The title string is not expanded and contains:

Lesson \arabic {KSRLessonNumberCounter}: Blah

Package hyperref loads nameref, which uses package gettitlestring to get a clean title string (removing \label, \index, ...). There are two methods, the traditional way without expansion or the version with expansion. The former is the default. By switching to method expand the problem is solved:

\usepackage{nameref}% loads gettitlestring
share|improve this answer

A different solution than Heiko's that uses standard names. You simply enter \subsection for subsections, without the need of *; for the reference to a section, use \lectref.

\documentclass[letterpaper, 12pt, twoside]{exam}


\renewcommand{\@seccntformat}[1]{Lesson \csname the#1\endcsname: }

  \hyperref[#1]{Lesson~\ref*{#1}: \nameref*{#1}}%




This is the first lesson. The second lesson is \lectref{sec:blah}.


This is the second section

\subsection{A subsection}

Here it is

\section{Lorem Ipsum}

Dollars are Set in \lectref{sec:blah}.


I used colorlinks to show them in the picture.

enter image description here

share|improve this answer

Your Answer


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.