Highlight or underline answer in Latex when code is in begin/end{align*}

I normally write my solutions to mathematics problems in Latex using the begin/end{align*} feature.

How does one highlight an equation, or answer in my case, when the code is in begin/end{align*} ?

See my code below:

\begin{align*}
%--
i^7 &= 1 \times i^3 \\[5mm]
%--
i^7 &=  i^3 \\[5mm]
%--
i^7 &= i^2 \times i \\[5mm]
%--
i^7 &= -1 \times i \\[5mm]
%--
i^7 &=  -i \\[5mm]
%--
i^7 &=  -i
\end{align*}


So I would like to highlight the last line BUT maintain the alignment

Thanks

• Do you mean underline as the highlight as said in the title? (Well, it is easy to change its color just using \color{} within a {}.) Commented Apr 25, 2022 at 4:32
• Will \color{} maintain the alignment so that all the equal signs are under each other? Commented Apr 25, 2022 at 4:45
• Here the last line you can use \color{red} i^7 & \color{red}=-i and it looks right. (No {} needed in align environment.) Commented Apr 25, 2022 at 4:48
• I tried your code and it gives me red text. I just want to highlight the the answer i^7 = -i. Commented Apr 25, 2022 at 4:56
• SOmething like this works fine for me. \documentclass{article} \usepackage{xcolor} \usepackage{mathtools} % or \usepackage{amsmath} \begin{document} \begin{align*} %-- i^7 &= 1 \times i^3 \\[5mm] %-- i^7 &= i^3 \\[5mm] %-- i^7 &= i^2 \times i \\[5mm] %-- i^7 &= -1 \times i \\[5mm] %-- i^7 &= -i \\[5mm] %-- \color{red} i^7 & \color{red} =-i \end{align*} \end{document}  Result: image Commented Apr 25, 2022 at 4:57

1 Answer

This problem can be concluded as colorbox inside align, so we can use

\documentclass{article}
\usepackage{xcolor}
\usepackage{mathtools}

% Reference: https://tex.stackexchange.com/a/13693/234654
\newlength\dlf
\newcommand\alignedbox[2]{
% #1 = before alignment
% #2 = after alignment
&
\begingroup
\settowidth\dlf{$\displaystyle #1$}
\addtolength\dlf{\fboxsep+\fboxrule}
\hspace{-\dlf}
\fcolorbox{yellow}{yellow}{$\displaystyle #1 #2$}
\endgroup
}

\begin{document}
\begin{align*}
%--
i^7 &= 1 \times i^3 \\[5mm]
%--
i^7 &=  i^3 \\[5mm]
%--
i^7 &= i^2 \times i \\[5mm]
%--
i^7 &= -1 \times i \\[5mm]
%--
i^7 &=  -i \\[5mm]
%--
\alignedbox{i^7}{=-i}
\end{align*}
\end{document}


This genius idea is borrowed from here.

This method calculates the length before the aligned mark & into variable \dlf. Then \hspace is used to left shift the colorbox. Now the alignment is achieved.