8

I'm looking to typeset certain references à la varioref thus so: if I reference chapter 3 from within chapter 2, I'd like the output to look like "...in the next chapter---" rather than "...in chapter 3 on page XXX...", similarly for sections, parts, etc.

Does anyone know of such a package or how I may go about tweaking varioref to suit my needs?

mpr
  • 3,646

1 Answers1

5

Here's an option you can try that uses the smartref package. We start off by called \addtoreflist{chapter} which defines the macro

\sgetchapterval{<macro>}{<lab>}

which stores the value of the chapter counter associated with label <lab> in <macro>. Then, using some \ifthenelse statements (provided by xifthen), we condition on whether the chapter reference is for the previous or the next chapter. If otherwise, we default to a varioref reference using \vref.

enter image description here

\documentclass{book}
\usepackage{smartref}% http://ctan.org/pkg/smartref
\usepackage{varioref}% http://ctan.org/pkg/varioref
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
\addtoreflist{chapter}% Provides chapter counter extraction
\newcounter{mychap}% Additional chapter counter

\newcommand{\chapref}[1]{%
  \sgetchapterval{\themychap}{#1}% Save chapter counter from reference in \themychap
  \setcounter{mychap}{\value{chapter}}%
  \addtocounter{mychap}{1}% Next chapter
  \ifthenelse{\equal{\mychap}{\themychap}}%
    {the next chapter}%
    {\addtocounter{mychap}{-2}% Previous chapter
     \ifthenelse{\equal{\mychap}{\themychap}}%
       {the previous chapter}%
       {Chapter~\vref{#1}}%
    }%
}%
\begin{document}
\chapter{First chapter} \label{chap:first}
See Chapter~\vref{chap:second} and Chapter~\vref{chap:last}. See \chapref{chap:second} or \chapref{chap:last}.
\chapter{Second chapter} \label{chap:second}
\chapter{Last chapter} \label{chap:last}
See Chapter~\vref{chap:second} and Chapter~\vref{chap:first}. See \chapref{chap:second} or \chapref{chap:first}.
\end{document}

I'm sure you could fine-tune this to your liking.

blaimi
  • 103
Werner
  • 603,163
  • Hmmm... my PDFLaTeX is spiking 100% CPU when trying to compile your example into my manuscript... when killing it I get some Missing \endcsname inserted errors :S – mpr Oct 23 '11 at 19:38
  • I don't have that problem with smartref 2002/02/28 v1.9 and varioref 2011/10/02 v1.4z. – Werner Oct 23 '11 at 20:00
  • possibly an outdated hyperref... I'm running TeXLive on Ubuntu... they're very anal about what gets to the distro :S – mpr Oct 23 '11 at 20:02
  • My MWE does not use hyperref, since non of the included packages use it. – Werner Oct 23 '11 at 21:22
  • I'm very sorry, I should have said so in the question... hyperref is to be loaded and supported (ie. the "next chapter" and "previous chapter" should be links rather than just text). I've been doing some testing on your code and found that it threw a lot of warnings if loading smartref before hyperref, inverting the loading order seemed to fix it, but now the code does not work (it always says "previous chapter"). Again, I'm sorry, should I edit the question to reflect this fact or should I just close it and start a new one? – mpr Oct 24 '11 at 12:09
  • Th mistake was on my side, too many hackish snippets. The answer wasrigh on the money. – mpr Oct 25 '11 at 15:21
  • I've been fiddling around with this piece of code and found that if you redefine \thechapter (in order to get, for instance Oldstyle numbers in body, newstyle numbers in section headings) this no longer works, outputting the \vref every time... – mpr Oct 25 '11 at 21:18
  • It seems that smartref is returning the formatted label text as the result of \sgetchapterval, which makes this solution incompatible with custom label formatting... do you have any ideas? – mpr Oct 26 '11 at 10:51
  • I'll have to look into this over the next couple of days. If it cannot be resolved in a reasonable amount of time, you could post a new question stating the connection to this one, including the new "oldstyle number" requirement. I think the Q&A as-is may be helpful in general, while yours is a specific case that may(?) require a different solution altogether. – Werner Oct 26 '11 at 15:45
  • A quick test with hyperref didn't yield anything fruitful. I would suggest wrapping it up here, asking a new question and referencing this one, since it may require a completely different strategy to incorporate both hyperref and oldstyle numbering. – Werner Oct 27 '11 at 03:56