2

I've been trying several different approaches to place a box (a highlight red box) around equations in beamer. Though, I usually do not get the expected result, or a bunch of errors pops up and nothing compiles.

Below follows a MWE of three equations inside the align environment (ideally I want to keep this environment). I need a box that can go at each equation at a time (or more than one sometimes, but not all of them at once). Also, some methods I've tried could not place a box around the \sum correctly, usually cutting part of them out, or the iterators below them.

\documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{amsmath} \begin{document} \begin{align} & \left[ \omega \sum_{k \in K} y_k + \sum_{i \in V} \sum_{j \in V} \sum_{k \in K} c_{ij}x_{ijk}\right]\\ \notag\\ & \sum_{k \in K} \sum_{j \in V | j \ne i} x_{ijk} = 1 & & \forall i \in P\\ \notag\\ & \sum_{j \in V | j \ne i} x_{ijk} - \sum_{j \in V | j \ne i} x_{dev(i)jk} = 0 && \forall i \in P, k \in K \end{align} \end{document} 

Thanks in advance,

2 Answers 2

4

I'd recommend the hf-tikz package; it requires hand adjustments, but is completely independent of align.

\documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{hf-tikz} \begin{document} \begin{frame} \begin{align} \tikzmarkin{first}(0.2,0.75)(-0.2,-0.7) & \biggl[ \omega \sum_{k \in K} y_k + \sum_{i \in V} \sum_{j \in V} \sum_{k \in K} c_{ij}x_{ijk} \biggr] \tikzmarkend{first} \\[2ex] & \sum_{k \in K} \sum_{j \in V | j \ne i} x_{ijk} = 1 && \forall i \in P \\[2ex] \tikzmarkin{third}(0.2,0.6)(-0.2,-0.75) & \sum_{j \in V \mid j \ne i} x_{ijk} - \sum_{j \in V \mid j \ne i} x_{dev(i)jk} = 0 && \forall i \in P, k \in K \tikzmarkend{third} \end{align} \end{frame} \end{document} 

enter image description here

If you just want the red border, with true corners,

\documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage[customcolors]{hf-tikz} \hfsetfillcolor{white} \hfsetbordercolor{red} \begin{document} \begin{frame} \begin{align} \tikzmarkin[disable rounded corners=true]{first}(0.2,0.75)(-0.2,-0.7) & \biggl[ \omega \sum_{k \in K} y_k + \sum_{i \in V} \sum_{j \in V} \sum_{k \in K} c_{ij}x_{ijk} \biggr] \tikzmarkend{first} \\[2ex] & \sum_{k \in K} \sum_{j \in V | j \ne i} x_{ijk} = 1 && \forall i \in P \\[2ex] \tikzmarkin[disable rounded corners=true]{third}(0.2,0.6)(-0.2,-0.75) & \sum_{j \in V \mid j \ne i} x_{ijk} - \sum_{j \in V \mid j \ne i} x_{dev(i)jk} = 0 && \forall i \in P, k \in K \tikzmarkend{third} \end{align} \end{frame} \end{document} 

enter image description here

1
  • This one seems to work exactly how I wanted it to work. Nice answer, thank you, egreg :) Commented Nov 29, 2016 at 22:52
2

You can obtain it with the \Aboxed command from mathtools, but it can enclose a single &, so I had to remove the alignment of the .

For colour, I patched the \boxed command from amsmath, which is used by \Aboxed (but be aware all math boxed equations will be red). Additionally, I used \smashoperator in case of large subscript, to improve the spacing with the following expression:

\documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{mathtools}} \usepackage{xpatch} \xpatchcmd{\boxed}{% \fbox}{% \fcolorbox{red}{white}}{}{} \begin{document} \begin{align} \Aboxed{& \Biggl[ \omega\sum_{k \in K} y_k + \sum_{i \in V} \sum_{j \in V} \sum_{k \in K} c_{ij}x_{ijk}\Biggr]}\\ \notag\\ &\sum_{k \in K} \smashoperator[r]{\sum_{j \in V | j \ne i}} x_{ijk}= 1 \qquad \forall i \in P\\ \notag\\ \Aboxed{&\smashoperator[r]{\sum_{j \in V | j \ne i}} x_{ijk} - \smashoperator{\sum_{j \in V | j \ne i}} x_{dev(i)jk}= 0\qquad \forall i \in P, k \in K} \end{align} \end{document} 

enter image description here

5
  • Thank you for the answer, Bernard. I really preferred that the \forall appeared in an extra &. There is no way to do that? Moreover, is there any way to make the box red? Commented Nov 29, 2016 at 1:48
  • For the rule colour I added a patch to \ boxed. For the other alignment, I don't how to do that: when I tried which & it gobbled the second column. Maybe a work-around using eqparbox? Commented Nov 29, 2016 at 2:14
  • Oh well, I guess I'll have to use this way then. Thanks again anyway, you have been very helpful :) (Edit: I'll wait a bit before voting for best answer, in case anyone appears with a magic answer) Commented Nov 29, 2016 at 2:37
  • 1
    if boxing can't handle &, then the \foralls can be aligned manually, by using \mathrlap of the first part of equation (2) over a phantom of the first part of equation (3), and setting the second part of equation (2) after that.. Commented Nov 29, 2016 at 18:35
  • Boxing accepts only one &, it seems. I was thinking of using eqparbox, but didn't have to test yet Commented Nov 29, 2016 at 20:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.