使用练习包,我们如何使答案组的标题与练习组的标题相匹配?

使用练习包,我们如何使答案组的标题与练习组的标题相匹配?
%--------------------------------------------------------------------------------------------------------- %
\documentclass[english]{book}
%--------------------------------------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage[explicit]{titlesec} %
\usepackage{titletoc} %
\usepackage{fancyhdr} %
\usepackage{fancyref} %
\usepackage{babel} %
\usepackage{tikz} % tikz - essential
\usepackage{newtxtext} %
\usepackage{libertine} %
\usepackage{setspace} %
\usepackage{colortbl} % changed to just colortbl
\usepackage{stackengine} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,lmargin=12.5mm,rmargin=12.5mm,bindingoffset=12.5mm,noheadfoot,nomarginpar,showframe,showcrop]{geometry}
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{stackengine} %
\usepackage{graphicx} %
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{ifpdf} %
\usepackage{exercise} %
\usepackage{ifthen}\newboolean{firstanswerofthechapter} %
\usepackage{hyperref} %

\definecolor{ThemeColor}{rgb}{0.50,0.04,0.04} % ThemeColor DarkRed
\definecolor{DarkBlue}{rgb}{0.00,0.08,0.45} % #000086
\definecolor{DarkOrange}{rgb}{0.55,0.22,0.05} % #FB9902
\definecolor{DarkRed}{rgb}{0.45,0.06,0.06} % #880000
\definecolor{LightOrange}{rgb}{1.00,0.87,0.64} % #FB9902
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85}

\tcbset{myinnerbox/.style={no shadow, shrink tight, arc=1.5pt, extrude by=2.0mm,
colframe=black, boxrule=0.7pt, frame style={opacity=0.25},
interior style={opacity=0.5}, colback=black!5!white}}

%
% ------------------------------------- Exercise preliminaries ------------------------------------------- %
%

\makeatletter
\let \@subQuestionHeader@original=\@subQuestionHeader
\newcommand{\@subQuestionHeader@noItem}{ %
    \hspace{0pt} % between (a) and (b)
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderDifficulty
        \subQuestionNB\hspace{0.4pt} % adjust space from the ) bracket
    \endgroup
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderTitle
    \endgroup % affects subquestion on new line
    \ignorespaces
        }

\newcommand{\subQuestionOnSameLine}{ %
    \let \@subQuestionHeader=\@subQuestionHeader@noItem
    \subQuestion
}
\newcommand{\subQuestionOnNewLine}{ %
    \let \@subQuestionHeader=\@subQuestionHeader@original {} % \\[-9pt]
    \subQuestion
}
\makeatother

\makeatletter
\def\endExerciseEnv{\termineliste{1}\@EndExeBox\vspace{6pt}%
\vskip\ExerciseSkipAfter}

\gdef\endAnswerEnv{\termineliste{1}\@EndExeBox\vspace{8pt}\termineliste{0}\vskip\AnswerSkipAfter}
\makeatother

\renewcommand{\AtBeginExercise}{\vspace{-6pt}}
%
% ----------------------------------------------- Define counters ------------------------------------ %
%
\numberwithin{Exercise}{chapter} % ! not counterwithin
\numberwithin{Answer}{chapter}
\newcounter{problem}[Exercise] %

\renewcommand{\ExerciseName}{Exercise}
\renewcommand{\QuestionNB}{\tcbhighmath[size=fbox,colframe=DarkRed,colback=blue!12!white]{\arabic{Question}}\hspace{3.2pt}}
\renewcommand{\QuestionHeaderTitle}{\emph{(\QuestionTitle)}\ }
\renewcommand{\subQuestionNB}{\alph{subQuestion}\hspace{0.7pt})\hspace{1.3pt}}
\renewcommand{\ExerciseHeaderTitle}{\hspace{2.4pt}-\hspace{1.6pt}-\hspace{1.6pt}-\;\ExerciseTitle}
\renewcommand{\QuestionHeaderDifficulty}{\theQuestionDifficulty\ }


% Setting up the EXERCISES header (conditional)...
\renewcommand{\ExerciseHeader}
{\ifthenelse{\boolean{firstanswerofthechapter}} % line 1
%
{\newpage\noindent\hspace{1.5pt}\vspace*{4pt} % line 2

% AAAAAAAAAAAA
{\libertineSB\fontsize{12}{14}\selectfont{\text{Exercises based on the content of \text{chapter\hspace{2.4pt}\thechapter}}}} % END OF MAJOR TITLE line 5

\vspace{9pt} % BBBBBBBBBBBB
\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 6
{\libertineSB\fontsize{11.5}{15}\selectfont{\textcolor{ThemeColor}{Exercise~\ExerciseHeaderNB}}}\ExerciseHeaderTitle\newline\smallskip}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}} % ELSE line 7
%
    {\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 8
%
        {\libertineSB\fontsize{11.5}{15}\selectfont{\textcolor{ThemeColor} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   
{Exercise~\ExerciseHeaderNB}}}\ExerciseHeaderTitle\newline\newline\smallskip} % line 9
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}}} % line 10



% Setting up the ANSWERS header (conditional)...
\renewcommand{\AnswerHeader}
{\ifthenelse{\boolean{firstanswerofthechapter}} % this is correct
%
{\newpage\noindent\hspace{1.5pt}\vspace*{4pt}
% AAAAAAAAAAAA
{\libertineSB\fontsize{11}{14}\selectfont{Answers\hspace{1.2pt}/\hspace{0.1pt}solutions to exercises based on \text{chapter\hspace{2.4pt}\thechapter}}}  % END OF MAJOR TITLE line 5

\vspace{9pt} % BBBBBBBBBB
\noindent\def\stackalignment{l}\stackunder[4.5pt]
{\colorbox{LightOrange} % continue
{\libertineSB\fontsize{11}{14}\selectfont{\textcolor{ThemeColor}
{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{1.5pt}\pageref{\AnswerRef}
\ExerciseHeaderTitle}\,}}} %
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}\vspace{3pt}} % ELSE line 7
%
%
{\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 8
%
        {\libertineSB\fontsize{11}{14}\selectfont{\textcolor{ThemeColor} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{1.5pt}\pageref{\AnswerRef}
\ExerciseHeaderTitle}\,}}} %
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}\vspace{3pt}}}


\begin{document}

\setstretch{1.20}
\chapter{} % Chapter 1

\setboolean{firstanswerofthechapter}{true}
\begin{Exercise}[title={title of FIRST exercise},label=chap_01_exer_01] % EX 1_1
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{false}
\begin{Exercise}[title={title of SECOND exercise},label=chap_01_exer_02] % EX 1_2
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\begin{Exercise}[title={title of THIRD exercise},label=chap_01_exer_03] % EX 1_3

\Question{Question 1}
\Question{Qustion 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{true}
\begin{Answer}[ref={chap_01_exer_01}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\vspace{6pt}
\noindent {\color{DarkBlue}\fontsize{11}{12}\selectfont{The wording\hspace{0.6pt}: ``\,title of THIRD exercise\,'' is what I wish to change - it should be the

\noindent same as the title description in the corresponding question\hspace{0.7pt}.

\vspace{4pt}
\noindent It suggests the use of a construction using a ``\,hash $1$\,'' - but I do not understand that.}}

\vspace{15pt}
\setboolean{firstanswerofthechapter}{false}
\begin{Answer}[ref={chap_01_exer_02}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\begin{Answer}[ref={chap_01_exer_03}]
\Question{Answer to question 1}

\Question{Answer to question 2}
\end{Answer}

\end{document}

答案1

您现在的代码存在问题,您指的\ExerciseTitle是全局的。当您写下所有练习,然后写下所有答案时,最后一个练习会改变的值,\ExerciseTitle而当您展开它时得到的就是最后一个值。

您可以尝试各种花哨的技巧,\expandafter并尝试强制 Latex 将标题的内容保存在一个框中,然后在答案中需要标题时将其打印出来。然而,有人已经想到了这样的解决方案:

您有选项answeresdelayed。使用此选项时,答案不会被排版,而是保存在一个框中。然后当您使用时,\shipoutAnswer它会按顺序排版它们。

所以你要做的就是按照这种方式编写代码:练习1 -> 对应的答案1 -> 练习2 -> 对应的答案2。然后\shipoutAnswer在最后。

%--------------------------------------------------------------------------------------------------------- %
\documentclass[english]{book}
%--------------------------------------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage[explicit]{titlesec} %
\usepackage{titletoc} %
\usepackage{fancyhdr} %
\usepackage{fancyref} %
\usepackage{babel} %
\usepackage{tikz} % tikz - essential
\usepackage{newtxtext} %
\usepackage{libertine} %
\usepackage{setspace} %
\usepackage{colortbl} % changed to just colortbl
\usepackage{stackengine} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,lmargin=12.5mm,rmargin=12.5mm,bindingoffset=12.5mm,noheadfoot,nomarginpar,showframe,showcrop]{geometry}
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{stackengine} %
\usepackage{graphicx} %
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{ifpdf} %
\usepackage[answerdelayed]{exercise} %
\usepackage{ifthen}\newboolean{firstanswerofthechapter} %
\usepackage{hyperref} %

\definecolor{ThemeColor}{rgb}{0.50,0.04,0.04} % ThemeColor DarkRed
\definecolor{DarkBlue}{rgb}{0.00,0.08,0.45} % #000086
\definecolor{DarkOrange}{rgb}{0.55,0.22,0.05} % #FB9902
\definecolor{DarkRed}{rgb}{0.45,0.06,0.06} % #880000
\definecolor{LightOrange}{rgb}{1.00,0.87,0.64} % #FB9902
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85}

\tcbset{myinnerbox/.style={no shadow, shrink tight, arc=1.5pt, extrude by=2.0mm,
colframe=black, boxrule=0.7pt, frame style={opacity=0.25},
interior style={opacity=0.5}, colback=black!5!white}}

%
% ------------------------------------- Exercise preliminaries ------------------------------------------- %
%

\makeatletter
\let \@subQuestionHeader@original=\@subQuestionHeader
\newcommand{\@subQuestionHeader@noItem}{ %
    \hspace{0pt} % between (a) and (b)
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderDifficulty
        \subQuestionNB\hspace{0.4pt} % adjust space from the ) bracket
    \endgroup
    \begingroup
        \@getsubQuestionInfo
        \subQuestionHeaderTitle
    \endgroup % affects subquestion on new line
    \ignorespaces
        }

\newcommand{\subQuestionOnSameLine}{ %
    \let \@subQuestionHeader=\@subQuestionHeader@noItem
    \subQuestion
}
\newcommand{\subQuestionOnNewLine}{ %
    \let \@subQuestionHeader=\@subQuestionHeader@original {} % \\[-9pt]
    \subQuestion
}
\makeatother

\makeatletter
\def\endExerciseEnv{\termineliste{1}\@EndExeBox\vspace{6pt}%
\vskip\ExerciseSkipAfter}

\gdef\endAnswerEnv{\termineliste{1}\@EndExeBox\vspace{8pt}\termineliste{0}\vskip\AnswerSkipAfter}
\makeatother

\renewcommand{\AtBeginExercise}{\vspace{-6pt}}
%
% ----------------------------------------------- Define counters ------------------------------------ %
%
\numberwithin{Exercise}{chapter} % ! not counterwithin
\numberwithin{Answer}{chapter}
\newcounter{problem}[Exercise] %

\renewcommand{\ExerciseName}{Exercise}
\renewcommand{\QuestionNB}{\tcbhighmath[size=fbox,colframe=DarkRed,colback=blue!12!white]{\arabic{Question}}\hspace{3.2pt}}
\renewcommand{\QuestionHeaderTitle}{\emph{(\QuestionTitle)}\ }
\renewcommand{\subQuestionNB}{\alph{subQuestion}\hspace{0.7pt})\hspace{1.3pt}}
\renewcommand{\ExerciseHeaderTitle}{\hspace{2.4pt}-\hspace{1.6pt}-\hspace{1.6pt}-\;\ExerciseTitle}
\renewcommand{\QuestionHeaderDifficulty}{\theQuestionDifficulty\ }


% Setting up the EXERCISES header (conditional)...
\renewcommand{\ExerciseHeader}
{\ifthenelse{\boolean{firstanswerofthechapter}} % line 1
%
{\newpage\noindent\hspace{1.5pt}\vspace*{4pt} % line 2

% AAAAAAAAAAAA
{\libertineSB\fontsize{12}{14}\selectfont{\text{Exercises based on the content of \text{chapter\hspace{2.4pt}\thechapter}}}} % END OF MAJOR TITLE line 5

\vspace{9pt} % BBBBBBBBBBBB
\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 6
{\libertineSB\fontsize{11.5}{15}\selectfont{\textcolor{ThemeColor}{Exercise~\ExerciseHeaderNB}}}\ExerciseHeaderTitle\newline\smallskip}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}} % ELSE line 7
%
    {\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 8
%
        {\libertineSB\fontsize{11.5}{15}\selectfont{\textcolor{ThemeColor} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   
{Exercise~\ExerciseHeaderNB}}}\ExerciseHeaderTitle\newline\newline\smallskip} % line 9
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}}} % line 10



% Setting up the ANSWERS header (conditional)...
\renewcommand{\AnswerHeader}
{\ifthenelse{\boolean{firstanswerofthechapter}} % this is correct
%
{\newpage\noindent\hspace{1.5pt}\vspace*{4pt}
% AAAAAAAAAAAA
{\libertineSB\fontsize{11}{14}\selectfont{Answers\hspace{1.2pt}/\hspace{0.1pt}solutions to exercises based on \text{chapter\hspace{2.4pt}\thechapter}}}  % END OF MAJOR TITLE line 5

\vspace{9pt} % BBBBBBBBBB
\noindent\def\stackalignment{l}\stackunder[4.5pt]
{\colorbox{LightOrange} % continue
{\libertineSB\fontsize{11}{14}\selectfont{\textcolor{ThemeColor}
{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{1.5pt}\pageref{\AnswerRef}
\ExerciseHeaderTitle}\,}}} %
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}\vspace{3pt}} % ELSE line 7
%
%
{\noindent\def\stackalignment{l}\stackunder[4.5pt]{\colorbox{LightOrange} % line 8
%
        {\libertineSB\fontsize{11}{14}\selectfont{\textcolor{ThemeColor} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{1.5pt}\pageref{\AnswerRef}
\ExerciseHeaderTitle}\,}}} %
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}\vspace{3pt}}}


\begin{document}

\setstretch{1.20}
\chapter{} % Chapter 1

\setboolean{firstanswerofthechapter}{true}
\begin{Exercise}[title={title of FIRST exercise},label=chap_01_exer_01] % EX 1_1
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\begin{Answer}[ref={chap_01_exer_01}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\setboolean{firstanswerofthechapter}{false}
\begin{Exercise}[title={title of SECOND exercise},label=chap_01_exer_02] % EX 1_2
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\begin{Answer}[ref={chap_01_exer_02}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}


\begin{Exercise}[title={title of THIRD exercise},label=chap_01_exer_03] % EX 1_3

\Question{Question 1}
\Question{Qustion 2}
\end{Exercise}

\begin{Answer}[ref={chap_01_exer_03}]
\Question{Answer to question 1}

\Question{Answer to question 2}
\end{Answer}

\shipoutAnswer



\end{document}

答案2

我已编辑 \AnswerHeader,使其包含一个额外的可选参数 #1,这使我能够手动将练习描述的精确副本输入到答案标题中 - 从而在练习和答案之间建立万无一失的关联。在我看来,通过数字进行交叉引用并不能提供足够有意义的检查。

相关内容