2

In the MWE below, created following other threads from the website, I would like to know how to change the following two things:

1) How to add the buttons which link back to the question (E.g. the button "Back to problem 1.1" in the MWE below) at the end of the solution, as I have done with the button "Solution" which comes at the end of the question.

2) The hyperlinks from the solutions back to the questions seem to be a bit off, i.e. they link back somewhere lower than the actual start of the question.

Thank you very much.

\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}

2 Answers 2

3

To get the "Back to..." button at the end of the solution, one has to use the tail end of the environment defnition of mySoln. However, one cannot employ #1 style parameters in the tail end, and so I save #1 as \tmpmysoln in the front end of the environment, and then use it in the tail end.

As to the other part of the question, I have no idea how to redirect the precise pointing location of a hyperlink.

\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}

enter image description here

0
2

With \RenewDocumentEnvironment it's possible to access the #1 parameter in the end - code of the solution environment.

\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}

enter image description here

2
  • The \phantomsection command does not seem to fix the problem with the hyperlink being a bit off. Do you know how to fix this? Commented Apr 28, 2016 at 19:19
  • @JakeHaider: A lot of hacking of hyperlink anchors, or a \raisebox stuff. Nothing that I can do as quick setup
    – user31729
    Commented Apr 28, 2016 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .