提示在另一章中使用

提示在另一章中使用

考虑以下 MWE:

\documentclass[12pt]{book}
\usepackage{exsheets}
\SetupExSheets[solution]{print=true}

\begin{document}
    \begin{question}
         This is a question
        
    \end{question}

    \begin{solution}
    This is an answer       
    \end{solution}  

\end{document}

我试图在解决方案环境之外,为每个问题提供一个提示,以便根据章节提示中的“章节号.问题号”打印这些提示。如何定义这样的命令或环境?考虑以下 MWE:

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\usepackage{environ}

\makeatletter
\NewEnviron{hint}{%
    \def\@currentlabel{\BODY}\label{hint:\thequestion}%
    
    
    
}

\usepackage{pgffor}

\newcommand\printmyhints{%
    
    
    \foreach \x in {1,...,\thequestion}{% 
        
        \noindent
        \thechapter.\x.~\ref{hint:\x}\par
    }
}

\makeatother
\begin{document}
    
    \chapter{Analysis of Algorithms}
    
    \begin{question}
        Which of the following answers is correct for the given recurrence relation?
        
    \end{question}
    \begin{hint}
        a hint is here!
    \end{hint}
    \begin{solution}
        this is a solution
    \end{solution}
    \chapter{answer Sheet}

        \printmyhints
    

\end{document}

上述代码的问题是提示必须有计数器 1.1,1.2,但这些从第 2 章开始。

答案1

诀窍是将章节和问题编号与提示一起存储,并对提示(标签)使用单独的计数器。

我还随意\foreach用 TeX替换了它\loop。只需记住循环提示计数器即可。

顺便说一句,这符合辅助文件方法。

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
\usepackage{environ}

\newcounter{hint}

\makeatletter
\NewEnviron{hint}{\refstepcounter{hint}%
    \edef\@currentlabel{\thechapter.\thequestion~\BODY}%
    \label{hint:\thehint}%
}
\newcommand\printmyhints{% you could use \foreach, but this is faster
\bgroup% use local registers
  \count1=\value{hint}% a macro would also work
  \setcounter{hint}{0}%
  \loop\ifnum\value{hint}<\count1
    \stepcounter{hint}%
    \noindent\ref{hint:\thehint}\par
  \repeat
\egroup}

\makeatother
\begin{document}
    
    \chapter{Analysis of Algorithms}
    
    \begin{question}
        Which of the following answers is correct for the given recurrence relation?
        
    \end{question}
    \begin{hint}
        a hint is here!
    \end{hint}
    \begin{solution}
        this is a solution
    \end{solution}
    \chapter{answer Sheet}

        \printmyhints
    

\end{document}

这显示了一个保存框方法。为了允许多行文本,它使用\vbox而不是\hbox样式保存框。 udbox\vbox的一个版本lrbox

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
\usepackage{environ}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\SetupExSheets{counter-format=ch.qu[1]}
\RenewQuSolPair{question}[headings-format=\large\bfseries,name=Problem]{solution}
%\usepackage{showframe}

\makeatletter
\def\udbox#1{% fragile
  \edef\reserved@a{%
  \endgroup
  \setbox#1\vbox{%
  \begingroup\aftergroup}%
  \def\noexpand\@currenvir{\@currenvir}%
  \def\noexpand\@currenvline{\on@line}}%
  \reserved@a
  \@endpefalse
  \color@setgroup
  \ignorespaces}
\def\endudbox{\unskip\color@endgroup}
\makeatother

\newsavebox{\hintbox}

\newenvironment{hint}{\begin{udbox}{\hintbox}%
  \unvbox\hintbox
  \noindent\thechapter.\thequestion\space}%
{\end{udbox}%
  \global\setbox\hintbox=\copy\hintbox}

\newcommand\printmyhints{\usebox\hintbox}


\begin{document}
    
    \chapter{Analysis of Algorithms}
    
    \begin{question}
        Which of the following answers is correct for the given recurrence relation?
        
    \end{question}
    \begin{hint}
      Consider the following:
      %\hrule
      \begin{itemize}
        \item test
      \end{itemize}
    \end{hint}
    \begin{solution}
        this is a solution
    \end{solution}
    \chapter{answer Sheet}

        \printmyhints

\end{document}

相关内容