Hi,

a couple of times the question came up¹²³ how to create more
intelligent referencing. There are already mechanisms in the core,
namely \somewhere and \atpage but they both have drawbacks.
\somewhere happily prints its text if the figure is placed on the
same double page. In fact, no text at all should be printed if the
figure is visible. Furthermore it's a little verbose to use unless
hidden in a custom macro. \atpage on the other hand doesn't hesitate
to print “see figure 1.2 at page 42” while you're on page 42.

I'm aware that automatic generation of reference text is problematic
since the text depends on the placement of floats which in turn
depends on the text which might result in oscillation. I'm not sure
how likely this is to occur in practice.

I don't know if there's interest in integrating such functionality
into the core. I attached some code, a new macro \smartref, which
takes the same arguments as \in.

  \smartref{figure}[fig:somefigure]

- it prints the reference if it is on the same page
  (e.g. Figure 1.2)

- it prints the reference and a customizable text if the reference
  is on the next/previous page (e.g. Figure 1.2 on the previous page)

- it prints the reference and the page if the reference is further
  away than one page (e.g. Figure 1.2 on page 42)

- it adapts to single-sided and double-sided layouts


Marco

¹ http://thread.gmane.org/gmane.comp.tex.context/65295
² http://thread.gmane.org/gmane.comp.tex.context/59455
³ http://thread.gmane.org/gmane.comp.tex.context/76001
\unprotect

\unexpanded\def\strc_references_smart
  {\strc_references_start_goto
   \let\currentreferencecontent\currentreferencedefault
   \strc_references_pickup_smart_goto}

\def\strc_references_pickup_smart_goto
  {\dodoublegroupempty\strc_references_pickup_goto_smart_indeed}

\starttexdefinition strc_references_pickup_goto_smart_indeed 
#left#right#dummy[#label]
  \leftreferencetoks
  \iffirstargument
    {#left}
  \else
    \defaultleftreferencetoks
    \let\leftofreferencecontent\empty
  \fi
  \rightreferencetoks
  \ifsecondargument
    {#right}
  \else
    \defaultrightreferencetoks
    \let\rightofreferencecontent\empty
  \fi
  \doifreferencefoundelse{#label}
    {\goto{\referencesequence}[#label]
     \strc_references_check_visibility
     \ifreferenceisvisible\else
       \space\strc_references_smart_string
     \fi}
     {\let\currentreferencecontent\dummyreference
      \goto{\referencesequence}[#label]}
  \strc_references_stop_goto
\stoptexdefinition

\newif\ifreferenceisvisible \referenceisvisiblefalse

\starttexdefinition strc_references_check_visibility
  \def\strc_references_smart_string{\labeltext\v!atpage\currentreferencepage}
  \referenceisvisiblefalse

  \ifnum\referencepagestate=\plusone %% 1 = reference on current page
    \referenceisvisibletrue
  \else
    \ifsinglesided
      \strc_references_do_relative_else\plusone
        {\def\strc_references_smart_string{\labeltext\v!nextpage}}
        {\strc_references_do_relative\minusone
         {\def\strc_references_smart_string{\labeltext\v!previouspage}}{}}
    \else
      \ifodd\realpageno
        \strc_references_do_relative_else\minusone
          {\def\strc_references_smart_string{\labeltext\v!previouspage}
           \referenceisvisibletrue}
          {\strc_references_do_relative_else\plusone
           {\def\strc_references_smart_string{\labeltext\v!nextpage}}{}}
      \else
        \strc_references_do_relative_else\minusone
          {\def\strc_references_smart_string{\labeltext\v!previouspage}}
          {\strc_references_do_relative_else\plusone
           \referenceisvisibletrue{}}
      \fi
    \fi
  \fi
\stoptexdefinition

\starttexdefinition strc_references_do_relative_else #relativepagenumber
  \ifnum\currentreferencerealpage=\numexpr\realpageno+#relativepagenumber\relax
    \expandafter\firstoftwoarguments
  \else
    \expandafter\secondoftwoarguments
  \fi
\stoptexdefinition

\let\smartref\strc_references_smart

\startinterface all
  \setinterfacevariable {previouspage} {previouspage}
  \setinterfacevariable {nextpage}     {nextpage}
\stopinterface

\setuplabeltext
  [\s!en]
  [\v!atpage=on page\nobreakspace, %% “at page” sounds weird
   \v!previouspage=on the previous page,
   \v!nextpage=on the next page]

\protect

\useMPlibrary [dum]
\setuppagenumbering [alternative=doublesided]

\starttext

      See \smartref{figure}[fig:alpha] and \smartref{figure}[fig:gamma].
\page See \smartref{figure}[fig:alpha] and \smartref{figure}[fig:gamma].

\startplacefigure [reference=fig:alpha] \externalfigure \stopplacefigure
\startplacefigure [reference=fig:beta]  \externalfigure \stopplacefigure

\page See \smartref{figure}[fig:alpha] and \smartref{figure}[fig:gamma].

\startplacefigure [reference=fig:gamma] \externalfigure \stopplacefigure
\startplacefigure [reference=fig:delta] \externalfigure \stopplacefigure

\page See \smartref{figure}[fig:alpha] and \smartref{figure}[fig:gamma].
\page See \smartref{figure}[fig:alpha] and \smartref{figure}[fig:gamma].

\stoptext

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to