13

One can use \everymath and \everydisplay to insert tokens at the beginning of math mode, but what about inserting tokens at end of math mode? Lets say I want to enclose the math mode material in a pair of DVI specials begin foo and end foo, how to do that:

\everymath{\special{begin foo}}
% where to put \special{end foo}
\everydisplay{\special{begin foo}}
% ditto

Regular text $a+b=c$ more text $$\sqrt{x}=y$$
\bye
7
  • 4
    does it have to be inside the math or does \aftergroup\foo count where \def\foo{\special{end foo}} ? Mar 22, 2015 at 12:07
  • @DavidCarlisle: it needs to be inside the math. Mar 22, 2015 at 12:24
  • That's unfortunate as it's not really possible unless you redefine $. (or use luatex I guess) but especially for the inline case there isn't really a lot of meaning to being inside or outside math, it has all been changed to a horizontal box list by that stage in any case. Mar 22, 2015 at 12:35
  • My actual use case is enclosing math mode material with \beginR and \endR commands and here it makes difference as TeX needs to see the \endR before finishing the packing of the math mode material, otherwise it will complain about missing \endR (I didn’t post that initially as it needs an unreleased version of XeTeX to be even able to use these primitives in math mode). Mar 22, 2015 at 12:43
  • Ah well that's a different beast as \beginR needs to work before tex converts the math to an hlist but specials are information to the processor of the dvi output so work after tex has finished. Mar 22, 2015 at 13:12

1 Answer 1

10

It's easy to get a special after the math:

\tracingonline1
\tracingoutput1
\showboxdepth100
\showboxbreadth100

\def\efoo{\special{end foo}}

\everymath{\special{begin foo}\aftergroup\efoo}
% where to put \special{end foo}
\everydisplay{\special{begin foo}\aftergroup\efoo}
% ditto

Regular text $a+b=c$ more text $$\sqrt{x}=y$$
\bye

Produces

...\mathon
...\special{begin foo}
...\teni a
...\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
...\tenrm +
...\penalty 700
...\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
...\teni b
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\tenrm =
...\penalty 500
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\teni c
...\mathoff
...\special{end foo}

and

..\hbox(8.49092+1.94444)x32.64345, shifted 218.55577, display
...\special{begin foo}
...\hbox(8.49092+1.90904)x14.04863
....\hbox(0.39998+9.6)x8.33336, shifted -7.69096
.....\tensy p
....\vbox(8.49092+0.0)x5.71527
.....\kern0.39998
.....\rule(0.39998+0.0)x*
.....\kern3.38542
.....\hbox(4.30554+0.0)x5.71527
......\teni x
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\tenrm =
...\glue(\thickmuskip) 2.77771 plus 2.77771
...\teni y
...\kern0.35878
..\penalty 0
..\glue(\belowdisplayshortskip) 7.0 plus 3.0 minus 4.0
..\glue(\baselineskip) 10.05556
..\hbox(0.0+0.0)x469.75499, glue set 469.75499fil
...\special{end foo}

To get the specials before the \mathoff or inside the display is trickier and probably involves making $ active which would be incompatible with some uses.

2
  • 2
    So this just confirms my understanding that TeX does not provide such a mechanism? Mar 22, 2015 at 12:26
  • @KhaledHosny Your understanding is correct.
    – egreg
    Mar 22, 2015 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .