Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have no idea how to make a global setting to box every displayed and inline math. The box is decorated with colored frames, colored background and colored foreground (either text or math).

By making the global setting, I can easily turn it on or off and the remaining is left untouched.

I know there are 2 macros, \everydisplay and \everymath, but I cannot make use of them other than changing the stroke color with \color{red}, for example.

share|improve this question
As you already found out, \everymath and \everydisplay can be used only to insert something "in front of" the math material. For boxing, you at the very least have to insert something on both sides, even better to "grab" the math material so it can be boxed. Furthermore, for displayed math, additional questions about what should happen with numbers or page breaks arise. In the end, this will get really complicated with lots of special cases needing to be handeled. If you're lucky, you'll find some package doing a similar thing which can be used as a specimen. –  Stephan Lehmke Aug 25 '12 at 5:04
@StephanLehmke I'm not a guru, but maybe \aftergroup could help? I tried something with that and did not succeed, but maybe there are other who know what to do... –  yo' Aug 25 '12 at 5:52
@tohecz I tried that also, no luck. The main problem is, however, that inline math mode is used for a lot of things internally (like vertical centering or \textsuperscript) so this will turn into an odyssey. –  Stephan Lehmke Aug 25 '12 at 6:02
@StephanLehmke Yes, that is maybe the part of the problem that cannot be solved. As well I think that display math is used sometimes to horizontally center some stuff. –  yo' Aug 25 '12 at 6:23

1 Answer 1

up vote 13 down vote accepted

For playing the following example adds a \fbox around inline math:




$abc$ Hello\textsuperscript{World}





Update: Explanation and \mathsurround=0pt added.

The goal is to have something like


If \everymath is called, we are already in math mode. Therefore the first $ ends the math mode. The space, given by \mathsurround would be set around the empty formula, therefore it is set to zero.

Then a horizontal box is opened (lrbox) and we switch in math mode again, but with empty \everymath. Via \aftergroup we get to the point right after the closing $ and we can close the box and set the box with \fbox.

Variation added.

Also we could let the math group in place and put the lrbox inside:


The difference of these methods is the handling of \mathsurround, if it is not zero. In the first case the space is put inside the box, in the second case, the space is outside.

However, there is a serious drawback of this general method via \everymath. Inline math is used at many different places, thus the example also shows boxes at places where inline math is not expected.

Inline math with \fbox

share|improve this answer
Great trick. Why are the $ needed? –  Marco Daniel Aug 25 '12 at 7:38
@MarcoDaniel lrbox start of in horizontal mode (it's an hbox). No boxes start in math mode. –  Joseph Wright Aug 25 '12 at 8:56
I wouldn't way it's a drawback of this method, rather the other way around; it's a drawback to use mathmode to define non-math stuff. –  morbusg Aug 25 '12 at 12:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.