自动链接到练习包中的答案

自动链接到练习包中的答案

我已阅读这个问题关于如何在使用练习包时手动在每个练习中包含答案的链接。

我的问题是,是否可以通过在序言中添加一些代码来以某种方式实现此自动化,以便每个练习自动包含指向答案的链接。

\documentclass[11pt,twoside]{article}
 \usepackage{amsmath}

 \usepackage[colorlinks=true]{hyperref}
 \hypersetup{allcolors=magenta}

 \usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
 \colorlet{lightcyan}{cyan!40!white}

 \usepackage{ifthen}
 \newboolean{firstanswerofthechapter}  

 \usepackage{chngcntr}
 \usepackage{stackengine}

 \usepackage[lastexercise,answerdelayed]{exercise}
  \counterwithin{Exercise}{section}
  \counterwithin{Answer}{section}
  \renewcounter{Exercise}[section]
  \newcommand{\QuestionNB}{\bfseries\arabic{Question}.\ }
  \renewcommand{\ExerciseName}{\textbf{Exercise}}
  \renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
  \stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}
  \renewcommand{\AnswerName}{Exercise}
  \renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thesection}}\newline\newline%
    \noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page %
            \pageref{\AnswerRef}}}\smallskip}
{\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page \pageref{\AnswerRef}}}\smallskip}}
   \setlength{\QuestionIndent}{16pt}

  \begin{document} 

  \section{Section 1}

   \begin{Exercise}\label{EX1}
     Answers here: \refAnswer{EX1}
    \Question question

    \Question question

    \Question question

    \end{Exercise}

    \setboolean{firstanswerofthechapter}{true}
    \begin{Answer}[ref={EX1}]
     \Question answer
     \Question answer
     \Question answer
    \end{Answer}
    \setboolean{firstanswerofthechapter}{false}

    \section{Answers}
     \shipoutAnswer

    \end{document}

答案1

似乎练习提供了练习和答案计数器,这将提供自动命名协议。 \AtBeginExercise将处理\label。添加参数\begin{Answer}有点困难。

\documentclass[11pt,twoside]{article}
 \usepackage{amsmath}

 \usepackage[colorlinks=true]{hyperref}
 \hypersetup{allcolors=magenta}

 \usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
 \colorlet{lightcyan}{cyan!40!white}

 \usepackage{ifthen}
 \newboolean{firstanswerofthechapter}  

 \usepackage{chngcntr}
 \usepackage{stackengine}

 \usepackage[lastexercise,answerdelayed]{exercise}
  \counterwithin{Exercise}{section}
  \counterwithin{Answer}{section}
  %\renewcounter{Exercise}[section]% ???
  \renewcommand{\QuestionNB}{\bfseries\arabic{Question}.\ }
  \renewcommand{\ExerciseName}{\textbf{Exercise}}
  \renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
  \stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}
  \renewcommand{\AnswerName}{Exercise}
  \renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
{\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thesection}}\newline\newline%
    \noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page %
            \pageref{\AnswerRef}}}\smallskip
            \global\firstanswerofthechapterfalse}%
{\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page \pageref{\AnswerRef}}}\smallskip}}
   \setlength{\QuestionIndent}{16pt}
  
\renewcommand{\AtBeginExercise}{\label{EX\theExercise}
  Answers here: \refAnswer{EX\theExercise}}
  
\let\oldAnswer=\Answer
\renewcommand{\Answer}[1][\empty]{\ifx\empty#1\relax
  \oldAnswer[ref={EX\theAnswer}]
\else
  \oldAnswer[#1]
\fi}

  \begin{document} 

  \section{Section 1}

   \begin{Exercise}
     
    \Question question

    \Question question

    \Question question

    \end{Exercise}
    
       \begin{Exercise}
     
    \Question question

    \Question question

    \Question question

    \end{Exercise}
    
    %\setboolean{firstanswerofthechapter}{true}
    \begin{Answer}
     \Question answer
     \Question answer
     \Question answer
    \end{Answer}
   
    \begin{Answer}
     \Question answer
     \Question answer
     \Question answer
    \end{Answer}

    \section{Answers}
     \shipoutAnswer

    \end{document}

相关内容