2

I have defined the following environment to insert a solution in my text:

\newif\ifsolution
\def\solution#1{\ifsolution {\color{blue}\textbf{Oplossing:} #1} \else \relax\fi} 

If I add \solutiontrue to the file, it adds the solutions in blue, but if I put \solutionfalse, it omits them altogether. So far, so good.

The problem I have though is that in the solution itself, I can't seem to be able to insert blank lines, which I really need. For instance, if I put

\solution{This is the first line of the solution.

This is the second line. QED}

I receive an error. I understand that instead I have to write

\solution{This is the first line of the solution.
This is the second line. QED}

Now given that the blank lines are unmissable, is there a way to avoid the errors?

  • 1
    You may consider to typeset the answer in white, instead of omiting it. This will make it invisible (although it could be selected from a pdf and pasted into an editor to make it visible). Or, if you are not asking about how to "leave the blank", but about how to allow the use of newlines inside the argument, then you have to use \long\def instead of \def. – JLDiaz Jan 21 '17 at 20:41
  • I think this does the trick. I really don't need to insert blank space, my goal was more to create a 'teacher's copy' for my teaching assistants, in such a way that I can print a copy with solutions for them and a solution free one for the students. – Werner Jan 21 '17 at 21:16
2

I'm not quite sure if this is really what you wanted. But here is a way to allow blank lines in your macro input and omit the solution while keeping it's space as a wildcard in the output:

\documentclass{article}
\usepackage{xcolor}

\newif\ifsolution
\long\def\solution#1{%
  \setbox0\vbox{#1}
  \vbox to \ht0 {
    \ifsolution {\color{blue}\textbf{Oplossing:} #1} \else \relax\fi}}
\solutiontrue
%\solutionfalse

\setlength\parindent{0em}

\begin{document}
(before solution)

\solution{%
  This is the first line of the solution.

  This is the second line. QED
}

(after solution)
\end{document}

with \solutiontrue

out1

with \solutionfalse

out2


The reason for the blank lines not working in macro input by default lies in the TeX fundamentals. By design Knuth did not allow that behaviour of macro parameters. Only by adding the \long primitive you can overcome this (what some people would call a limitation). In this question you find a good discussion of this topic.

0

I intended your question in a different way, what about this solution with \makecell?

\documentclass{article}
\usepackage{color}
\usepackage{makecell}
\newif\ifsolution
\def\solution#1{\ifsolution {\color{blue}\textbf{Oplossing:} #1} \else \relax\fi}
\solutiontrue  
\begin{document}
 \solution{\makecell[l]{This is the first line of the solution.\vspace{3ex}\\   
    This is the second line. QED}}
\end{document}

enter image description here

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.