Using labels as counter values

  • latex
  • macros

Labels are tempting sources of “numbers” - their most common use, after all, is simply to typeset a number. However, their seeming simplicity is deceptive; the packages babel and hyperref, at least, fiddle with the definition of \ref and \pageref in ways that make

snippet.latex
\setcounter{foo}{\ref{bar}}

(etc.) not work; thus the technique may not be relied upon.

The solution is to use the refcount package (incidentally, by the author of hyperref). The package provides four commands, all similar to:

snippet.latex
\usepackage{refcount}
...
\label{bar}
...
\setcounterref{foo}{bar}

(the other three are \addtocounterref, \setcounterpageref and \addtocounterpageref).

The package also provides a command \getrefnumber{label-name} that may be used where a “number” value is needed. For example:

snippet.latex
... \footnote{foo bar ...\label{foofoot}}
...
\footnotemark[\getrefnumber{foofoot}]

which gives you a second footnote mark reference the the footnote. (There is also a command \getpagerefnumber, of course).

The commands could be used by one determined not to use changepage to determine whether the current page is odd, but it's probably no more trouble to use the fully-developed tool in this case.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Creative Commons Lizenzvertrag Edit this page Old revisions Sitemap Backlinks RSS feed Impressum Flattr this