1

I have minor problems with the solutions shown in Hyperlink with answers package

I wanted to make a macro which takes solution and exercise as arguments, uses a button like mutual reference, and separates visually from the environment. Here goes my MWE:

\documentclass[12pt,a4paper]{book}
\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{\node[buttonstyle] {#1};}}

\theoremstyle{definition}
\newtheorem{ex}{%
  \hypertarget{sol:\theex}{ } \hfill
  \hyperlink{ex:\theex}{\button{Solution}}\\
  \phantomsection
  }[chapter]
\Newassociation{sol}{Soln}{corr}
\renewenvironment{Soln}[1]
  {\par\bigskip\noindent\phantomsection{\bfseries \hypertarget{ex:#1}{}
{\bfseries Solution  of exercise #1}\hfill
  \hyperlink{sol:#1}{\button{Back to exercise #1}}\\}\quad}
  {\par\bigskip}

% Usage \MEExercise[keys]{exercise}{solution}
\newcommand\MEExercise[3][]{
\ifthenelse{\equal{#3}{}}
{ % The no solution case
}
{ %Have a solution
\begin{tcolorbox}
\begin{ex}
#2
\begin{sol}
#3
\end{sol}
\end{ex}
\end{tcolorbox}
}
}

\begin{document}
\chapter{This}
\Opensolutionfile{corr}[ans1]
\section{Problems}
\begin{tcolorbox}
\begin{ex}
First exercise.
\begin{sol}
First solution.
\end{sol}
\end{ex}
\end{tcolorbox}

%\MEExercise{this is exercise 1}{this is solution of exercise 1}
\chapter{That}
%\newpage
\begin{ex}
Second exercise.
\begin{sol}
Second solution.
\end{sol}
\end{ex}
%\MEExercise{this is exercise 2}{this is solution of exercise 2}
%\newpage
\Closesolutionfile{corr}
%\newpage
\section{Solutions}
\input{ans1}
\end{document}

My problems: 1./ the hyperlink buttons seem to be positioned quite unrelated to the exercise and solution. If I put the button and the exercise into a box, it is more or less acceptable, but it would be better to place the button at the end of the exercise text

2./ I want to put the stuff in a macro, see \MEExercise. However, in that case the content of the ans1 file is

\begin{Soln}{1.1}
First solution.
\end{Soln}
\begin{Soln}{1.2}
this is solution of exercise 1 \end {sol} \end {ex} \end {tcolorbox}  
\chapter{That}
%\newpage
\begin{ex}
Second exercise.
\begin{sol}
Second solution.
\end{Soln}

and it leads to the error that \begin{Soln}{1.2} is closed by \end {sol}

  • I guess, that \phantomsection should go before \hyperlink etc. – user31729 Nov 23 '14 at 11:30
  • You can align the button text with the current line using \newcommand*{\button}[1]{\tikz[baseline=(text.base)]{\node[buttonstyle] (text) {#1};}} – John Kormylo Nov 23 '14 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.