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 Answers2

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}

Peter Grill
  • 223,288
  • \textcolor, not \color. – egreg Sep 04 '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 04 '18 at 22:36
  • 1
    @AramPapazian Because \textcolor is right and \color is wrong here. – egreg Sep 04 '18 at 22:41
  • @egreg: Corrected to use \MathColor. – Peter Grill Sep 05 '18 at 08:59
  • @AramPapazian: See the link in the References as to why not to use \color (or even \textcolor) in math mode. – Peter Grill Sep 05 '18 at 08:59
  • @egreg ... That's not what I asked, but it's ok. I'll go look it up. – Aram Papazian Sep 05 '18 at 12:04
  • @PeterGrill Ok thanks for that =D I'll look into why not to use color using the link. I think I'm still preferring my second method because even though Phantom does do the job, I want there to actually be a bigger space for the students to be able to write in. Handwriting is bigger than typing ;) So I really need to be able to specify the heigh I want. – Aram Papazian Sep 05 '18 at 12:06
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