4

I am aware of both this and this questions. However, can we combine them? What I mean by this is can I enclose in a fancy box à la empheq part of an equation in an align environment?

(I believe there is no need for a MWE since the question is simple and has MWEs in both linked questions; however if you need a MWE comment and I'll add one)

EDIT: Adding MWE

\documentclass{minimal}

\usepackage{color}
\definecolor{myblue}{rgb}{.8, .8, 1}
\usepackage{empheq}
\usepackage{amsmath,mathtools}

\newlength\mytemplen
\newsavebox\mytempbox

\makeatletter
\newcommand\mybluebox{%
\@ifnextchar[%]
{\@mybluebox}%
{\@mybluebox[0pt]}}

\def\@mybluebox[#1]{%
\@ifnextchar[%]
{\@@mybluebox[#1]}%
{\@@mybluebox[#1][0pt]}}

\def\@@mybluebox[#1][#2]#3{
\sbox\mytempbox{#3}%
\mytemplen\ht\mytempbox
\advance\mytemplen #1\relax
\ht\mytempbox\mytemplen
\mytemplen\dp\mytempbox
\advance\mytemplen #2\relax
\dp\mytempbox\mytemplen
\colorbox{myblue}{\hspace{1em}\usebox{\mytempbox}\hspace{1em}}}

\makeatother

\begin{document}
\begin{align}
\Aboxed{\text{stuff} &= \text{other stuff}}
\end{align}
\begin{empheq}[box={\mybluebox[5pt]}]{equation*}
\text{stuff} = \text{other stuff}
\end{empheq}
Now, both?
\end{document}

Thanks, in before

0

2 Answers 2

5

If you don't mind loading TikZ, you may try

\documentclass{minimal}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds}
\newcommand{\tikznode}[2]{\tikz[remember picture,baseline=(#1.base)]{\node(#1)[inner sep=0pt]{#2};}}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{myblue}{rgb}{.8, .8, 1}
\usepackage{empheq}
\usepackage{amsmath,mathtools}

\begin{document}
\begin{align}
\tikznode{1}{\strut}\text{stuff} &= \text{other stuff}\tikznode{2}{\strut}
\end{align}\tikz[remember picture,overlay]{\node[draw,fit=(1) (2)]{};}
\begin{align}
\tikznode{3}{\strut}\text{stuff} = \text{other stuff}\tikznode{4}{\strut}
\end{align}\tikz[remember picture,overlay]{
\node[fit=(3) (4),fill=blue,fill opacity=0.25]{};}
Now, both?
\begin{align}
\tikznode{5}{\strut}\text{stuff} = \text{other stuff}\tikznode{6}{\strut}
\end{align}\tikz[remember picture,overlay]{
\node[draw,fit=(5) (6),fill=blue,fill opacity=0.25]{};}
It also works if you want to put the box around several lines
\begin{align}
\tikznode{7}{\strut}E &= m_0\ c^2\notag\\
&=\frac{m}{\sqrt{1-v^2/c^2}\tikznode{9}{\strut}}c^2\tikznode{8}{\strut}
\end{align}\tikz[remember picture,overlay]{
\node[draw,fit=(7) (8) (9),fill=blue,fill opacity=0.25]{};}
As you see in this example, you need to place the tikznodes in the most extreme
positions. 
\end{document}

enter image description here Of course, loading TikZ just for this might be overkill, but if you load it anyway and/or aim at even fancier styles, it might be worthwhile.

9
  • my file is an overkill by itself, so I'm fine with this, thanks Feb 5, 2018 at 15:56
  • Oh, it doesn't scale with taller equations Feb 5, 2018 at 16:04
  • @st.vit It does, please see my edit.
    – user121799
    Feb 5, 2018 at 16:05
  • ooh it worked, but it is overkill indeed now Feb 5, 2018 at 16:15
  • @st.vit Depends on what you want, of course. One might cook up a macro that sets the nodes in a way that the macro thinks is best, but then you lose the flexibility to shade only parts of an equation and so on. As I said, I personally would use this only if I have loaded TikZ anyway and/or if I want to add fancier stuff such as a gradient in the shading, shadows, reflections etc. All these things are super simple with TikZ.
    – user121799
    Feb 5, 2018 at 16:20
4

Like this?

enter image description here

\documentclass{minimal}

\usepackage{color}
\definecolor{myblue}{rgb}{.8, .8, 1}
\usepackage{empheq}
\usepackage{amsmath,mathtools}

\newlength\mytemplen
\newsavebox\mytempbox

\makeatletter
\newcommand\mybluebox{%
\@ifnextchar[%]
{\@mybluebox}%
{\@mybluebox[0pt]}}

\def\@mybluebox[#1]{%
\@ifnextchar[%]
{\@@mybluebox[#1]}%
{\@@mybluebox[#1][0pt]}}

\def\@@mybluebox[#1][#2]#3{
\sbox\mytempbox{#3}%
\mytemplen\ht\mytempbox
\advance\mytemplen #1\relax
\ht\mytempbox\mytemplen
\mytemplen\dp\mytempbox
\advance\mytemplen #2\relax
\dp\mytempbox\mytemplen
\setlength{\fboxsep}{0pt}%
\fbox{\colorbox{myblue}{\hspace{1em}\usebox{\mytempbox}\hspace{1em}\mathstrut}}}

\makeatother

\begin{document}
\begin{align}
\Aboxed{\text{stuff} &= \text{other stuff}}
\end{align}
\begin{empheq}[box={\mybluebox[5pt]}]{equation*}
\text{stuff} = \text{other stuff}
\end{empheq}
Now, both?
\end{document}
3
  • Nope nope, I need the fancy box inside an align environment. kind of \somefancybox{a &= b} where all of this is inside align. Feb 5, 2018 at 15:22
  • 1
    regardless of other attributes, the second example would be improved by having a "margin" on the bottom. add a \mathstrut. Feb 5, 2018 at 15:55
  • Didn't know about that. Thanks @barbarabeeton Feb 6, 2018 at 18:58

You must log in to answer this question.

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