[Tex/LaTex] How to get the chapter number like a prefix of all references


This was the problem I posted:
"I need to output "see section 2.3" where the referenced section is the third on the second chapter."

You all are right. I've to redefine the problem.

Sections are referenced in a lot of distinct places:

  • In table of contents. Managed by \tableofcontents
  • In minitocs. Managed by minitoc package
  • In headers. Managed by fancyhdr package
  • Like a leader section. Managed by \section
  • In text. Managed by \ref

In tocs and minitocs, because each section appears below its chapter (with some indentation) it's not necessary to prefix the section number with the chapter number. The label scope is local; so I need a local renderization. Example:

                  1. Chapter One ------------------1
                       1. First section -----------1
                       2. Second section ----------3
                   2. Chapter Two -----------------5
                       1. First section ...........5

This means that sections 1.1 and 2.1 must be rendered as section 1.

When we use the \section command we know what chapter we are in: it's shown in the headers of the page. So I want also local scope:

             1. First section
                       bla bla
             2. Second section
                       bla bla

In headers I want global scope:

             1. Chapter One                      1.1. First section

In text we can make a crossreference to whatever point of text. So we need global scope:

                See section 1.1 (first section in first chapter)

This is the problem:
The default behavoir give as global scope every where. But if I redefine \thesection to local scope, then I redefine everywhere.

This is my code:




\renewcommand{\chaptermark}[1]{\markboth{\thechapter\ #1}{}}
\renewcommand{\sectionmark}[1]{\markboth{\thechapter.\thesection\ \ #1}}






\section{First section}\label{sec:A}

aaaaaa \newpage bbbbbbbb \newpage ccccccccccc



\section{Second section}\label{sec:B}

See sections \ref{sec:A} and  \ref{sec:B}


Best Answer

I think you had the marks backward. You want the right mark on odd pages and the left mark on even pages.

So this is a bit of a hack, but it seems to work to get references to sections to appear how you want.



\renewcommand{\chaptermark}[1]{\markboth{\thechapter\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thechapter.\thesection\ #1}}



\begin{document} \tableofcontents \chapter{One}


\section{First section}
aaaaaa \newpage bbbbbbbb \newpage

\chapter{Two} \minitoc[e]
\section{Second section}

See sections \ref{sec:A}, \ref{sec:B}, and \ref{sec:C}.


This works because hyperref writes entries in the aux file that look like

\newlabel{sec:A}{{1}{3}{First section\relax }{section.1.1}{}}

If you trace the execution of the \ref, you see that \T@ref expands to \NR@setref{#1}\@firstoffive{#1} and the \@firstoffive gets used before the second argument of \newlabel. That is, it eventually expands to

\@firstoffive{1}{3}{First section\relax }{section.1.1}{}

You can get a different behavior by replacing \@firstoffive with a 5 argument macro. In this case, \checkforsectioni which absorbs 5 arguments and then uses TeX's delimited argument capability to grab the text before the first period (if one exists) in its 4th argument. If that text is section or subsection, then the text that gets used is that after the first period, namely 1.1 in this case. Otherwise, it does the normal thing and expands to the first argument.

Related Question