Next: , Up: Equation references


4.11.1 Formatting equation references

Both defining an equation label and referring to it should usually produce output. This output is produced with the \eqprint macro, which takes one argument, the equation number being defined or referred to. By default, this just produces `(number)', where number is the equation number. To produce the equation number in a different font, or with different surrounding symbols, or whatever, you can redefine \eqprint. For example, the following definition would print all equation numbers in italics. (The extra braces define a group, to keep the font change from affecting surrounding text.)

     \def\eqprint#1{{\it (#1)}}

In addition to changing the formatting of equation numbers, you might want to add more structure to the equation number; for example, you might want to include the chapter number, to get equation numbers like `(1.2)'. To achieve this, you redefine \eqconstruct. For example:

     \def\eqconstruct#1{\the\chapternumber.#1}

(If you are keeping the chapter number in a count register named \chapternumber, naturally.)

The reason for having both \eqconstruct and \eqprint may not be immediately apparent. The difference is that \eqconstruct affects the text that cross-reference label is defined to be, while \eqprint affects only what is typeset on the page. The example just below might help.

Usually, you want equation labels to refer to equation numbers. But sometimes you might want a more complicated text. For example, you might have an equation `(1)', and then have a variation several pages later which you want to refer to as `(1*)'.

Therefore, Eplain allows you to give an optional argument (i.e., arbitrary text in square brackets) before the cross-reference label to \eqdef. Then, when you refer to the equation, that text is produced. Here's how to get the example just mentioned:

     $$...\eqdef{a-eq}$$
     ...
     $$...\eqdef[\eqrefn{a-eq}*]{a-eq-var}$$
     In \eqref{a-eq-var}, we expand on \eqref{a-eq}, ...

We use \eqrefn in the cross-reference text, not \eqref, so that \eqprint is called only once.

As another example, consider the following requirement: we want to include chapter number in all equation references, and additionally we want to include the part number when referencing an equation from any part other than the one where the equation appears. For example, references to the third equation in chapter 2 of part 1 should be typeset as `(2.3)' throughout part 1, but as `(I.2.3)' in any other part. Let's assume we have the current chapter and part numbers in count registers \chapnum and \partnum, respectively.

The idea is to have \eqconstruct store the part number of the equation (that is, the part number at the time of definition), so that later \eqprint can compare the stored number with the current part number (that is, the part number at the time of reference). The complicating factor is that internally, the result of \eqconstruct is both expanded and written out to the .aux file, and used to typeset the equation number, so the commands that store the part number should behave correctly in both situations. This is difficult to achieve with expandable commands; therefore, to avoid expansion problems, we are going to use only TeX primitives, which are non-expandable:

     \newcount\eqpartnum
     
     \def\eqconstruct#1{%
       \global\eqpartnum=\the\partnum\relax
       \number\chapnum.#1%
     }
     
     \def\eqprint#1{%
       \setbox0=\hbox{#1}%
       (\ifnum\partnum=\eqpartnum \else
          \uppercase\expandafter{\romannumeral\eqpartnum}.%
        \fi
        \box0)%
     }%

In \eqconstruct, besides constructing the base equation number (e.g., `1.2'), we also store the part number of the equation in the count register \eqpartnum (\the\partnum is expanded when the equation number is written to the .aux file, so the equation label definition in the .aux file will contain the actual part number). In \eqprint, we need to know the equation's part number before we typeset the base equation number, therefore we first put the argument in a box, thus causing \eqpartnum to be set.