TransWikia.com

How to change cleveref references to 'verbal' varioref style?

TeX - LaTeX Asked by VisorZ on June 19, 2021

I would like to use cleveref‘s automatic ref target type recognition but with the more verbal page reference of varioref. This is my MWE:

documentclass{report}
usepackage{varioref}       
%usepackage{cleveref}     % note cleveref package changes type of ref..
                           % and style of references to last/next page
begin{document}

        % This figure appears on page 1.
        begin{figure}[!htb]
          centering
          rule{24pt}{24pt}
          caption{The famous black square}
          label{OneBlackSquare}
        end{figure}

        % This sentence appears on page 2.
        clearpage
        The non-color black is shown in vref{OneBlackSquare}.

end{document}

It produces a nice to read verbal reference when using the varioref package:

varioref verbal description

The only thing missing is the automatic target type. Enabling the cleveref package adds this very good feature but also overrides all verbal behavior of varioref. Regardless of the space to the target all references are located by page number instead of “on next page”.

cleveref automatic type recognition

But how can I combine them to get:

  1. the automatic target type like figure or equation of cleveref and
  2. the verbal description of the target’s position if it is on the last/next page to improve the reader’s flow?

Edit after a week: I received no solution proposal and both documentation do not solve my problem. Hence I stick with varioref and write the type of floating object hard coded in the text.

2 Answers

A nice bug! There is a space character missing in the cleveref package. Line 2337 currently reads:

        advance@tempcnta-2%

but should read:

        advance@tempcnta-2 %

yes, it is that space character (see What are the differences between TeX counts and LaTeX counters?).

I have notified Toby Cubitt, but for now you can either fix your package source locally, or overwrite the change in your preamble as follows:

documentclass{report}
usepackage{varioref}    
usepackage{cleveref}

makeatletter
defcref@old@@vpageref#1[#2]#3{%
    leavevmode%unskip  <<<
    globaladvancec@vrcnt@ne%
    vref@pagenum@tempa{thec@vrcnt @vr}%
    vref@pagenum@tempb{thec@vrcnt @xvr}%
    %vref@label{thec@vrcnt @xvr}%  <<<
    ifx@tempa@tempbelse%
      vref@err{noexpandvref or noexpandvpageref at page boundary
                @tempb-@tempaspace (may loop)%
                }%
    fi%
    vrefpagenumthevpagerefnum{#3}%
    vref@space%
    ifx@tempathevpagerefnum%
      def@tempc{#1}%
      ifx@tempc@empty%
         unskip%
      else%
         #1%
      fi%
    else%
      #2%
      is@pos@numberthevpagerefnum%
         {%
          is@pos@number@tempa%
           {@tempcnta@tempa%
            advance@tempcnta@ne%
           }%
           {@tempcntamaxdimen}%
          ifnum thevpagerefnum =@tempcnta%
           ifodd@tempcnta%
             if@twoside%
               reftextfaceafter%
             else%
               reftextafter%
             fi%
           else%
             reftextafter%
           fi%
          else%
            advance@tempcnta-2 %  <--- there was a space missing here!
            ifnum thevpagerefnum =@tempcnta%
              ifodd@tempcnta%
                reftextbefore%
              else%
                if@twoside%
                  reftextfacebefore%
                else%
                  reftextbefore%
                fi%
              fi%
            else%
              reftextfaraway{#3}%
            fi%
          fi%
         }%
         {reftextfaraway{#3}}%
    fi%
    vref@label{thec@vrcnt @xvr}%  <<<
    vref@label{thec@vrcnt @vr}%
  }%
makeatother

begin{document}

        % This figure appears on page 1.
        begin{figure}[!htb]
          centering
          rule{24pt}{24pt}
          caption{The famous black square}
          label{OneBlackSquare}
        end{figure}

        % This sentence appears on page 2.
        clearpage
        The non-color black is shown in vref{OneBlackSquare}.

end{document}

This gives the expected:

The non-color black is shown in Fig. 1 on the preceding page.

Correct answer by Karalga on June 19, 2021

Something like:

newcommand{refthing}[1]{cref{#1}, vpagerefnearby{#1}{vpageref{#1}}{cpageref{#1}}}

should do it.

Answered by Michael Saunders on June 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP