2

When using \property_ref:nn from the new ltproperties, how should one properly add hyperlink to it?


In the following example, I defined two commands \MyLabel and \MyRef. \MyLabel would record the current label and type, and \MyRef would print it accordingly. I would like to add hyperlink in \MyRef pointing to the corresponding MyLabel. However, directly using \hyperref[label]{...} leads to errors:

Use of \??? doesn't match its definition.
<argument> \???  

This error message doesn't really say much, so I have no idea where is actually going wrong.

There is a property called target that might be useful in this case (for example, maybe one should replace the label with \property_ref:nn { #1 } { target }?). However I didn't success in making it to work: when I write \property_record:nn { #1 } { label, type, target } instead of \property_record:nn { #1 } { label, type }, the errors gone, but the generated hyperlink does not work.

\documentclass{article}

\usepackage{hyperref}

\ExplSyntaxOn

\tl_new:N \l__mymodule_current_type_tl

\property_new:nnnn { type } { now } { unknown } { \l__mymodule_current_type_tl }
\NewDocumentCommand \MyLabel { m }
  {
    \group_begin:
    \exp_args:No \addtocounter { mymodule_ \l__mymodule_current_type_tl } { -1 }
    \exp_args:No \refstepcounter { mymodule_ \l__mymodule_current_type_tl }
    % \label{#1}
    \property_record:nn { #1 } { label, type }
    \group_end:
  }
\NewDocumentCommand \MyRef { m }
  {
    \group_begin:
    % \hyperref[#1]
      {
        \tl_use:c { l_mymodule_ \property_ref:nn { #1 } { type } _label_text_tl }
        \nobreakspace
        % \ref{#1}
        \property_ref:nn { #1 } { label }
      }
    \group_end:
  }

\newcounter{mymodule_T}
\tl_const:Nn \l_mymodule_T_label_text_tl { TEST }
\NewDocumentEnvironment { test } { }
  {
    \tl_set:Nn \l__mymodule_current_type_tl { T }
    \fbox{ \l_mymodule_T_label_text_tl \nobreakspace \tl_use:c {themymodule_T} }
  }
  {
    % Does nothing
  }

\ExplSyntaxOff

\begin{document}

\begin{test}\MyLabel{test}
\end{test}

\bigskip

\MyRef{test}

\end{document}

1 Answer 1

3

If you want to setup a hyperref link you need the target name. \label stores that automatically, but \property_record:nn not. There you have to add it explicitly. Also better do not define a generic property like type. This names should be reserved for the kernel. You will run into problems if other packages use such names too.

\documentclass{article}

\usepackage{hyperref}

\ExplSyntaxOn

\tl_new:N \l__mymodule_current_type_tl

\property_new:nnnn { jinwen/type } { now } { unknown } { \l__mymodule_current_type_tl }
\NewDocumentCommand \MyLabel { m }
  {
    \addtocounter { mymodule_ \l__mymodule_current_type_tl } { -1 }
    \refstepcounter { mymodule_ \l__mymodule_current_type_tl }
    \property_record:nn { #1 } { label, jinwen/type,target }
  }
\NewDocumentCommand \MyRef { m }
  {
     \hyperlink{\property_ref:nn{#1}{target}}
      {
        \cs_if_exist_use:c 
         { l_mymodule_ \property_ref:nn { #1 } { jinwen/type } _label_text_tl }
        \nobreakspace
        \property_ref:nn { #1 } { label }
      }
  }

\newcounter{mymodule_T}

\tl_const:Nn \c_mymodule_T_label_text_tl { TEST }
\NewDocumentEnvironment { test } { }
  {
    \tl_set:Nn \l__mymodule_current_type_tl { T }
    \fbox{ \c_mymodule_T_label_text_tl \nobreakspace \tl_use:c {themymodule_T} }
  }
  {
    % Does nothing
  }

\ExplSyntaxOff

\begin{document}

\begin{test}\MyLabel{test}
\end{test}

\bigskip

\MyRef{test}

\end{document}
1
  • 1
    Thank you for your advice!
    – Jinwen
    Commented Nov 20, 2023 at 22:09

You must log in to answer this question.

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