4

I think the question is self-explanatory. I have an align environment with four equations and I want only the last two of them framed in a single box. So not separately framed.

If I try to achieve this by creating two align environments and boxing the entire second align environment then the equation groups are centered aligned and I dont want that. I want to preserve the alignment of the original group. Namely on the equal sign.

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
\begin{align*}
a =& b\\
c =& d\\
    \framebox{e =& FFF\\
f =& GGG}\\
\end{align*}
\end{document}
  • 1
    Not like that no, it hides the & from align. BTW: When you use =& write it as ={}& otherwise the spacing after = will be wrong. In this case you could just as well use &=. – daleif Jan 29 '15 at 13:06
6

Another option, using the tikzmark library from TikZ; \tikzmark allows the placement of marks to be used to draw the frame (which can be as fancy as desired):

enter image description here

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\draw
  ([shift={(-1em,2.5ex)}]pic cs:starta) 
  rectangle 
  ([shift={(1em,-1.25ex)}]pic cs:enda);
\draw[red!60!black,thick,rounded corners,fill=yellow!30]
  ([shift={(-1em,2.5ex)}]pic cs:startb) 
  rectangle 
  ([shift={(1em,-1.25ex)}]pic cs:endb);
\end{tikzpicture}

\begin{align*}
a &= b\\
c &= d\\
\tikzmark{starta}e &= FFF\\
f &= GGG\tikzmark{enda}
\end{align*}

\begin{align*}
a &= b\\
cc &= d\\
\tikzmark{startb}eee &= FFF\\
f &= GGG\tikzmark{endb}
\end{align*}

\end{document}

Since internal calculations are involved, the code needs two runs to stabilize.

In the same spirit, but with shorter code, you can use the hf-tikz package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hf-tikz}

\begin{document}

\renewcommand{\fcol}{white}
\renewcommand{\bcol}{black}
\begin{align*}
a &= b\\
c &= d\\
\tikzmarkin{a}(1em,-1.5ex)(-1em,2.5ex)e &= FFF\\
f &= GGG\tikzmarkend{a}
\end{align*}

\renewcommand{\fcol}{yellow!30}
\renewcommand{\bcol}{red!60!black}
\begin{align*}
a &= b\\
cc &= d\\
\tikzmarkin{b}(1em,-1.5ex)(-1em,2.5ex)eee &= FFF\\
f &= GGG\tikzmarkend{b}
\end{align*}

\end{document}

enter image description here

  • +1 just it gives me another motivation to really start learning TikZ! I have been planning this for so long and I am each time amazed at by its power. Only these internal calculations are a bit scary off course, even though I somehow grasp the sense of them. Thx again – Mathias Vanwolleghem Jan 29 '15 at 15:44
  • @MathiasVanwolleghem You're welcome! I've updated my answer with another option using this time the hf-tikz package (this gives shorter code). – Gonzalo Medina Jan 29 '15 at 15:56
  • as a last question @GonzaloMedina. This should also work to highlight certain elements of a matrix then ? – Mathias Vanwolleghem Feb 2 '15 at 14:45
  • @MathiasVanwolleghem Sure! The idea is the same: place marks at some strategic locations and then draw the frame using those marks. – Gonzalo Medina Feb 2 '15 at 16:06
  • OK but what I really want actually is transform the rectangle into a node and at a reference name to this node so that I can later use it to draw arrows between the boxes. Let me reformulate: suppose that in the above example we had boxed both the first two and the last two (of the four), how then could I have drawn an arrow between the two ? I was thinking adding a named node to both of them but I just can't get the syntax right. – Mathias Vanwolleghem Feb 2 '15 at 16:58
3

Certainly neither the most optimal solution nor the most general, but it works (in this peculiar case): using a combination of the \Aboxed command of the mathtools package (which loads and complements amsmath) and two aligned environments.

\documentclass{minimal}
\usepackage{mathtools}
\begin{document}
\begin{align*}
  &\begin{aligned}
    a &= b\\
    c &= d
  \end{aligned}\\
  \Aboxed{&{\begin{aligned}
    e &= FFF\\
    f &= GGG
  \end{aligned}}}
\end{align*}
\end{document}

enter image description here

Note the braces surrounding the second aligned environment: as explained in mathtools documentation:

As \Aboxed looks for the alignment & it may be necessary to hide constructions like matrices that also make use of &. Just add a set of braces around the construction you want to hide.

  • Note that the overall alignment only seems to fit here because a, c, e, f have a similar width. Counter measures might be needed if the widths are not the same (such as using \mathmakebox to make a right aligned boxed with a certain given width – daleif Jan 29 '15 at 13:55
  • You're right, I've just noticed that. Waiting for a more general solution then, if it exists… – Franck Pastor Jan 29 '15 at 13:58
  • if you look closely, you will see that the equals signs are not aligned between the boxed and not-boxed groups. unfortunately, i don't know the answer -- but it's a good question. – barbara beeton Jan 29 '15 at 13:58
  • @barbara beeton Indeed, same problem as noticed by daleif. In that case it is probably the bigger width of the f letter. – Franck Pastor Jan 29 '15 at 14:02
  • a "counter measure" that might work is to figure out what is the widest left-hand-side element, and force one other left-hand-side element (in the boxed or not-boxed portion(s)) to that same width. not elegant, but the easiest workaround i can think of. – barbara beeton Jan 29 '15 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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