
我有一個用於創建練習的框架,其中每個子問題的解決方案的內容都被輸入到\solution
- 宏中,該巨集僅在全域布林值設為 true 時才顯示其內容。
我感興趣的是具有和不具有解決方案的版本都有獨特的方程式/圖形/表格/列表編號。特別是,我需要考慮解決方案中是否增加了計數器。這樣做的預期好處是,一旦解決方案稍後發布,學生在解決方案中可能引用的方程式編號(和其他內容)就不會改變其編號。
我的第一次嘗試是嘗試將所有內容排版到一個未列印的盒子中(例如,由環境\sbox
列印lrbox
) - 希望這實際上會增加計數器 - 但我已經遇到了equation
環境問題。
下面是一個 MWE,其中歐拉公式應該具有(3)
用於切換的兩種設定的方程式編號with_solution
。為了使無解決方案版本正常運作,必須註解掉\iftoggle
in的第三個參數(的內容) 。\solution
編輯:規避該問題的一個解決方案是將所有相關計數器從屬於子問題,因為這樣公式中的出現是第一次,因此必然是唯一的(因為公式出現在兩個版本中 - 有和沒有解決方案)。然而,這對我來說不是一個有趣的解決方案,因為實際上(相對於簡化的 MWE),我的方程式已經看起來像(5.3.2)
eq.問題2練習 5 的 3,而子問題看起來像“5.3a)”、“5.3b)”等(使用一致的符號 - 即(5.3a.2)
- 對我來說沒有吸引力)。將計數器從屬於problem
沒有幫助,因為在每個子問題的不同公式之間,解決方案能夠增加不同的計數器,而這些計數器不會透過呼叫 重置\subproblem
。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage{xparse}
\newtoggle{with_solution}
\newsavebox\tempbox
\NewDocumentCommand{\solution}{+m}{
\iftoggle{with_solution}{
\par\medskip\noindent\textbf{Solution:} #1
}
{
% \sbox\tempbox{#1}
\begin{lrbox}\tempbox
#1
\end{lrbox}
}
}
% \problem and \subproblem cut to bare bones for simplicity
\newcommand{\problem}[1]{\section{#1}}
\newcounter{subproblem}
\newcommand{\subproblem}{\par\medskip\noindent\stepcounter{subproblem}{\bfseries\alph{subproblem})}\quad}
\toggletrue{with_solution} % Set to true to include solution
%\togglefalse{with_solution} % Set to false to exclude solution
\begin{document}
\problem{A Problem}
\subproblem Problem Formulation
\solution{Solution}
\subproblem Problem Formulation with \eqref{eq:prb}
\begin{equation}\label{eq:prb}
a=b
\end{equation}
\solution{Solution with \eqref{eq:sol}
\begin{equation}\label{eq:sol}
x=y
\end{equation}
}
\subproblem Problem Formulation with \eqref{eq:prb2}, having the same number both with or without typesetting the solutions.
\begin{equation}\label{eq:prb2}
\mathrm{e}^{2\pi\mathrm{i}}=1
\end{equation}
\solution{The \texttt{$\backslash$solution}-macro should be able to handle \texttt{$\backslash$par}'s, figures, tables, listings (and also increase their counters accordingly, even when not typeset)...}
\end{document}
答案1
另一條路要走
\NewDocumentCommand{\solution}{+m}{
\iftoggle{with_solution}
{\par\medskip\noindent\textbf{Solution:} #1\par}
%{\setbox\tempbox=\vbox{#1}}} % this line comes from egreg's comment
% The following line is my alternative
{\par\vbox to0cm{\vspace*{\paperheight}\medskip\noindent\textbf{Solution:} #1\vspace*{-\paperheight}}\par}}
請注意,類似的命令\addtocontents
是在輸出例程期間執行的。因此,在我的回答中,\listoffigure
包含所有數字,無論它們是否顯示。 (事實上,它們被顯示......)雖然egreg的答案產生\listofshownfigure
。
對於間距
處理負空間並不有趣。我想把它放在一邊。實驗表明,其\par\marginpar{}\par
行為如下\par
。
\NewDocumentCommand{\solution}{+m}{
\iftoggle{with_solution}
{\par\medskip\noindent\textbf{Solution:} #1\par}
%{\setbox\tempbox=\vbox{#1}}} % this line comes from egreg's comment
% The following line is my alternative
{\par\marginpar{\moveright\paperwidth\vbox to0cm{\medskip\noindent\textbf{Solution:} #1}}\par}}