テキストにソリューションを挿入するために、次の環境を定義しました。
\newif\ifsolution
\def\solution#1{\ifsolution {\color{blue}\textbf{Oplossing:} #1} \else \relax\fi}
\solutiontrue
ファイルに追加すると、ソリューションが青色で追加されますが、 を追加すると\solutionfalse
、ソリューションが完全に省略されます。ここまでは順調です。
しかし、私が抱えている問題は、ソリューション自体では、本当に必要な空白行を挿入できないことです。たとえば、
\solution{This is the first line of the solution.
This is the second line. QED}
エラーが表示されます。代わりに次のように記述する必要があることは理解しています。
\solution{This is the first line of the solution.
This is the second line. QED}
空白行は見逃せないので、エラーを回避する方法はあるでしょうか?
答え1
これが本当にあなたが望んでいたことかどうかはよくわかりません。しかし、マクロ入力で空白行を許可し、ソリューションを省略しながら、そのスペースを出力でワイルドカードとして保持する方法は次のとおりです。
\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}
と\solutiontrue
と\solutionfalse
マクロ入力でデフォルトで空白行が機能しない理由は、TeXの基本にあります。設計上、Knuthはマクロパラメータのそのような動作を許可しませんでした。\long
これを克服できます(一部の人はこれを制限と呼ぶでしょう)。この質問このトピックに関する良い議論が見つかります。
答え2
私はあなたの質問を別の意味で意図していましたが、 を使ったこの解決策はどうでしょうか\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}