(オプションで) 非表示になっているコンテンツのカウンターを増やし続けるにはどうすればよいでしょうか?

(オプションで) 非表示になっているコンテンツのカウンターを増やし続けるにはどうすればよいでしょうか?

私は演習を作成するためのフレームワークを持っています。このフレームワークでは、各サブ問題の解決策の内容が -macro に入力され\solution、グローバル ブール値が true に設定されている場合にのみその内容が表示されます。

私が関心を持っているのは、解答ありと解答なしの両方のバージョンで、方程式/図/表/リストの番号が一意であることです。特に、解答でカウンターが増加するかどうかを考慮する必要があります。これにより、学生が解答で参照する可能性のある方程式の番号 (およびその他のもの) が、後で解答が公開されても番号が変更されないことが意図されています。

私が最初に試みたのは、印刷されないボックス (たとえば、 by\sboxまたはlrbox-environment) にすべてをタイプセットして、実際にカウンターを増やすことだったのですが、すでにequation環境で問題が発生しています。

以下は MWE です。ここでは、オイラーの公式に(3)、トグルの両方の設定に対する方程式番号が必要ですwith_solution。解のないバージョンを機能させるには、\iftoggleinの 3 番目の引数 (の内容)\solutionをコメント アウトする必要があります。

編集:この問題を回避する 1 つの解決策は、関連するすべてのカウンターをサブ問題に従属させることです。そうすれば、定式化での発生が最初であり、したがって必然的に一意になります (定式化は、ソリューションの有無にかかわらず、両方のバージョンに表示されるため)。ただし、これは私にとって興味深い解決策ではありません。なぜなら、実際には (短縮された MWE と比較して)、(5.3.2)演習 5 の問題 3 の式 2 の方程式がすでに のようになっているのに対し、サブ問題は "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}}

関連情報