3

I renewed my command for chapter/section...etc So I don't see the chapter number in all (sub)sections name. Also the point was that when I'm in a chapter and I use \ref to reference to a section of the current chapter I only see the number of the section (and not the chapter number before it). Here are my \renewcommand that I'm using for that :

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}

But now I want also to be able to reference a section from another chapter and in that case I want the number of the chapter to appear. I can't find a way to do that... my desperate attempt to remedy to this problem is currently to use that :

\newcommand\fullref[2]{\ref{#1}.\ref{#2}}

where the two arguments are the label of the chapter and the label of the section in this chapter. So with that command I have what I want the reference to look like but I obviously have 2 hypertext links. Is there anyway to have something similar to that but with one unique link that points to the section?

Here is a full standalone example :

\documentclass[a4paper]{report}
\usepackage{hyperref}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}

\newcommand\fullref[2]{\ref{#1}.\ref{#2}}

\begin{document}

\begin{titlepage}
\end{titlepage}


\chapter{Hello}
\label{chapter: Hello}

\section{I'm Brian}
\label{section: Brian}

\section{I'm from the US}
\label{section: US}

A section from this chapter don't need to have the chapter number in it's reference : \ref{section: Brian}.

But a section from an other chapter should have it : \fullref{Chapter: Welcome}{section: Bob}


\chapter{Welcome}
\label{Chapter: Welcome}

\section{I'm Bob}
\label{section: Bob}

\section{I'm from the Canada}
\label{section: Canada}


\end{document}
3
  • Using chapter numbers in one case and omitting them in other ones is not really straightforward and leaves inconsistencies within the document. The reader might get confused
    – user31729
    Commented Feb 19, 2015 at 22:01
  • 1
    @Christian Hupfer: I don't think so. Mentioning the number of the current chapter always seemed absurdly redundant to me. I'd like to know how to do it.
    – Bernard
    Commented Feb 19, 2015 at 22:25
  • @Bernard: Me too. I imagine something like with zref for example or a hack into \label and \ref.
    – user31729
    Commented Feb 19, 2015 at 22:27

3 Answers 3

3

I assume this doesn't happen often, otherwise you would have stuck with the default hierarchical definitions of sectional numbers, including chapters from the get-go. To that end, define a new \label macro, say \totallabel which prepends \thechapter. to the existing label that you can reference as usual using \ref:

enter image description here

\documentclass[a4paper]{report}
\usepackage{hyperref}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}

\newcommand\fullref[2]{\ref{#1}.\ref{#2}}

\makeatletter
\newcommand{\totallabel}[1]{% \totallabel{<label>}
  \edef\@currentlabel{\thechapter.\@currentlabel}% Prepend \thechapter. to current label
  \label{#1}%
}
\makeatother

\begin{document}

\chapter{Hello}
\label{chap:Hello}

\section{I'm Brian}
\label{sec:Brian}

\section{I'm from the US}
\label{sec:US}

A section from this chapter don't need to have the chapter number in it's reference : \ref{sec:Brian}.

But a section from an other chapter should have it: \fullref{chap:Welcome}{sec:Bob} \ref{chap:sec:Bob}


\chapter{Welcome}
\label{chap:Welcome}

\section{I'm Bob}
\label{sec:Bob}\totallabel{chap:sec:Bob}% Insert a \totallabel

\section{I'm from the Canada}
\label{sec:Canada}

\end{document}
4
  • Very good. I like this.
    – touhami
    Commented Feb 19, 2015 at 23:31
  • @Werner: I see, we had similar ideas ;-) Your's is shorter... (damn! ;-))
    – user31729
    Commented Feb 19, 2015 at 23:41
  • 1
    @ChristianHupfer: Yes. I like yours since it makes the additional label part of the regular label, with the prepended "tag".
    – Werner
    Commented Feb 19, 2015 at 23:49
  • @Werner thank you! It doesn't happen often in fact and this is a perfect fix! Commented Feb 20, 2015 at 0:11
3

This uses a 'hack'

The label is stored using the \@currentlabel approach, which evaluates the current settings of the \the.... counter format output macro.

I restored it temporarily, used \@currentchapterlabel, forced it to be \@currentlabel and added an automatic additional label prefixed with chapterfullabel::, which is written similarly, all in a group, such that no outer macros are effected.

The reference is done with \fullref{labelname} which automatically uses the label tag. I don't recommend to redefine \ref for such things.

\documentclass{book}

\usepackage{xpatch}

\usepackage[hypertexnames=true]{hyperref}

% Save the definitions of \the.... macros first 

\let\latexthechapter\thechapter
\let\latexthesection\thesection
\let\latexthesubsection\thesubsection
\let\latexthesubsubsection\thesubsubsection

% Now redefine
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}

\makeatletter

% Generate a user-defined tag for the full chapter label
\newcommand{\fullreftagname}{chapterfulllabel::}

% Command for restoring \the.... macros
\newcommand{\@@restorechapterformat}{%
  \let\thechapter\latexthechapter
  \let\thesection\latexthesection
  \let\thesubsection\latexthesubsection
  \let\thesubsubsection\latexthesubsubsection
}%

\xapptocmd{\refstepcounter}{%
  \begingroup%
  \@@restorechapterformat%  Temporarily use the full format
  \protected@xdef\@currentchapterlabel
  {\csname p@#1\endcsname\csname the#1\endcsname}%
  \endgroup}{\typeout{Great Success}}{\typeout{Miserable fail}}

\AtBeginDocument{%  Must be here due to hyperref`s change
  \let\LaTeXLabel\label%
  \renewcommand{\label}[1]{%
    \begingroup
    \let\@currentlabel\@currentchapterlabel%
    \LaTeXLabel{\fullreftagname#1}% Write another label with \fullreftagname prefix
    \endgroup
    \LaTeXLabel{#1}% regular label
  }%
}

\newcommand{\fullref}[1]{%
  \ref{\fullreftagname#1}%
}
\makeatother


\begin{document}
\tableofcontents
\chapter{First}

\section{Introduction} \label{sec::introduction}

\subsection{Background} 

\chapter{Other chapter}

\section{Solution of everything} \label{sec::solution_of_everything}

\subsection{The world formula -- at last}

\subsubsection{More down} \label{subsubsec::something}

\chapter{Last chapter}

In \fullref{sec::introduction} we saw... whereas in \fullref{sec::solution_of_everything}, however in 
\fullref{subsubsec::something}

\end{document}

enter image description here

1

What about this, but you still have the problem of table of contents

\documentclass[a4paper]{report}
\usepackage{hyperref}

\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\quad}% 
{\csname #1@cntformat\endcsname}}

\def\section@cntformat{\arabic{section}\quad}
\def\subsection@cntformat{\arabic{section}.\arabic{subsection}\quad}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\makeatother

\begin{document}

\begin{titlepage}
\end{titlepage}

\tableofcontents
\chapter{Hello}
\label{chapter: Hello}

\section{I'm Brian}
\label{section: Brian}
\subsection{I'm Brian}

\section{I'm from the US}
\label{section: US}

A section from this chapter don't need to have the chapter number in it's reference : \ref{section: Brian}.

But a section from an other chapter should have it : \ref{section: Bob}

\chapter{Welcome}
\label{Chapter: Welcome}

\section{I'm Bob}
\label{section: Bob}

\section{I'm from the Canada}
\label{section: Canada}

\end{document}
3
  • Which problem with the ToC do you refer to?
    – user31729
    Commented Feb 19, 2015 at 23:40
  • section numbers still Roman.arabic I.1 and not just 1
    – touhami
    Commented Feb 19, 2015 at 23:49
  • Oh, I see...it requires restoring the old formats when \addcontentsline acts...
    – user31729
    Commented Feb 19, 2015 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .