(A follow-up of sorts on Variable chapter references, as per @werner 's suggestion)
This is somewhere in between a request for comments and an actual question (as well as a summary of my experiences whilst trying to implement the linked question's idea to my personal liking, I hope someone else finds it helpful too).
The idea is (in the same spirit as the varioref package) to avoid redundancy and awkwardness whilst (hyper)referencing.
So, suppose you have a (hyper)reference to chapter 4 from within chapter 5, I'd like that to come out as (a linked up) "the next chapter" rather than "chapter 5 on page XX" (italics added for emphasis), analogously if you're referencing chapter 3 from within chapter 4.
Likewise, I'd like this mechanism to work for sections, subsections, etc. all the way down to subparagraphs.
An initial solution
An initial (ie. considering \chapters alone) solution to the problem was posted in the linked question above, and it basically boils down to:
\usepackage{hyperref}
\usepackage{smartref}
\usepackage{cleveref}
\usepackage{xifthen}
\newcounter{actualchap}
\addtoreflist{chapter}
\newcommand{\relref}[1]{
\sgetchapterval{\targetchap}{#1}% Retrieve chapter counter from reference
\setcounter{actualchap}{\value{chapter}}
\addtocounter{actualchap}{1}% Next chapter
\ifthenelse{\equal{\targetchap}{\theactualchap}}%
{\hyperref[#1]{the next chapter}}%
{\addtocounter{actualchap}{-2}% Previous chapter
\ifthenelse{\equal{\targetchap}{\theactualchap}}%
{\hyperref[#1]{the previous chapter}}%
{\cref{#1}}%
}%
}
\crefformat{chapter}{#2chapter~#1#3}
\Crefformat{chapter}{#2Chapter~#1#3}
Note that we're using the hyperref package, the smartref package, the cleveref package, and the xifthen package.
Digression / explanation
Why do we need both smartref and cleveref? Well, because we'd like to get all of this nice functionality without sacrificing (hyper)reference customization. Every other solution I could come up with eventually ended up doing (directly or indirectly) something along the lines of:
\renewcommand*{\thechapter}{WHATEVER}
now, this breaks all of our carefully laid down smartref incantations: smartref relies on the .aux file containing untainted LaTeX references, whilst redefining \thechapter could make said entries contain LaTeX commands themselves (trying to get chapter numbers using \oldstylenums was a sure shot way of falling right into this), In other words: formatting is happening too soon! (it's present right there in the .aux file already)
If, on the other hand, we use cleverefexclusively (by using cleveref's \cref command instead of LaTeX's \ref one), we can achieve the same effect (see the \crefformat and \Crefformat commands above, see cleveref's documentation for an explanation on what the #2 and #3 parameters are and why do we need to define a capitalized and non-capitalized version of each) without ruining our other work: cleveref leaves laTeX's references alone, and it only applies formatting at the very end! (the second time LaTeX is run)
Question proper
So, this is pretty sweet, but still, there's room for improvement (and my TeXnical skills are pretty much nonexistent besides some debugging and .aux file digging):
I'd like this same mechanism to work on sections, subsections, etc. (as stated above) without having to define different commands (ie. the
\relrefcommand above should be able of discerning on its own whether the reference it's given is to a chapter, section, subsection, etc.),it should work on
*ed sectioning commands as well,it should play along nicely with
hyperrefwhilst doing all of this (I see no reason why this could not be so).
So... longish question indeed, wanted to share the experience gathered nevertheless.
Have I tickled your interest? :)
smartrefat all. Why not just usecleverefand\cref'? (If you *really* don't want to change all your\refs into\crefs, you can\let\ref\crefin your preamble. Though I recommend doing a search-and-replace \ref -> \cref in your source instead, to leave the original LaTeX\ref` intact just in case.) – Toby Cubitt Nov 21 '13 at 15:55