The goal is to get a pair of commands, \mytarget(*) and \mylink/\Mylink. Specifically, I am interested why "my" solution does NOT work, and how to fix it (if possible).
Syntax
\mytarget{<label name>}{<Text to display>}\mytarget*{<label name>}{<Text to display>}\mylink{<label name>}{<Text to instead display / could be empty>}\mylink{<label name>}{<Text to instead display / could be empty>}
Usage
The command
\mytargetis intended as an anchor that can be referenced before/after its usage in document.- Unstarred version,
\mytarget, is also part of the text. For example,bla bla \mytarget{label name here}{123} uh uhshould givebla bla 123 uh uh, and the "123" part could be referenced earlier/later by\mylink/\Mylink. - Starred version,
\mytarget*, is not part of the text. Sobla bla \mytarget*{label name here}{123} uh uhshould givebla bla uh uh, and the "123" part could be referenced earlier/later by\mylink/\Mylink. Notice there is no "123" visible here.
- Unstarred version,
The commands
\mylink/\Mylinkdisplay the links to a corresponding\mytarget.- For example,
bla bla \mytarget*{label name here}{123} uh uh \mylink{label name here}{}would yieldbla bla uh uh 123. The "123" part here is a link to the spot between "bla bla" and "uh uh". (Of course, this example is ridiculous and, of course, in reality the link leads to the line itself). - If the second argument for
\mylink{}{}/\Mylink{}{}is given, it should override the original second argument of\mytarget(*). For instance,bla bla \mytarget*{label name here}{123} uh uh \mylink{label name here}{654}should givebla bla uh uh 654. The "654" part is again a link. - The difference between
\mylink/\Mylinkis that\mylinkis faithful to the second argument of\mytarget(*){}{}. Whereas\Mylinkdoes everything the same way expect\Mylinkcapitalises the first letter of the second argument. Sobla bla \mytarget*{label name here}{turtle} uh uh \mylink{label name here}{}would givebla bla uh uh turtle; whereasbla bla \mytarget*{label name here}{turtle} uh uh \Mylink{label name here}{}yieldsbla bla uh uh Turtle. Notice that "turtle" is capitalised to "Turtle" in the second case. In both cases, "turtle" and "Turtle" are links.
- For example,
Effort
Based on helpful comments from Marijn, I was led to this answer and package crossreftools. Here is what I tried:
\documentclass[12pt]{book}
\usepackage{amsmath}
\usepackage{aligned-overset}
\usepackage{commath}
\usepackage[shortlabels]{enumitem}
\usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false]{hyperref}
\usepackage{crossreftools}
\makeatletter
\newcommand{\mytarget}{@ifstar{@mytargetstar}{@mytargetnostar}}
\newcommand{@mytargetstar}[2]{\crtcrossreflabel*{#2}[#1]@ifnextchar\space{\hspace{-1sp}}{}}
\newcommand{@mytargetnostar}[2]{\crtcrossreflabel{#2}[#1]@ifnextchar\space{\hspace{-1sp}}{}}
\DeclareRobustCommand{\mylink}[2]{%
\if\relax\detokenize{#2}\relax
\crtnameref{#1}
\else
\crthyperlink{#1}{#2}
\fi
\@ifnextchar\space{\hspace{-1sp}}{}
}
\DeclareRobustCommand{\Mylink}[2]{%
\if\relax\detokenize{#2}\relax
\crtunameref{#1}
\else
\crthyperlink{#1}{\MakeUppercase #2}
\fi
\@ifnextchar\space{\hspace{-1sp}}{}
}
\makeatother
\begin{document}
a b c \mytarget*{anchor}{I0--I1} d e f
\newpage
Why doesn't the link of \mylink{anchor}{ullallaa} lead anywhere?
\end{document}
Problems with my effort
- Why doesn't the link of
\mylink{anchor}{ullallaa}lead anywhere? - (Not compulsory) Should I change the remaining three
newcommands into\DeclareRobustCommands? - (Not compulsory) Is there a better way to get rid of spaces than putting
\@ifnextchar\space{\hspace{-1sp}}{}everywhere?
I am running
pdflatex3.141592653-2.6-1.40.22 (MikTeX 21.3), and every package in my local distrubution was updated on 28/04/2021. Output is identical (in terms of errors, and what link doesn't work without the 3 packages) on Overleaf with default settings.