0

At the end of each problem, I'd like to have the indication of the page number of the solution to that problem.

If I do something like this,

\documentclass{article}

\usepackage{amsthm,thmtools}

\declaretheoremstyle[
notefont=\bfseries,
notebraces={}{}, 
headformat={\large\NUMBER.\NOTE},
headpunct={\vspace{\topsep}\newline},
spacebelow=40pt,
prefoothook={\hfill Solução: pág. \pageref{solucao:\Currentlabel}}
]{problemastyle}

\declaretheorem[style=problemastyle]{problema}

\usepackage{answers}

\Newassociation{solucao}{Sol}{solucoes}
\renewenvironment{Sol}[1]{\begin{trivlist}
 \item[\hskip\labelsep\textbf{#1.}] \label{solucao:#1} \ignorespaces}%
 {\end{trivlist}}

\usepackage{hyperref}

\usepackage{lipsum}

\begin{document}
\Opensolutionfile{solucoes}

\begin{problema}[Primeiro problema]
 Problema 1.
 \begin{solucao}
   \lipsum
 \end{solucao}
\end{problema}

\begin{problema}[Segundo problema]
 Problema 2.
 \begin{solucao}
   \lipsum[1]
 \end{solucao}
\end{problema}

\begin{problema}[Terceiro problema]
 Problema 3.
 \begin{solucao}
   \lipsum[1]
 \end{solucao}
\end{problema}


\Closesolutionfile{solucoes}

\include{solucoes}

\end{document}

I get what I want, but the links on the page numbers are all pointing to the same place.

How can I correct this?

2
  • 1
    a trivlist doesn't set a link target by default. Use \item[{\MakeLinkTarget[page]{}}\hskip\labelsep\textbf{#1.}], or (if your system is older) \item[\phantomsection\hskip\labelsep\textbf{#1.}] Commented Dec 25, 2023 at 11:32
  • That solved it! Thank you!
    – Pedro
    Commented Dec 25, 2023 at 12:13

1 Answer 1

1

You need to have the environment Sol to not only produce a cross referencing label but to also create a named destination/target/anchor for linking whose name is made the new definition of the macro \@currentHref and thus is used by the command \label when creating the cross referencing label. You can use \phantomsection, but you can as well implement your own thing which uses the argument of the Sol-environment as the name for the named destination/target/anchor as well. In this case the argument of the Sol-environment may only consist of things which pdf-viewers can process as components of names of destinations/targets/anchors, and as no two destinations/targets/anchors may have the same name, in this case no two Sol-environment may have arguments that expand to the same things.

\documentclass{article}

\usepackage{amsthm,thmtools}

\declaretheoremstyle[
notefont=\bfseries,
notebraces={}{}, 
headformat={\large\NUMBER.\NOTE},
headpunct={\vspace{\topsep}\newline},
spacebelow=40pt,
prefoothook={\hfill Solução: pág. \pageref{solucao:\Currentlabel}}
]{problemastyle}

\declaretheorem[style=problemastyle]{problema}

\usepackage{answers}

\Newassociation{solucao}{Sol}{solucoes}

\renewenvironment{Sol}[1]{\begin{trivlist}%
 \item[\hskip\labelsep\SolutionAnchor{#1}\textbf{#1.}]\ignorespaces}%
 {\end{trivlist}}%

\makeatletter
\DeclareRobustCommand\SolutionAnchor[1]{%
  \begingroup
  \Hy@localanchornametrue
  \Hy@MakeCurrentHref{solution.#1}%
  \Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
  \label{solucao:#1}%
  \endgroup
}%
%
% Alternatively, using \phantomsection:
% 
% \DeclareRobustCommand\Hy@SolutionAnchor[1]{%
%   \begingroup
%   \Hy@localanchornametrue
%   \phantomsection
%   \label{solucao:#1}%
%   \endgroup
% }%
%
% Or, with a very recent TeX installation, use \MakeLinkTarget*
%
%\DeclareRobustCommand\SolutionAnchor[1]{%
%  \MakeLinkTarget*{solution.#1}%
%  \label{solucao:#1}%
%}%

\makeatother


\usepackage{hyperref}

\usepackage{lipsum}

\begin{document}
\Opensolutionfile{solucoes}

\begin{problema}[Primeiro problema]
 Problema 1.
 \begin{solucao}
   \lipsum
 \end{solucao}
\end{problema}

\begin{problema}[Segundo problema]
 Problema 2.
 \begin{solucao}
   \lipsum[1]
 \end{solucao}
\end{problema}

\begin{problema}[Terceiro problema]
 Problema 3.
 \begin{solucao}
   \lipsum[1]
 \end{solucao}
\end{problema}


\Closesolutionfile{solucoes}

\include{solucoes}

\end{document}
15
  • 1
    please no. Don't use internal and undocumented hyperref commands like \Hy@MakeCurrentHref or \Hy@raisedlink. There is no garanty that such commands will stay. There is an official interface with \MakeLinkTarget or \phantomsection. Commented Dec 25, 2023 at 11:45
  • @UlrikeFischer My experience is that even documented interfaces change.And new official interfaces often did not yet make it into the package management of the operating system in use. Commented Dec 25, 2023 at 12:57
  • Sorry, but the fact that you can read the code and find the internal commands doesn't mean that you should recommend that to other users. Such tricks are bad in the long run and it makes it very hard to improve hyperref if every internal change lead to complains from users. Commented Dec 25, 2023 at 13:59
  • 1
    sorry if I sound grumpy. But it isn't really nice to speak of update-terrorism, update terror and pseudo-fancy new stuff only because I ask you in view of future development not to use internal commands but an official interface. There are really enough options like \phantomsection (older) or \MakeLinkTarget (newer and also provided by the kernel) or \refstepcounter or even \hypertarget. Commented Dec 25, 2023 at 19:43
  • 1
    If someone uses or changes internals of an external package it is their job to adapt their package if these internals changes. If I change hyperref I naturally try to find breakages first and notify the authors, but nevertheless the responsability to resolve the problem lies by them. "Missing interfaces" may explain the use of the internals, but doesn't justify their use. They could have written their own package, or make a feature request for an interface instead. Commented Dec 29, 2023 at 11:50

You must log in to answer this question.

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