Tell me more ×
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.

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?

share|improve this question

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
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
show 5 more comments

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.
http://tex.stackexchange.com/questions/32478/variable-chapter-references
tex.stackexchange.com
crawl=CC-MAIN-2013-20
47133
application/http; msgtype=response
sha1:SCZLF6D247XMAMXV72KZR7CQKVTNV57Q
<urn:uuid:fcf25214-29cb-4938-a86b-7784ce771235>
2013-05-23T12:02:54Z
198.252.206.16
sha1:XVPBRHBPD3ZVFLRMJUM5G5ZTQESNVS7K
<urn:uuid:24938398-cbd4-4991-be85-ab984ad908a1>
http://tex.stackexchange.com/questions/32478/variable-chapter-references
response
<urn:uuid:8e10fbfb-67b1-43d7-9374-69b6bcab9d2e>
robots: classic
hostname: ip-10-60-113-184.ec2.internal
software: Nutch 1.6 (CC)/CC WarcExport 1.0
isPartOf: CC-MAIN-2013-20
operator: CommonCrawl Admin
description: Wide crawl of the web with URLs provided by Blekko for Spring 2013
publisher: CommonCrawl
format: WARC File Format 1.0
conformsTo: http://bibnum.bnf.fr/WARC/WARC_ISO_28500_version1_latestdraft.pdf