隱藏部分的空白行

隱藏部分的空白行

我定義了以下環境以在文字中插入解決方案:

\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

輸出1

\solutionfalse

輸出2


預設情況下,空行在巨集輸入中不起作用的原因在於 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}

在此輸入影像描述

相關內容