# Intelligent \vref

I want to use the package varioref, but using abbreviations ... (p.1) instead of ... on page 1. Unfortunately, I have also references in parentheses, where this would lead to unaesthetic/forbidden (?) double closing brackets.

I tried using \@ifnextchar to recognize this case, but this seems not to work.

Here, a minimal example:

\documentclass[]{article}
\usepackage{varioref}
\usepackage{cleveref}
\usepackage[english]{babel}

\makeatletter
\addto\extrasenglish{%
\def\reftextfaraway#1{\@ifnextchar){on p.~\pageref{#1}}{(p.~\pageref{#1})}}%
}
\makeatother

\begin{document}
In \vref{sec:test}, this is explained.
Other text (cf.\ \vref{sec:test}).

\newpage
\null\newpage
\section{Test}
\label{sec:test}
\end{document}


Intended output:

In chapter 1 (p. 3), this is explained. Other text (cf. chapter 1 on p. 3).

Current output:

In chapter 1 (p. 3), this is explained. Other text (cf. chapter 1 (p. 3)).

• In my opinion, a simple separating comma would look nicer, and would avoid this difficulty. – Bernard Mar 17 '16 at 19:19
• @Bernard thanks for this suggestion. How can it be implemented? How can the space before the \reftextfaraway removed? – jofel Mar 18 '16 at 14:20
• @Bernard I found the solution for the space before comma problem: \unskip fixes the problem. – jofel Mar 18 '16 at 14:30
• Fine! Does there remain any problem to solve? – Bernard Mar 18 '16 at 14:43

## 1 Answer

Here I save the definition of \vref and then redefine it with two arguments. I check if the second argument is a ) and redefine \reftextfaraway accordingly. I then call on the saved version of \vref with the first argument and pass freely the 2nd argument.

\documentclass[]{article}
\usepackage{varioref}
\usepackage{cleveref}
%\usepackage[english]{babel}

\let\svvref\vref
\makeatletter
%\addto\extrasenglish{%
\renewcommand\vref[2]{%
\ifx)#2%
\renewcommand*{\reftextfaraway}[1]{on p.~\pageref{#1}}%
\else
\renewcommand*{\reftextfaraway}[1]{(p.~\pageref{#1})}%
\fi
\svvref{#1}#2%
}%
%}
\makeatother

\begin{document}
In \vref{sec:test}, this is explained.
Other text (cf.\ \vref{sec:test}).

\newpage
\null\newpage
\section{Test}
\label{sec:test}
\end{document}


• Thanks for this nice suggestion. I found two minor glitches: It seems that it eats spaces after \vref (which can be solved by either using \vref{...}\  where spaces are needed, or using \svvref{#1}\xspace #2 with the xspace package). In addition, it destroys \vref*. – jofel Mar 18 '16 at 14:16
• @jofel I see that \vref* is to remove the space to the left of the citation. However, I note that merely loading the cleveref package breaks \vref*, so I am not sure what resolution you might like to see, given that you load cleveref. – Steven B. Segletes Mar 18 '16 at 14:25
• Thanks for clarifying \vref* with cleverref. Please forget this part of my comment. – jofel Mar 18 '16 at 14:27
• @jofel As to the space issue, your workarounds may be the best available. By absorbing two arguments into the redefined \vref, any space between them is lost. But I will think on it a bit more. – Steven B. Segletes Mar 18 '16 at 14:28