# color change over multiple lines of aligned equation

I want to be able to generate my document in two different levels of detail.

1. (When draft is not set): normal version
2. (When draft option is used): more detailed version. Some calculations include extra steps to make it more easy to follow. The additional lines should be colored differently (gray).

I came up with my own explain command for this. Its argument should only be shown when draft is set and should be set with gray color.

However in the MEW below this is not working.

\documentclass[
% draft,
]{article}

\usepackage{amsmath}
\usepackage{ifdraft}
\usepackage{xcolor}

\newcommand{\explain}[1]{%
\ifdraft{%
\textcolor{gray}{%
#1%
}\ignorespaces%
}{}%
}

\begin{document}
\begin{aligned} {(a + b)}^2 \explain{% &= (a + b)(a + b)\\ &= a^2 + ab + ba + b^2\\ } &= a^2 + 2ab + b^2 \end{aligned}
\end{document}


The example fails to compile when draft is used. It seems my use of \textcolor is not valid since it spans over the alignment character &.

How could I change my new command to make this work?

Ty, Franz

• If you're latex is fully up to date, then it is not \textcolor it then &'s, they are now hidden so deep that aligned cannot see them. And just using \color{gray} does not work either as it never penetrates into the next cells and thus does nothing after the first &. Sep 20 at 13:04
• Perhaps you should explain in better detail what exactly the end goal is with this. Sep 20 at 13:04
• I want to be able to generate the document in two different levels of detail. One with just the first line ({(a + b)}^2) and the last line (&= a^2 + 2ab + b^2) and the other one with the additional steps in between. The additional steps should be colored gray if they are present. I want to use the draft option to toggle between the versions. Sep 20 at 13:14
• Explain it in your question. Sep 20 at 13:17
• ok. it should be more clear now. Sep 20 at 17:54

The problem is that you cannot format multiple cells (anything delimited by & or \\ inside the aligned environment) at once. You can, however, style the contents of every cell individually. So, the following would be possible:

\documentclass[
draft,
]{article}

\usepackage{amsmath}
\usepackage{ifdraft}
\usepackage{xcolor}

\newcommand{\explain}[1]{%
\ifdraft{#1}{}%
}
\newcommand{\explaincolor}{%
\ifdraft{\color{gray}}{}%
}

\begin{document}
\begin{aligned} {(a + b)}^2 \explain{ & \explaincolor = (a + b)(a + b) \\ & \explaincolor = a^2 + ab + ba + b^2 \\ } & = a^2 + 2ab + b^2 \end{aligned}
\end{document}


With the draft option disabled, you would get:

With the above in mind, you could try to automatically insert the relevant color after every & and after every \\ (except if nothing follows at the end of the argument) that appears inside the argument of \explain (additionally once at the very beginning of the argument to cover the first cell). I am unsure, however, whether this won't break something in certain circumstances:

\documentclass[
draft,
]{article}

\usepackage{amsmath}
\usepackage{ifdraft}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentCommand { \insertexplaincolor } { m } {
\tl_set:Nn \l__myexplcol_explcolbody_tl { \explaincolor #1 }
% add \color macro after every occurance of &
\regex_replace_all:nnN { \& } { \& \c{explaincolor} } \l__myexplcol_explcolbody_tl
% add \color macro after every \\
\regex_replace_all:nnN { \c{\\} } { \c{\\} \c{explaincolor} } \l__myexplcol_explcolbody_tl
% remove \color macro after \\ if occuring at the end of the string (ignoring whitespace)
\regex_replace_all:nnN { \c{\\} \c{explaincolor} \s* \z } { \c{\\} } \l__myexplcol_explcolbody_tl
\tl_use:N \l__myexplcol_explcolbody_tl
}
\ExplSyntaxOff

\newcommand{\explaincolor}{\color{gray}}

\newcommand{\explain}[1]{%
\ifdraft{\insertexplaincolor{#1}}{}%
}

\begin{document}
\begin{aligned} {(a + b)}^2 \explain{ & = (a + b)(a + b) \\ & = a^2 + ab + ba + b^2 \\ } & = a^2 + 2ab + b^2 \end{aligned}

\end{document}

• well ... that's annoying. Especially if I move lines into or out of the additional explanations. I admit it is a solution though. Sep 20 at 19:17
• @Franz I added another solution that automatically inserts the color macro. But handle this approach with care. It might break in certain circumstances. Sep 20 at 19:31
• haha ok I don't even remotely understand how this works ... but it does. I will use it and see how far it gets me in other situations where I want to add stuff. I thought NewDocumentCommand need the xparse package but even that seems to be wrong. Could explaincolor not be removed and color{gray} be inserted directly? Sep 20 at 20:36
• @Franz You can replace \c{explaincolor} with \c{color}\{gray\} in the \insertexplaincolor macro. I thought it would be nice to have the color definition somewhere else ... =) Sep 20 at 21:00
• turns out that some use cases for my original definition of explain now do not color the text correctly anymore. I guess that was to be expected. I started a new question for this here (tex.stackexchange.com/questions/658973/…) maybe you want to take a look? Sep 21 at 14:12