Within a chapter/section scheme, Sometimes I want to be able to have a reference to a section, which would normally appear in the form c.s, where c is the chapter number and s is the section number, to appear just in the form s.
How?
Sample source:
\documentclass[12pt]{memoir}
\usepackage{hyperref}
\usepackage{cleveref}
\crefname{chapter}{Chapter}{Chapters}
\begin{document}
In section \ref{sec:more} of \cref{chap:one} \dots .
And in \cref{sec:one} \dots .
\chapter{One}\label{chap:one}
\section{About one}\label{sec:one}
\section{More about one}\label{sec:more}
\chapter{Two}
In section \ref{sec:more}
\end{document}
Conflict with creating two tables of contents
The answer that uses zref
works as shown in the answer, but when I add code, shown below, to the preamble in order to produce both a long and a short table of contents — with commands \shorttableofcontents
and \longtableofcontents
just after \begin{document}
, I get an error on the line where I use \shorttableofcontents
.
The error is:
Undefined control sequence
Here's the entire source, including the extra code for using zref
along with the code (courtesy of Christian Hupfer) for creating the two tables of contents.
\documentclass[12pt]{memoir}
\usepackage[colorlinks]{hyperref}
\usepackage{cleveref}
\usepackage{xparse}
\usepackage{xassoccnt}
\usepackage[user,hyperref]{zref}
\RegisterPostLabelHook{\zlabel}
\makeatletter
\zref@newprop{section}{\arabic{section}}% Change \arabic{section} by \Roman{section} etc. if needed
\zref@addprop{main}{section}
%
\newcommand{\secref}[1]{%
\zref@ifrefundefined{#1}{%
\ref{#1}% fall back to usual \ref if there's no such zref - specific label.
}{%
\hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{section}}%
}%
}
\makeatother
\crefname{chapter}{Chapter}{Chapters}
\crefname{section}{Section}{Sections}
%% SHORT & LONG TOCs
%% Code by Christian Hupfer
\makeatletter
% Just in case we're not loading hyperref
\@ifpackageloaded{hyperref}{%
}{
\providecommand{\@currentHref}{}
\providecommand{\hyperlink}[2]{#2}
\providecommand{\getrefnumber}[1]{}
\providecommand{\phantomsection}{}
}
\AtBeginDocument{%
\newcommand{\shortcontentsstuff}{%
\contentsline{chapter}{\hyperlink{\getrefnumber{toc::shorttoc::anchor}}{\shortcontentsname}}{\pageref{toc::shorttoc}}{\getrefnumber{toc::shorttoc::anchor}}
}
\newcommand{\longcontentsstuff}{%
\contentsline{chapter}{\hyperlink{\getrefnumber{toc::longtoc::anchor}}{\longcontentsname}}{\pageref{toc::longtoc}}{\getrefnumber{toc::longtoc::anchor}}
}
}
\newif\if@shorttoc
%
\renewcommand{\tableofcontents}{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\contentsname
\@mkboth{%
\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}
% Addition
\if@shorttoc
\protected@edef\@currentlabel{shorttoc}% Rather unimportant
\edef\@currentlabelname{\shortcontentsname}% For \nameref
\label{toc::shorttoc}%
% Now let us fix the hyperanchor for the short toc
\edef\@currentlabelname{}%
\protected@edef\@currentlabel{\@currentHref}\label{toc::shorttoc::anchor}
\else
\protected@edef\@currentlabel{longtoc}% Rather unimportant
\edef\@currentlabelname{\longcontentsname}
\label{toc::longtoc}%
% Now let us fix the hyperanchor for the long toc
\edef\@currentlabelname{}%
\protected@edef\@currentlabel{\@currentHref}\label{toc::longtoc::anchor}
\fi
% End of additions
\@starttoc{toc}%
\if@restonecol\twocolumn\fi
}
\newcommand{\longcontentsname}{Detailed Contents}
\newcommand{\shortcontentsname}{Short Contents}
%
\newcommand{\shorttableofcontents}[1][1]{%
\begingroup
\@shorttoctrue
\setcounter{tocdepth}{#1}
\let\shortcontentsstuff\relax
\phantomsection
\write\@auxout{\string\@writefile{toc}{\protect\longcontentsstuff}}
\let\contentsname\shortcontentsname% Disabling the \shortcontentsstuff in toc
\tableofcontents
\endgroup
\@shorttocfalse% Disable the short toc
}
\newcommand{\longtableofcontents}[1][3]{%
\begingroup
\setcounter{tocdepth}{#1}
\phantomsection
\let\longcontentsstuff\relax% Disabling the \longcontentsstuff in toc
\write\@auxout{\string\@writefile{toc}{\protect\shortcontentsstuff}}
\let\contentsname\longcontentsname
\tableofcontents
\endgroup
}
\makeatother
\begin{document}
\shorttableofcontents\clearpage
In section \secref{sec:more} of \cref{chap:one} \dots .
And in \cref{sec:one} \dots .
\chapter{One}\label{chap:one}
\section{About one}\label{sec:one}
\section{More about one}\label{sec:more}
\chapter{Two}
In section \ref{sec:more}
\end{document}
Trickier answer does work conflict-free
The second, "Trickier" version in the answer does work without conflicting with the code for two tables of contents.
\providecommand{\LastRefSteppedCounter}{}
to the preamble preferably before\RegisterPostLabelHook
. It was missing inxassoccnt
v.1.4. I will fix this feature/bug and providexassoccnt
v.1.5. soon\providecommand
by-pass solution is not necessary after that, but can be kept, since it is ignored then.