Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to define a command that puts a red box around an equation with yellow highlighting. I have the basics working pretty well. What I am unable to figure out is:

  1. How to keep the alignment of the equations on the equal sign.
  2. Be able to have the alignment character within the call to highlight box. That is, I would really prefer to be able to say something like:

    \highlightbox{g &= f}

instead of what I resorted to doing below.

Here is what I have so far:

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\newcommand{\highlightbox}[1]{%
    \setlength{\fboxrule}{6pt}\fcolorbox{red}{yellow}{#1}\quad%
}

\begin{document}
\begin{align*}
   \highlightbox{a} &\highlightbox{= b} \\
                 c  &              = d \\
                 e  &              = f  \\
   \highlightbox{g} &\highlightbox{= f} \\
\end{align*}
\end{document}
share|improve this question

migrated from stackoverflow.com Mar 16 '11 at 22:36

This question came from our site for professional and enthusiast programmers.

    
This \Aboxed sol'n seems to work great. Now how do I go about changing the name from Aboxed (don't like overwriting existing commands). I tried the obvious and it does not work. Also, any way to include multiple align points on same line with/without boxing? –  Peter Grill Mar 17 '11 at 23:55
    
@PeterGrill: Welcome to tex.sx! Your comment won't be seen by many people here so it would be best to repost it as a fresh question. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. –  Joseph Wright Mar 18 '11 at 6:49
    
@PeterGrill: Multiple align points can be done as one would normally do it with align, I've updated the example below to show this. As for the name, I do not know enough about TeX to help, what I've tried so far haven't worked. –  Torbjørn T. Mar 19 '11 at 17:25
add comment

2 Answers 2

up vote 21 down vote accepted

The mathtools package provides an Aboxed command, that allows one to make a box across an alignment. By redefining that slightly, you can get the desired effect:

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsfonts}

\makeatletter
\def\@Aboxed#1&#2\ENDDNE{%
  \settowidth\@tempdima{$\displaystyle#1{}$}%
  \addtolength\@tempdima{\fboxsep}%
  \addtolength\@tempdima{\fboxrule}%
  \global\@tempdima=\@tempdima
  \kern\@tempdima
  &
  \kern-\@tempdima
  \fcolorbox{red}{yellow}{$\displaystyle #1#2$}
}
\makeatother

\begin{document}
\begin{align*}
              \Aboxed{a &= b}  &        c  &= d \\
                 c  &    = d   & \Aboxed{i &= k} \\
                 e  &    = f   &        g  &= h  \\
\end{align*}
\end{document}

I basically copied the definition of Aboxed from mathtools.dtx and changed the last line of the definition from \boxed to \fcolorbox{....


(I wasn't sure if the following should be added as a new answer or as an edit. Please advise if I should make it a new answer.)

When looking at some lecture notes I wrote a while ago, I found that I had a command for this purpose in my preamble, and it came to me that I had copied it from LaTeXcommunity.org, but never used it. So here is another solution, based on this post at LaTeX Community by daleif:

\documentclass{article}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{xcolor}

\newlength\dlf
\newcommand\alignedbox[2]{
  % #1 = before alignment
  % #2 = after alignment
  &
  \begingroup
  \settowidth\dlf{$\displaystyle #1$}
  \addtolength\dlf{\fboxsep+\fboxrule}
  \hspace{-\dlf}
  \fcolorbox{red}{yellow}{$\displaystyle #1 #2$}
  \endgroup
}

\begin{document}
\begin{align*}
              \alignedbox{a}{=b}  &        c  &= d \\
                 c  &    = d   & \alignedbox{i}{=k} \\
                 e  &    = f   &        g  &= h  
\end{align*}
\end{document}

This yields:

enter image description here

This does not have the problem of overwriting an existing command, but requires the calc package.

share|improve this answer
    
Nice solution! I added $s around #1#2, now it seems to work. –  Hendrik Vogt Mar 17 '11 at 9:03
    
@Hendrik Vogt Thanks, indeed it seems to work now. I added a \displaystyle as well. –  Torbjørn T. Mar 17 '11 at 19:19
    
@Peter Grill: I added a second solution. –  Torbjørn T. Jun 10 '11 at 20:30
    
Follow up to this answer: Highlight an equation within an align environment with color option. –  Peter Grill Sep 16 '12 at 5:26
add comment

This is rather an ugly hack: I used tikz package to create a node around the left and right side of the equation in align, using remember picture option to remember the size and location of the nodes. Then I drew the box with another tikz picture with the overlay option.

The problem with that is tikz will draw the box over the text in the equation, so I had to use the remembered position of the nodes to recreate the nodes again. There must be a better way to do that.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzstyle{nd} = [anchor=base, inner sep=0pt]
\tikzstyle{ndpic} = [remember picture, baseline, every node/.style={nd}]

\begin{document}
\begin{align*}
   \tikz[ndpic]{\node(left) {$a$};} &= \tikz[ndpic]{\node (right) {$b$};} \\
                 c  &              = d \\
                 e  &              = f  \\
         \tikz[ndpic]{\node(left1) {$\displaystyle \lim_{x\to 0}\frac{\sin x}{x}$};} &= \tikz[ndpic]{\node (right1)
         {$\displaystyle \int_{-\pi}^\pi \frac{\sqrt{x^2 - 1}}{2x}\;dx$};} \\
\end{align*}

\begin{tikzpicture}[overlay, remember picture]
   \draw[very thick, red, fill=yellow] ($ (left.south west)+(-.1,-.1) $)
   rectangle ($ (right.north east)+(.1,.1) $);
   \node[nd] at (left.base) {$a$};
   \node[nd] at (right.base) {$b$};
   \node[nd] at ($ (right.base)!.5!(left.base) $) {$=$};
\end{tikzpicture}

\begin{tikzpicture}[overlay, remember picture]
   \draw[very thick, red, fill=yellow] ($ (left1.south west)+(-.1,-.2) $)
   rectangle ($ (right1.north east)+(.1,.1) $);
   \node[nd] at (left1.base) {$\displaystyle \lim_{x\to 0}\frac{\sin x}{x}$};
   \node[nd] at (right1.base) {$\displaystyle \int_{-\pi}^\pi \frac{\sqrt{x^2 - 1}}{2x}\;dx$};
   \node[nd] at ($ (right1.base west)!.5!(left1.base east) $) {$=$};
\end{tikzpicture}
\end{document}

result of the above code

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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