# Hyperlinking conditions with math symbols error

I have this code borrowed from a similar stack thread

\newcounter{susis}

\ifcsname r@susistarget#1\endcsname
\else
\fi
}

\newcommand{\myHypertarget}[2]{%
\refstepcounter{susis}\label{susistarget#1}%
\hypertarget{#1}{\textcolor{teal}{#2}}%
\else
\hypertarget{#1}{\fbox{\textcolor{red}{#2}}}%
\fi
}


And it works when I run \myHyperlink{Hello}{Hello}. However, when I try to do \myHyperlink{\lor}{\lor} (logical OR symbol), it says: Missing \endcsname inserted. <to be read again> \lor.

How would I fix this error?

Additional info: I'm trying to target and link math operators. It works with normal hypertarget and hyperlink, but I want to include the additional feature: if the link refers to an undeclared target, it would display in red text instead of blue.

• welcome to tex.se! \lor is math expression, so it should be in math environment: $\lor$. if this influence on working of your hyper links, i cant test (you not provide complete small document beginning with \documentclass{...} and ending with \end{document}) – Zarko Feb 4 '18 at 5:39

You need to employ the directive in the form

\myHyperlink{lor}{$\lor$}


Two general recommendations:

• The first argument should be a simple string -- no macros of any kind


The internal name is “stringified” and suitable protection is added around it in the .aux file.

\documentclass{article}
\usepackage[
paperwidth=5cm,
paperheight=5cm,
vmargin=2mm,
hmargin=0.5cm,
]{geometry}
\usepackage{xcolor,xparse,hyperref}

\newcounter{susis}

\ExplSyntaxOn

{
\japseow_hyperlink:nn { \tl_to_str:n { #1 } } { #2 }
}
\NewDocumentCommand{\myHypertarget}{ O{#2}m }
{
\japseow_hypertarget:nn { \tl_to_str:n { #1 } } { #2 }
}

{
\tl_if_exist:cTF { r@susistarget#1 }
{
}
{
}
}
\cs_new_protected:Nn \japseow_hypertarget:nn
{
\refstepcounter{susis}\label{susistarget\protectlabel{#1}}
\tl_if_exist:cTF { r@susislink#1 }
{
\hypertarget{#1}{\textcolor{teal}{#2}}
}
{
\hypertarget{#1}{\fbox{\textcolor{red}{#2}}}
}
}
\NewDocumentCommand\protectlabel{m}
{
\tl_to_str:n { #1 }
}
\ExplSyntaxOff

\begin{document}

\myHypertarget{Hello}\clearpage

\myHyperlink{$\lor$}\clearpage

\myHypertarget{$\lor$}\clearpage

\myHyperlink[weird]{This is too weird for using as a link}\clearpage

\myHypertarget[weird]{Good2}\clearpage

\myHypertarget[ops]{Whatever}

\end{document}


• Wow it works! But, I'm having a problem in my \begin{tcolorbox} \begin{align} ... \end{align} \end{tcolorbox}. I'm getting: Multiple \label's: label 'susistarget\protectlabel {unusedlabed}' will be lost. whenever I try to use the myHypertarget within the tcolorbox and align – japseow Feb 4 '18 at 13:30
• It seems to happen when I have a hypertarget and hyperlink at the same line (even if the link doesn't refer to the target) – japseow Feb 4 '18 at 13:37
• @japseow With align it's a very different thing, I'm afraid. – egreg Feb 4 '18 at 13:43
• I'm not very well-versed in LaTex, but will this solution work? I create a dynamic array/arraylist of strings. Whenever I create a new hypertarget, I add the "string-ified" input into the arraylist. Then when I call a new hyperlink, it checks if the stringified input is contained in the arraylist. If not, color it red instead of blue. Though, I don't even have a clue on how to implement it. – japseow Feb 4 '18 at 15:12

The desired functionality can be accomplished by combining these answers:

https://tex.stackexchange.com/a/168835/143704

https://tex.stackexchange.com/a/30484/143704