Take the 2-minute tour ×
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.

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:

\documentclass[catalan]{book}
\usepackage{fancyhdr}
\usepackage{nameref}
\usepackage{minitoc}
\usepackage{hyperref}

\makeindex
\dominitoc

%\renewcommand{\thesection}{\arabic{section}}

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

\fancyhead{}
\fancyhead[LO]{\leftmark}
\fancyhead[RE]{\thechapter.\rightmark}

\pagestyle{fancy}

\begin{document}
\tableofcontents

\chapter{One}

\minitoc[e]

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

aaaaaa \newpage bbbbbbbb \newpage ccccccccccc

\chapter{Two}

\minitoc[e]

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

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

\end{document}
share|improve this question
12  
Please post a minimal working example, showing your document class and any significant packages you load. –  Joseph Wright Nov 30 '10 at 17:29
    
@meren: Actually (if I insert the document environment you forgot), the result is "See sections 1.1 and 2.1". Please verify that your examples compile and exhibit the behaviour. –  Ulrich Schwarz Dec 1 '10 at 8:23
1  
That's true!!! But in my complete project this is not the behavoir. I'll try to find which package redefines the behavoir. Thanks –  meren Dec 1 '10 at 9:03
    
I'm confused by what you want. The point a name reference is that it is not the number: a real section is likely to have a descriptive name, and something like section 1.1.Introduction seems unlikely to be desirable. –  Joseph Wright Dec 1 '10 at 9:28
    
So, what's your problem -- the fact that the ToCs use the prefixes too? Or just the fact that the header uses too many prefixes? –  SamB Dec 30 '10 at 18:10
show 1 more comment

1 Answer

up vote 6 down vote accepted

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.

\documentclass[catalan]{book}
\usepackage{fancyhdr}
\usepackage{nameref}
\usepackage{hyperref}
\usepackage{minitoc}

\makeindex
\dominitoc

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

\fancyhead{}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\pagestyle{fancy}

\makeatletter
\def\T@ref#1{%
        \NR@setref{#1}\checkforsectioni{#1}%
}
\def\checkforsectioni#1#2#3#4#5{%
        \checkforsectionii{#1}#4\@nil.\@nil\@nnil
}
\def\checkforsectionii#1#2.#3\@nil#4\@nnil{%
        \ifnum\pdfstrcmp{#2}{section}=\z@
                #3%
        \else\ifnum\pdfstrcmp{#2}{subsection}=\z@
                #3%
        \else
                #1%
        \fi\fi
}
\makeatother
\begin{document} \tableofcontents \chapter{One}

\minitoc[e]

\section{First section}
\label{sec:A}
aaaaaa \newpage bbbbbbbb \newpage
ccccccccccc

\chapter{Two} \minitoc[e]
\section{Second section}
\label{sec:B}

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

\end{document}

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.

share|improve this answer
    
Wow!!!!!!!!!!! Very tricky. I don't understant how does it works, but it does!!! Thanks a lot. –  meren Jan 18 '11 at 10:46
1  
@meren: I added some explanation for why it works. If this solves your problem, you should accept it so it doesn't appear in the unanswered questions list. –  TH. Jan 18 '11 at 11:25
add comment

Your Answer

 
discard

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.