# An environment for detailed calculations

In the following MWE

\documentclass{article}
\usepackage{amsmath, xcolor, comment}

% \specialcomment{details}{\begingroup\color{gray}}{\endgroup}  % This does not work with aligned
\includecomment{details}
% \excludecomment{details}   % Uncomment to exclude details.

\begin{document}
This is my calculation:
\begin{aligned} &X = Y\\ \begin{details} &=Y_{1} \\ &\leq Y_{2} \end{details} &\le Z \end{aligned}
\begin{details}
These are even more details: $A = B$
\end{details}
\end{document}


I define a details environment which I use to enclose detailed calculations that I can selectively hide (by uncommenting the line with excludecomment). The previous environment works great even with the aligned environment by using the comment package. However, I would like to modify the environment so that the details are colored in gray. The most obvious modification (using specialcomment) does not work with aligned. I assume this is because the cells in an aligned environment cannot be broken with groups.

Is there a way to define such an environment while still working with aligned?

EDIT: Any solution would be most welcome, even if it does not involve comment.

If you define the special comment details like that, then it works:

\specialcomment{details}{%
\global\everymath{\color{gray}}%
\global\everydisplay{\color{gray}}%
\color{gray}%
}{%
\global\everymath{\normalcolor}%
\global\everydisplay{\normalcolor}%
\normalcolor
}


The problem is that any definitions within an aligned cell are local, unless they are made global. Opening a group in one cell and closing it in another does not work.

Details included:

Details excluded:

\documentclass{article}
\usepackage{amsmath, xcolor, comment}
\specialcomment{details}{%
\global\everymath{\color{gray}}%
\global\everydisplay{\color{gray}}%
\color{gray}%
}{%
\global\everymath{\normalcolor}%
\global\everydisplay{\normalcolor}%
\normalcolor
}
%\excludecomment{details}   % Uncomment to exclude details.
\begin{document}
This is my calculation:
\begin{aligned} &X = Y\\ \begin{details} &=Y_{1} \\ &\leq Y_{2} \end{details} &\le Z \end{aligned}
\begin{details}
These are even more details: $A = B$
\end{details}
\end{document}

• Brilliant! Although this seems to break the environment align for some reason. I know this wasn't part of the original questions, but I am wondering if there's an easy fix. Nov 5, 2020 at 13:35
• I should add that the solution breaks the align environment when details are not excluded and after first use of details. Even in align environments that do not use details. Nov 5, 2020 at 13:56
• I don't think that there is a good solution to your problem the way you try it. My solution works only in some cases. In case you try to do a presentation with overlays and partial uncovering: For this, there is the beamer class, which does the things you apparently want. Nov 6, 2020 at 18:34
• And keep the bounty (in case you feel obliged to give it away), my solution is not that great. Nov 6, 2020 at 18:36
• I think it's a good solution. I just need to modify so that \color{gray} is added to everymath and everydisplay instead of replacing the previous value and then the previous value should be restored at the end. Thanks for the help @gernot. Nov 9, 2020 at 13:28

Based on the solution by @gernot, with the fix I mentioned in the comments, which does not break align after first use of details,

\documentclass{article}
\usepackage{amsmath, xcolor, comment}

\makeatletter
\specialcomment{details}{%
\xdef\@oldeverymath{\the\everymath}%
\xdef\@oldeverydisplay{\the\everydisplay}%
\global\everymath\expandafter{\@oldeverymath\color{gray}}%
\global\everydisplay\expandafter{\@oldeverydisplay\color{gray}}%
\color{gray}%
}{%
\global\everymath\expandafter{\@oldeverymath}%
\global\everydisplay\expandafter{\@oldeverydisplay}%
\normalcolor%
}
\makeatother
%\excludecomment{details}   % Uncomment to exclude details.
\begin{document}

This is my calculation:
\begin{aligned} &X = Y\\ \begin{details} &=Y_{1} \\ &\leq Y_{2} \end{details} &\le Z \end{aligned}
\begin{details}
These are even more details: $A = B$
\end{details}

\begin{align}
A&=B\\
% \begin{details} % Not supported by comment package
%   &=C\\
% \end{details}
&=D
\end{align}
\end{document}


On the other hand, using a comment environment inside align seems unsupported for some reason, so details does not work there for a similar reason.