Any box commands in math mode need to be set in all styles so TeX can pick the right one later; the \mathchoice
command is what you need here. See my rant against \over
on this site the other day:-) If you just use \hbox{$...$}
you are starting a fresh math list at text size even in subscript position.
You can re-assert the correct spacing around your construct using \mathbin
\mathop
and friends, This could be done automatically by looking up the\mathcode
of the character you are boxing. Inspecting bm.sty
might be helpful, which does this to re-assert the correct math class after making things bold, but the issues are similar.
It's a bit late to re-create bm
interrogation of \mathcode
so this just looks at special cases \cdot
and +
but otherwise does something plausible I suspect
UPDATE At the end I place a version that does automatically pick up the math class from character tokens, things like \cdot
defined by \mathchar
and things like \log
with explicit \mathop
. I will leave the original version in place as the math class detection probably obscures the logic of the measuring which was perhaps the main part of the question.
\documentclass{article}
\usepackage{amsmath,color}
\makeatletter
\def\fb@cdot{\cdot}
\def\fb@p{+}
\def\@mfbox#1{%
\def\fb@{#1}%
\ifx\fb@\fb@cdot\mathbin\fi
\ifx\fb@\fb@p\mathbin\fi
{\text{\fboxsep\z@\colorbox{yellow}{$\m@th#1$}}}}
\def\@tfbox#1{{\fboxsep\z@\colorbox{yellow}{#1}}}
\def\fillbox#1{\ifmmode\expandafter\@mfbox\else\expandafter\@tfbox\fi{#1}}
\begin{document}
\vskip1em
$\fillbox{p}_{\fillbox{x}}
\mathrel{\stackrel{\fillbox{_{~+}}}{\fillbox{\leftarrow}}}
\fillbox{(}\fillbox{\frac{\fillbox{1}}{\fillbox{2}}}\fillbox{\cdot}
\fillbox{a}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}^{\fillbox{2}}\fillbox{)} \fillbox{+}
\fillbox{(}\fillbox{v}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}\fillbox{)}$\fillbox{;}\par
$\fillbox{v}_{\fillbox{x}}
\mathrel{\stackrel{\fillbox{_{~+}}}{\fillbox{\leftarrow}}}
\fillbox{a}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}$\fillbox{;}\par
\vskip1em
$p_x \mathrel{\stackrel{_{~+}}{\leftarrow}}
(\frac{1}{2}\cdot a_x\cdot\Delta t^2) + (v_x\cdot\Delta t)$;\par
$v_x \mathrel{\stackrel{_{~+}}{\leftarrow}} a_x\cdot\Delta t$;\par
\end{document}
Version using bm
-style math class detection.
\documentclass{article}
\usepackage{amsmath,color}
\makeatletter
\def\fb@eat#1#2#3#4#5{\futurelet\fb@let@token\fb@eat@}
\def\fb@eat@#1\fb@eat{%
\ifx\fb@let@token\bgroup
\else\ifx\fb@let@token\mathop
\mathop
\else\ifx\fb@let@token\mathbin
\mathbin
\else\ifx\fb@let@token\mathrel
\mathrel
\else\ifx\fb@let@token\mathopen
\mathopen
\else\ifx\fb@let@token\mathop
\mathop
\else\ifx\fb@let@token\mathpunct
\mathpunct
\else\ifcat.\ifcat a\noexpand\fb@let@token.\else\noexpand\fb@let@token\fi
\afterassignment\fb@mathchar\count@\mathcode`#1\relax\fb@eat
\else\ifx\fb@let@token\mathchar
\afterassignment\fb@mathchar\expandafter\count@\@gobble#1\relax\fb@eat
\else
\xdef\meaning@{\meaning\fb@let@token}%
\expandafter\fb@mchar@test\meaning@""\@nil
\fi\fi\fi\fi\fi\fi\fi\fi\fi
}
\def\@mfbox#1{%
\begingroup
\let\protect\empty
\expandafter\fb@eat\romannumeral`\Q#1\relax\fb@eat
\ifcase\count@
\or
\mathop\or
\mathbin\or
\mathrel\or
\mathopen\or
\mathclose\or
\mathpunct\or
\fi
{\text{\fboxsep\z@\colorbox{yellow}{$\m@th#1$}}}%
\endgroup}
\edef\fb@mchar@{\meaning\mathchar}
\def\fb@mchar@test#1"#2"#3\@nil{%
\xdef\meaning@{#1}%
\ifx\meaning@\fb@mchar@
\count@"#2\relax
\fb@mathchar\fb@eat
\fi
}
\def\fb@mathchar#1\fb@eat{%
\divide\count@"1000 }
\def\@tfbox#1{{\fboxsep\z@\colorbox{yellow}{#1}}}
\def\fillbox#1{\ifmmode\expandafter\@mfbox\else\expandafter\@tfbox\fi{#1}}
\begin{document}
$a-b$
$a{-}b$
$a\fillbox{-}b$
$\log x + \mathrm{log}x$
$\fillbox{\log} \fillbox{x} \fillbox{+} \fillbox{\mathrm{log}}\fillbox{x}$
$\fillbox{0}$
$ a \cdot b {\cdot} c$
$ a \fillbox{\cdot} b \fillbox{{\cdot}}c $
$a \fillbox{\mathchar"2201} b \fillbox{{\mathchar"2201}} c $
$ a - b < \alpha $
$\fillbox{a} \fillbox{-} \fillbox{b} \fillbox{<} \fillbox{\alpha}$
$\fillbox{a+b}$
$a = \sqrt{h}$
$a = \fillbox{\sqrt{h}}$
\vskip1em
$\fillbox{p}_{\fillbox{x}}
\mathrel{\stackrel{\fillbox{_{~+}}}{\fillbox{\leftarrow}}}
\fillbox{(}\fillbox{\frac{\fillbox{1}}{\fillbox{2}}}\fillbox{\cdot}
\fillbox{a}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}^{\fillbox{2}}\fillbox{)} \fillbox{+}
\fillbox{(}\fillbox{v}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}\fillbox{)}$\fillbox{;}\par
$\fillbox{v}_{\fillbox{x}}
\mathrel{\stackrel{\fillbox{_{~+}}}{\fillbox{\leftarrow}}}
\fillbox{a}_{\fillbox{x}}\fillbox{\cdot}%
\fillbox{\Delta}\fillbox{t}$\fillbox{;}\par
\vskip1em
$p_x \mathrel{\stackrel{_{~+}}{\leftarrow}}
(\frac{1}{2}\cdot a_x\cdot\Delta t^2) + (v_x\cdot\Delta t)$;\par
$v_x \mathrel{\stackrel{_{~+}}{\leftarrow}} a_x\cdot\Delta t$;\par
\end{document}
Producing
A solution with hf-tikz
(requires two compilation runs):
\documentclass[dvipsnames]{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}} % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}} % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}} % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}} % substitution
\hfsetfillcolor{Lavender!50}
\hfsetbordercolor{white}
\begin{document}
\[
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(
\tikzmarkin{a}(0.01,-0.175)(-0.015,0.3)\frac{p}{\const{\hbar}}\tikzmarkend{a}
\var{x} - \tikzmarkin{b}(0.01,-0.175)(-0.015,0.3)\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
\]
\end{document}
The result:
Remarks:
- it is better to use
\[ \]
rather than $$ $$
;
- the package allows you to not set in math mode the text inside a
\colorbox
: one just have to declare where the delimiters of the box should be positioned.
From version 0.3 of the package, things are a bit more user-friendly as shown in hf-tikz doesn't box around equation. The above code can be written thanks to styles as:
\documentclass[dvipsnames]{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}} % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}} % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}} % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}} % substitution
% available from version 0.3
\tikzset{offset def/.style={
above left offset={-0.015,0.3},
below right offset={0.01,-0.175},
},
hl/.style={
offset def,
set fill color=Lavender!50,
set border color=white,
}
}
\begin{document}
\[
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(
\tikzmarkin[hl]{a}\frac{p}{\const{\hbar}}\tikzmarkend{a}
\var{x} - \tikzmarkin[hl]{b}\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
\]
\end{document}
Procedure to get .png output
After reading the comments below, here is a way to get a .png
. The standalone
class here really helps since it has a png
class option.
Save as test.tex
:
\documentclass[dvipsnames,png, border=2pt]{standalone}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}} % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}} % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}} % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}} % substitution
\hfsetfillcolor{Lavender!50}
\hfsetbordercolor{white}
\begin{document}
$
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(
\tikzmarkin{a}(0.01,-0.175)(-0.015,0.3)\frac{p}{\const{\hbar}}\tikzmarkend{a}
\var{x} - \tikzmarkin{b}(0.01,-0.175)(-0.015,0.3)\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
$
\end{document}
and compile with:
pdflatex -shell-escape test.tex
This will create
- test.pdf
- test.png
perfectly cropped.
Best Answer
You can use
TikZ
with theoverlay
option and a correct anchor. That would look like this:You can specify extra options (like I have done here for the color). By using the
overlay
option and thetext
anchor we ensure correct placement and no influence on spacing. Thephantom
is added to get the normal spacing for the content of the box. This is the result of the example code:Edit: To show that the vertical spacing is not affected either, consider the following:
Which results in: