1

I am creating a handout with a command that hides the answers for students and reveals the answers for instructors. I defined a new command invisible that takes a bool and text. If 0, the text should be untouched. If 1, the text should be printed in white, making it invisible on paper. However in this example the text is printed regardless of the bool. I created another command altinvisible where the text is not a parameter. It did not solve the problem. What is wrong here?

\documentclass{article}
\usepackage{tikz}
\newcommand{\invisible}[2]{\ifcase#1\or\expandafter\textcolor{white}\fi{#2}}
\newcommand{\altinvisible}[1]{\ifcase#1\or\expandafter\textcolor{white}\fi}

\title{} \author{} \begin{document}

{3.14}

\textcolor{white}{3.14}

\invisible{0}{3.14}

\invisible{1}{3.14}

\altinvisible{0}{3.14}

\altinvisible{1}{3.14}

\end{document}

om94266
  • 11

1 Answers1

1

If you don't fully understand how TeX's macros and primitives (such as \expandafter in this case) work, I recommend writing easy-to-understand macros that does not use them (see the modified \invisible macro as in my answer) (possibly at the cost of slightly slower compilation).

If you want to learn, there are a few ways. See \altinvisible and \altbinvisible macros below.

Learning material:

tl;dr in this case, the bug is that you can clearly see the \expandafter cannot be used to execute the \fi early if there's more than one token between the \expandafter and the \fi.

\documentclass{article}
\usepackage{tikz}
\newcommand{\textcolorwhite}{\textcolor{white}}
\newcommand{\invisible}[2]{\ifcase#1 #2\or\textcolor{white}{#2}\fi}
\newcommand{\altinvisible}[1]{\ifcase#1\or\expandafter\textcolorwhite\fi}
\makeatletter
\newcommand{\altbinvisible}[1]{\ifcase#1\or\expandafter\@gobble\fi}
\makeatother

\title{} \author{} \begin{document}

visible: {3.14}

invisible: \textcolor{white}{3.14}

visible: \invisible{0}{3.14}

invisible: \invisible{1}{3.14}

visible: \altinvisible{0}{3.14}

visible: \altbinvisible{0}{3.14}

invisible: \altinvisible{1}{3.14}

invisible: \altbinvisible{1}{3.14}

\end{document}

user202729
  • 7,143