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

2 Answers2

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.

  • my file is an overkill by itself, so I'm fine with this, thanks – ɪdɪət strəʊlə Feb 05 '18 at 15:56
  • Oh, it doesn't scale with taller equations – ɪdɪət strəʊlə Feb 05 '18 at 16:04
  • @st.vit It does, please see my edit. –  Feb 05 '18 at 16:05
  • ooh it worked, but it is overkill indeed now – ɪdɪət strəʊlə Feb 05 '18 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. –  Feb 05 '18 at 16:20
  • how would you implement a macro that finds extrema (leftest, rightest, topest, bottomest) of the equation and sets them as nodes? – ɪdɪət strəʊlə Feb 06 '18 at 21:54
  • @st.vit The first question is whether this is what you really want. The second question is how "the equation" is defined. One may come up with a macro that does it based on the observation that \left and \right "know" how high the stuff is between them. But the macro might be fragile. –  Feb 06 '18 at 21:59
  • I don't know if that's what I really want, but I'm asking for the knowledge of it, because "why not?". Well, I have no idea how \lefts and \rights know it, I've never dug that. – ɪdɪət strəʊlə Feb 06 '18 at 22:07
  • @st.vit I'm unfortunately not very good at writing super robust macros. What you essentially are asking is a package. With \left and \right I mean that if you type \[\left(\frac{x}{y}\right)\] then the brackets automatically have the right size. Of course, this does not work if there is a & inbetween, e.g. \begin{align} \left( & \frac{x}{y}\right)\end{align} will cause an error. This is also the reason why a simple \fbox doesn't work in your problem.There are workarounds, but IMHO in the end it is better to adjust things as I did in the above example. –  Feb 06 '18 at 23:08
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}
John Doe-Nut
  • 115
  • 10