Hyperref 和答案包:如何在解决方案的末尾放置返回问题的链接

Hyperref 和答案包:如何在解决方案的末尾放置返回问题的链接

在下面的 MWE 中,根据网站上的其他线程创建,我想知道如何更改以下两件事:

1)如何在解决方案的末尾添加链接回问题的按钮(例如,下面 MWE 中的按钮“返回问题 1.1”),就像我对问题末尾的按钮“解决方案”所做的那样。

2)从解决方案回到问题的超链接似乎有点偏离,即它们链接回比问题实际开始位置更低的地方。

非常感谢。

\documentclass[10pt,A4paper]{article}

\usepackage{answers}
\usepackage{amsthm}
\usepackage{hyperref}

\usepackage{tcolorbox}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{shadows}

\tikzstyle{buttonstyle} = [rectangle, fill = black!30, draw = black!80, drop shadow, font={\sffamily\bfseries}, text=white]

\newcommand*{\button}[1]{\tikz[baseline=(text.base)]{\node[buttonstyle] (text) {#1};}}


\theoremstyle{definition}
\newtheorem{problem}{%
  \hypertarget{soln:\theproblem}{}
  }[section]




\Newassociation{soln}{mySoln}{Solutions}
\renewenvironment{mySoln}[1]
   {\bigskip\noindent\phantomsection{\bfseries \hypertarget{problem:#1}{}
{\bfseries Solution to problem #1}\hfill
  \hyperlink{soln:#1}{\button{Back to problem #1}}\\}\quad}


\newcommand{\marksol}{\vspace{0.2cm}\hyperlink{problem:\theproblem}{\button{Solution}}}


\newcommand{\bp}{\begin{problem}}
\newcommand{\enp}{\end{problem}}
\newcommand{\bs}{\marksol \begin{soln}}

\begin{document}



\newpage
\section{Assigned problems}
\Opensolutionfile{Solutions}
\bp Let $a$ and $b$ be positive real numbers. Prove that
\[\frac{a^2}{b}+\frac{b^2}{a}\geq a+b.\]

\bs We have
\[\frac{a^2}{b}+\frac{b^2}{a}-a-b=\frac{a^3+b^3-a^2b-ab^2}{ab}=\frac{(a-b)(a^2-b^2)}{ab}=\frac{(a-b)^2(a+b)}{ab}\geq 0.\]

\end{soln}
\enp

\bp Let $a,b,c,d$ be positive real numbers such that $a>b>c>d$ and $ad=bc$. Prove that $a+d>b+c$.

\bs Let $c=d\epsilon$, then $b=\frac{a}{\epsilon}$, where $\epsilon >1$. We need
to prove \[a+d \geq \frac{a}{\epsilon}+ d\epsilon\] that is
\[a\cdot \frac{\epsilon-1}{\epsilon}-d(\epsilon-1)\geq 0.\]
But this is equivalent to
\[\left(\frac{a}{\epsilon}-d\right)(\epsilon-1)\geq 0,\]
which is true because $\frac{a}{\epsilon}=b>d$ and $\epsilon>1$.

\end{soln}
\enp






\Closesolutionfile{Solutions}


\eject


\section{Solutions}

\input{Solutions.tex}
\end{document}

答案1

要在解决方案末尾获取“返回...”按钮,必须使用环境定义的尾端mySoln。但是,不能#1在尾端使用样式参数,因此我在环境的前端另存#1\tmpmysoln,然后在尾端使用它。

至于问题的另一部分,我不知道如何重定向超链接的精确指向位置。

\documentclass[10pt,A4paper]{article}

\usepackage{answers}
\usepackage{amsthm}
\usepackage{hyperref}

\usepackage{tcolorbox}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{shadows}

\tikzstyle{buttonstyle} = [rectangle, fill = black!30, draw = black!80, drop shadow, font={\sffamily\bfseries}, text=white]

\newcommand*{\button}[1]{\tikz[baseline=(text.base)]{\node[buttonstyle] (text) {#1};}}


\theoremstyle{definition}
\newtheorem{problem}{%
  \hypertarget{soln:\theproblem}{}
  }[section]




\Newassociation{soln}{mySoln}{Solutions}
\renewenvironment{mySoln}[1]
   {\bigskip\noindent\phantomsection{\bfseries \hypertarget{problem:#1}{}
{\bfseries Solution to problem #1}\gdef\tmpmysoln{#1}\\}\quad}
{\vspace{0.2cm}\hyperlink{soln:\tmpmysoln}{\button{Back to problem \tmpmysoln}}\par}


\newcommand{\marksol}{\vspace{0.2cm}\hyperlink{problem:\theproblem}{\button{Solution}}}


\newcommand{\bp}{\begin{problem}}
\newcommand{\enp}{\end{problem}}
\newcommand{\bs}{\marksol \begin{soln}}

\begin{document}



\newpage
\section{Assigned problems}
\Opensolutionfile{Solutions}
\bp Let $a$ and $b$ be positive real numbers. Prove that
\[\frac{a^2}{b}+\frac{b^2}{a}\geq a+b.\]

\bs We have
\[\frac{a^2}{b}+\frac{b^2}{a}-a-b=\frac{a^3+b^3-a^2b-ab^2}{ab}=\frac{(a-b)(a^2-b^2)}{ab}=\frac{(a-b)^2(a+b)}{ab}\geq 0.\]

\end{soln}
\enp

\bp Let $a,b,c,d$ be positive real numbers such that $a>b>c>d$ and $ad=bc$. Prove that $a+d>b+c$.

\bs Let $c=d\epsilon$, then $b=\frac{a}{\epsilon}$, where $\epsilon >1$. We need
to prove \[a+d \geq \frac{a}{\epsilon}+ d\epsilon\] that is
\[a\cdot \frac{\epsilon-1}{\epsilon}-d(\epsilon-1)\geq 0.\]
But this is equivalent to
\[\left(\frac{a}{\epsilon}-d\right)(\epsilon-1)\geq 0,\]
which is true because $\frac{a}{\epsilon}=b>d$ and $\epsilon>1$.

\end{soln}
\enp






\Closesolutionfile{Solutions}


\eject


\section{Solutions}

\input{Solutions.tex}
\end{document}

在此处输入图片描述

答案2

通过\RenewDocumentEnvironment它可以访问#1最后的参数——环境代码solution

\documentclass[10pt,A4paper]{article}

\usepackage{answers}
\usepackage{amsthm}

\usepackage{tcolorbox}
\usepackage{ifthen}
\usepackage{tikz}

\usepackage{xparse}
\usepackage{hyperref}

\usetikzlibrary{shadows}

\tikzstyle{buttonstyle} = [rectangle, fill = black!30, draw = black!80, drop shadow, font={\sffamily\bfseries}, text=white]



\newcommand*{\button}[1]{\tikz[baseline=(text.base)]{\node[buttonstyle] (text) {#1};}}


\theoremstyle{definition}
\newtheorem{problem}{%
  \phantomsection\hypertarget{soln:\theproblem}{}
  }[section]




\Newassociation{soln}{mySoln}{Solutions}
\RenewDocumentEnvironment{mySoln}{m}{%

  \bigskip\noindent{\phantomsection\hypertarget{problem:#1}{\bfseries Solution to problem #1}}

}{%
  \bigskip
  \phantomsection\hyperlink{soln:#1}{\button{Back to problem #1}}
}


\newcommand{\marksol}{\vspace{0.2cm}\phantomsection\hyperlink{problem:\theproblem}{\button{Solution}}}


\newcommand{\bp}{\begin{problem}}
\newcommand{\enp}{\end{problem}}
\newcommand{\bs}{\marksol \begin{soln}}

\begin{document}



\newpage
\section{Assigned problems}
\Opensolutionfile{Solutions}
\bp Let $a$ and $b$ be positive real numbers. Prove that
\[\frac{a^2}{b}+\frac{b^2}{a}\geq a+b.\]

\bs We have
\[\frac{a^2}{b}+\frac{b^2}{a}-a-b=\frac{a^3+b^3-a^2b-ab^2}{ab}=\frac{(a-b)(a^2-b^2)}{ab}=\frac{(a-b)^2(a+b)}{ab}\geq 0.\]

\end{soln}
\enp

\bp Let $a,b,c,d$ be positive real numbers such that $a>b>c>d$ and $ad=bc$. Prove that $a+d>b+c$.

\bs Let $c=d\epsilon$, then $b=\frac{a}{\epsilon}$, where $\epsilon >1$. We need
to prove \[a+d \geq \frac{a}{\epsilon}+ d\epsilon\] that is
\[a\cdot \frac{\epsilon-1}{\epsilon}-d(\epsilon-1)\geq 0.\]
But this is equivalent to
\[\left(\frac{a}{\epsilon}-d\right)(\epsilon-1)\geq 0,\]
which is true because $\frac{a}{\epsilon}=b>d$ and $\epsilon>1$.

\end{soln}
\enp






\Closesolutionfile{Solutions}


\eject


\section{Solutions}

\input{Solutions.tex}
\end{document}   


\theoremstyle{definition}
\newtheorem{problem}{%
  \phantomsection%
  \hypertarget{soln:\theproblem}{}%
}[section]




\Newassociation{soln}{mySoln}{Solutions}
\renewenvironment{mySoln}[1]{%
  \bigskip\noindent\phantomsection{\bfseries \hypertarget{problem:#1}{}%
    {\bfseries Solution to problem #1}\hfill\phantomsection\hyperlink{soln:#1}{\button{Back to problem #1}\quad}\leavevmode%
  }


\newcommand{\marksol}{\vspace{0.2cm}\phantomsection\hyperlink{problem:\theproblem}{\button{Solution}}}


\newcommand{\bp}{\begin{problem}}
\newcommand{\enp}{\end{problem}}
\newcommand{\bs}{\marksol \begin{soln}}

\begin{document}



\newpage
\section{Assigned problems}
\Opensolutionfile{Solutions}
\bp Let $a$ and $b$ be positive real numbers. Prove that
\[\frac{a^2}{b}+\frac{b^2}{a}\geq a+b.\]

\bs We have
\[\frac{a^2}{b}+\frac{b^2}{a}-a-b=\frac{a^3+b^3-a^2b-ab^2}{ab}=\frac{(a-b)(a^2-b^2)}{ab}=\frac{(a-b)^2(a+b)}{ab}\geq 0.\]

\end{soln}
\enp

\bp Let $a,b,c,d$ be positive real numbers such that $a>b>c>d$ and $ad=bc$. Prove that $a+d>b+c$.

\bs Let $c=d\epsilon$, then $b=\frac{a}{\epsilon}$, where $\epsilon >1$. We need
to prove \[a+d \geq \frac{a}{\epsilon}+ d\epsilon\] that is
\[a\cdot \frac{\epsilon-1}{\epsilon}-d(\epsilon-1)\geq 0.\]
But this is equivalent to
\[\left(\frac{a}{\epsilon}-d\right)(\epsilon-1)\geq 0,\]
which is true because $\frac{a}{\epsilon}=b>d$ and $\epsilon>1$.

\end{soln}
\enp






\Closesolutionfile{Solutions}


\eject


\section{Solutions}

\input{Solutions.tex}
\end{document}

在此处输入图片描述

相关内容