0

I am writing an exam in LaTeX, and I would like to include the solutions in the text. I'd like there to be one line at the top that I can comment out to remove all solutions from the exam.

In the with-solutions version, the solution text should always appear in red. In the without-solutions version, the solution needs to be replaced with something (a blank line, empty vspace, etc.).

This question got me to something that I think is close: Conditional typesetting / build

This is almost what I need:

\usepackage{xcolor}
\newif\ifsolution
\newcommand\answer{\ifsolution\color{red}}
\let\endanswer\fi

This allows me to put the solutions in \begin{answer}...\end{answer} and easily remove the solutions by commenting out \solutiontrue, but I can't figure out how to replace the solution text with something else.

This doesn't work, but hopefully gives an idea of what I want. When \solutionfalse, I want to replace the solution text with 2cm of blank vspace:

\usepackage{xcolor}
\newif\ifsolution
\newcommand\answer{\ifsolution\color{red}}
\let\endanswer{\else\vspace{2cm}\fi}
2
  • 1
    are you distributing on paper (in which case just change red to white) or digitally in which case you need to make the answer really not there not just white on white Commented Jan 13 at 16:20
  • I'm distributing digitally, so I need the answers to really not be there. Commented Jan 13 at 16:22

1 Answer 1

0

I suggest to add an \else clause in the \ifsolution statement for the 2cm \vskip. Does this meet your requirements?

\documentclass{article}
\usepackage{xcolor}


\begin{document}
    \newif\ifsolution
    \newcommand{\answer}[1]{\ifsolution{\color{red}#1}\else{\vspace{2cm}}\fi}
    %
    \solutiontrue
    \answer{visible answer}
    
    text before invisible answer
    
    \solutionfalse
    \answer{invisible}
    
    text after invisible answer
\end{document}

Result looks like this:

output of the LaTeX compiled code snippet


Edit:

Since @sgware asked to

Define a custom environment [...]

in the question title, I am trying to provide a solution for environments in the same manner as the previously accepted answer.

Therefore, I suggest to use the package environ, since it provides access to the environment's \BODY, meaning the text inside the environment if it is defined with the \NewEnviron command.

\documentclass{article}
\usepackage{xcolor}
\usepackage{environ}

\begin{document}
    \newif\ifsolution
    
    \NewEnviron{answer}{\ifsolution{\color{red}\BODY}\else{\vspace{2cm}}\fi}{}
    %
    \solutiontrue
    \begin{answer}
        visible answer
    \end{answer}
    
    text before invisible answer
    
    \solutionfalse
    \begin{answer}
        invisible answer
    \end{answer}
    
    text after invisible answer
\end{document}

The resulting output for this code snippet is identical to the upper one.


Finally, I encourage you to provide a complete MWE if you want to clarify your question or for further questions.

2
  • Yep, this is perfect, thanks! Commented Jan 13 at 16:47
  • @sgware You are most welcome! Commented Jan 13 at 16:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.