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.

(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):

  1. I'd like this same mechanism to work on sections, subsections, etc. (as stated above) without having to define different commands (ie. the \relref command above should be able of discerning on its own whether the reference it's given is to a chapter, section, subsection, etc.),

  2. it should work on *ed sectioning commands as well,

  3. it should play along nicely with hyperref whilst 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? :)

share|improve this question
    
I don't understand why you need smartref at all. Why not just use cleveref and \cref'? (If you *really* don't want to change all your \refs into \crefs, you can \let\ref\cref` in 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

1 Answer 1

up vote 1 down vote accepted

About your first question, I think you should be able to use the \getreftype{..} command as defined here (\getreftype{#1} in your case) to discern the type of the reference that is the parameter of your function.

I think something like that should work :

\newcounter{actualelement}

\addtoreflist{chapter}
\addtoreflist{section}
\addtoreflist{subsection}
% ... and more

\newcommand{\relref}[1]{
    \csname sget\getreftype{#1}val\endcsname{\target}{#1}% Retrieve 'target' counter from reference
    \setcounter{actualelement}{\value{\getreftype{#1}}}% Current 'element'
    \addtocounter{actualelement}{1}% Next 'element'
    \ifthenelse{\equal{\target}{\theactualelement}}%
        {\hyperref[#1]{the next \getreftype{#1}}}%
        {%
            \addtocounter{actualelement}{-2}% Previous 'element'
            \ifthenelse{\equal{\target}{\theactualelement}}%
                {\hyperref[#1]{the previous \getreftype{#1}}}%
                {\cref{#1}}%
        }%
}

\crefformat{chapter}{#2chapter~#1#3}
\Crefformat{chapter}{#2Chapter~#1#3}
\crefformat{section}{#2section~#1#3}
\Crefformat{section}{#2Section~#1#3}
\crefformat{subsection}{#2subsection~#1#3}
\Crefformat{subsection}{#2Subsection~#1#3}

But no idea at this time for points 2 and 3 ...

share|improve this answer
1  
Did you test your suggestion? –  qubyte Feb 19 '12 at 6:07
    
Yes, I was using it a few times ago (december, I guess). But I just realised I forgot to link to the page with the macro getreftype{..} ! My bad. –  XaF Feb 19 '12 at 8:42
1  
I think you meant to link here ;) –  mpr Feb 19 '12 at 17:27
    
@mpr you're right, I didn't remember exactly were I originally found the idea of this macro, and I have found the link I refered to in my history :) –  XaF Feb 19 '12 at 21:03

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.