20

As I read and found out the command \thepage doesn't always output the correct page number.

The proposed alternative is to use \pageref{here}\label{here}. But this seems too unhandy, because I have to change the label every time I want to get the current page, which makes it impossible to just replace it by a simple new command.

P.S.: In the end I want to define this:

\newcommand{\see}[1]{\cref{#1}\ifthenelse{\equal{\pageref{#1}}{\thepage}}{}{ on \cpageref{#1}}}

where the part on \cpageref{#1} should only be output if it is not the current page.

3 Answers 3

8

You can easily combine the \pageref{here}\label{here} in one command by using e.g. a counter to avoid multiple labels:

\documentclass{article}
\newcounter{labelhere}
\makeatletter
\newcommand\pagerefhere{%
 \stepcounter{labelhere}%
 \pageref{here\thelabelhere}\label{here\thelabelhere}}
\begin{document}

\pagerefhere blub \newpage \pagerefhere bla

\end{document}
17

Simply load varioref before cleveref and use \vref in the document body. See section 2.3 of the varioref manual for details about suppressing textual references.

\documentclass{article}

\usepackage{varioref}
\renewcommand\reftextfaceafter{on page~\thevpagerefnum}
\renewcommand\reftextfacebefore{on page~\thevpagerefnum}
\renewcommand\reftextafter{on page~\thevpagerefnum}
\renewcommand\reftextbefore{on page~\thevpagerefnum}

\usepackage{cleveref}

\begin{document}

\section{foo}\label{foo}

\clearpage

\section{bar}\label{bar}

\section{gnu}

See also \vref{foo}.

See also \vref{bar}.

\end{document}

Output (p. 2):

enter image description here

4
  • Thank you, this seams to work with for the pages but it doesn't include the ref types e.g. section …, listing … and so on. And it doesn't really answer my original question. May 23, 2013 at 12:39
  • @white_gecko Check my example again -- reference types are included with \vref if you also load cleveref.
    – lockstep
    May 23, 2013 at 13:10
  • Sorry, I don't know, what I'm doing wrong but it doesn't include the types, not for Listings, not for Sections and not for Figures. Could it be because I use German in my document? May 29, 2013 at 17:32
  • Ok, it works in a separate document, now have to figure out, why it doesn't in my main document May 29, 2013 at 17:43
3

In my experience, using several packages which enhance cross referencing in LaTeX, leads to problems or odd behavior. Luckily modern versions of varioref is able to do some of the things cleveref can as well and, depending on your exact needs, it may not be necessary to use both packages.

If you are looking for a single macro, which will output "section 1.2", "section 1.2 on the next page", "section 1.2 on page 14", "chapter 4 on page 51", "equation (2.5) on the facing page", etc. depending on what the \label references and where \label and \ref is placed, varioref should be enough. With

\usepackage{varioref}
\labelformat{chapter}{chapter~#1}
\labelformat{section}{section~#1}
\labelformat{equation}{equation~(#1)}

in the preamble, \vref{label} will result in the output above and there should be no need for a custom macro. Please note, that \vref will print a space before the reference and that the space will be suppressed if using \vref* instead. If you start a sentence with a reference and therefore need the first letter capitalized, use \Vref.

I you prefer "on page 11" to "on the next page" for every reference, you should include the following lines from lockstep's answer in your preamble.

\renewcommand\reftextfaceafter{on page~\thevpagerefnum}
\renewcommand\reftextfacebefore{on page~\thevpagerefnum}
\renewcommand\reftextafter{on page~\thevpagerefnum}
\renewcommand\reftextbefore{on page~\thevpagerefnum}

cleveref has features that varioref lacks, such as referencing ranges, and according to the author of cleveref it should be able to replace varioref entirely. From the cleveref manual:

The enhanced referencing feature provided by the varioref package's \labelformat command decides how to format cross-references when the label is defined, rather than when it is referenced. Often this isn’t a problem. But it makes it impossible to format cross-references differently according the context in which they are referenced, which can sometimes be very useful. For example, if you want cross-references at the beginning of a sentence formatted any other way than by capitalising the first letter of the cross-reference text, it is impossible using varioref. E.g. you may want to use the abbreviation “eq.”, but revert to "Equation" at the beginning of sentences (words at the start of sentences shouldn’t be abbreviated in English). This is not possible with varioref. Perhaps even more significantly, varioref’s \labelformat implementation makes it impossible to typeset multiple references automatically; if you want to refer to equations eq1 through eq3, with varioref you are back to typing Eqs.~(\ref{eq1}) to~(\ref{eq3}) by hand. Not to mention missing out on all the other cleveref features. In fact, cleveref fully supports varioref, taking over responsibility for typesetting cross-references, whilst retaining (and even enhancing) all the varioref page-referencing magic.

I myself have not had use for the features of cleveref and find varioref easier to use.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .