1

I'm trying to prepare notes for a class and am wanting to use an align environment in multiple areas but with a potential of two possibilities. Here's a mwe

\documentclass{amsart}
\usepackage{xcolor}

\newcommand\usehidden{1}
\ifnum \usehidden=1
    \newcommand{\hidden}[2]{\vspace*{#1}}
\else
    \newcommand{\hidden}[2]{{\color{red}{#2}}}
\fi 

\begin{document}
    \begin{align*}
        f(x) &= (x+1)^2\\
            &\hidden{7cm}{= (x+1)(x+1)\\
            &= x^2 + x + x + 1}\\
        &= x^2 + 2x + 1
    \end{align*}
\end{document}

Basically the idea is that when I print the student's copy, usehidden is set to 1 and a vertical space is inserted for the students to fill in by hand. On my copy usehidden is set to 0 and the information is actually printed, but in red so I know that was the part that was excluded for the students.

So this works when I set \usehidden{1} but when \usehidden{0} I run into problems because of the color environment. Is there anyway to add color to multiple lines within an align environment?

Also, I noticed vspace isn't giving the appropriate size. Is there a way to make it give the right size?

The thing is, I want to use this style of format quite a few times. (I think I'm at around 50?) So anything that is generalizable and not only for this specific example would be nice.

2

I would recommend you use \phantom{} instead of guessing at what amount of space to leave:

enter image description here

Notes:

  • This does require that you use \hidden on each line that needs to be be hidden.

References:

Code:

\documentclass{amsart}
\usepackage{xcolor}

%% https://tex.stackexchange.com/questions/85033/colored-symbols-in-latex
\newcommand*{\MathColor}{}
\def\MathColor#1#{\mathcoloraux{#1}}
\newcommand*{\mathcoloraux}[3]{%
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3%
  \endgroup
}

\providecommand\usehidden{0}
\ifnum \usehidden=1\relax
    \newcommand{\hidden}[1]{\phantom{#1}}
\else
    \newcommand{\hidden}[1]{{\MathColor{red}{#1}}}
\fi 

\begin{document}
\begin{align*}
    f(x) &= (x+1)^2 \\
         & \hidden{= (x+1)(x+1)} \\
         & \hidden{= x^2 + x + x + 1} \\
    &= x^2 + 2x + 1
\end{align*}
\end{document}
| improve this answer | |
  • \textcolor, not \color. – egreg Sep 4 '18 at 21:33
  • Theoretically I'm aiming for a solution that allows me to group multiple lines together into one big bundle, but this might be a good alternative if I can't find a bundle version. @egreg why textcolor and not color? – Aram Papazian Sep 4 '18 at 22:36
  • 1
    @AramPapazian Because \textcolor is right and \color is wrong here. – egreg Sep 4 '18 at 22:41
  • @egreg: Corrected to use \MathColor. – Peter Grill Sep 5 '18 at 8:59
  • @AramPapazian: See the link in the References as to why not to use \color (or even \textcolor) in math mode. – Peter Grill Sep 5 '18 at 8:59
0

If possible, it'd be nice to still get a solution using array, but until then I found the following workaround using array:

\documentclass{amsart}
\usepackage{xcolor}
\usepackage{array}


\newcommand\usehidden{0}

\ifnum \usehidden=1
    \newcommand{\hidden}[2]{&\vspace*{#1}\\}
\else
    \newcommand{\hidden}[2]{%
        \noalign{\gdef\mycolor{\color{red}}} %
        #2
        \noalign{\gdef\mycolor{}} 
    }
\fi 

\begin{document}
\def\mycolor{}
\newcolumntype{R}{>{\mycolor}r}
\newcolumntype{C}{>{\mycolor}c}
\newcolumntype{L}{>{\mycolor}l}

\[  
    \begin{array}{RL}
        f(x) &= (x+1)^2\\
        \hidden{7cm}{&= (x+1)(x+1)\\
            &= x^2 + x + x + 1\\}
        &= x^2 + 2x + 1
    \end{array}
\]
\end{document}

The idea was taken from: Color a line in array

| improve this answer | |
  • FYI, I was not able to get this to work as I thin it should. – Peter Grill Sep 4 '18 at 19:33
  • Thanks for the catch. I had forgotton to update the vspace parameter. Should be working as expected now. – Aram Papazian Sep 5 '18 at 12:02

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.