2

I have this code borrowed from a similar stack thread

\newcounter{susis}

\newcommand{\myHyperlink}[2]{%
\refstepcounter{susis}\label{susislink#1}%
\ifcsname r@susistarget#1\endcsname
\hyperlink{#1}{\textcolor{blue}{#2}}%
\else
\hyperlink{#1}{\textcolor{red}{#2}}%
\fi
}

\newcommand{\myHypertarget}[2]{%
\refstepcounter{susis}\label{susistarget#1}%
\ifcsname r@susislink#1\endcsname
\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.

  • 1
    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
1

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

  • If the second argument is a math-mode command, such as \lor, you need to employ math mode to typeset it, i.e., surround \lor with $ symbols.

enter image description here

\documentclass{article}
\usepackage{xcolor,hyperref}
\hypersetup{colorlinks}

\newcounter{susis}
\makeatletter
\newcommand{\myHyperlink}[2]{%
   \refstepcounter{susis}\label{susislink#1}%
   \ifcsname r@susistarget#1\endcsname
      \hyperlink{#1}{\textcolor{blue}{#2}}%
   \else
      \hyperlink{#1}{\textcolor{red}{#2}}%
   \fi
}
\newcommand{\myHypertarget}[2]{%
   \refstepcounter{susis}\label{susistarget#1}%
   \ifcsname r@susislink#1\endcsname
      \hypertarget{#1}{\textcolor{teal}{#2}}%
   \else
      \hypertarget{#1}{\fbox{\textcolor{red}{#2}}}%
   \fi
}
\makeatother

\begin{document}
\myHyperlink{Hello}{Hello} \myHypertarget{Hello}{Hello}

\myHyperlink{lor}{$\lor$} \myHypertarget{lor}{$\lor$}

\myHyperlink{good1}{Good1} \myHypertarget{good2}{Good2}

\myHyperlink{wedge}{$\wedge$} \myHypertarget{dots}{$\dots$}
\end{document}
  • 1
    I'm aware that it's discouraged to use macros to for hyperref, but aren't there any workarounds? targeting and linking works perfectly if where weren't any \ifcsnames around. Edit: And my notes is full of \land, \lor, \implies, \lnot, etc. and every time I call these things, I want to link them to the original target. It would be very difficult if for each call of \lor, i had to link{lor}{\lor}, instead of simply link{\lor} – japseow Feb 4 '18 at 8:33
  • @japseow - Both user-level macros contain the instruction \label{susislink#1}. Some "TeX-special" characters are allowed in the argument of \label -- but not all. In particular, the backslash character isn't allowed. Similarly, the directive \ifcsname r@susistarget#1\endcsname only works if #1 doesn't contain a backslash character. It's for these reasons that one a LaTeX macro that contains a backslash isn't allowed in the first argument of either \myHyperlink} or \myHypertarget}. – Mico Feb 4 '18 at 13:17
1

I would use an approach with an optional argument for the internal name; if it's present, it is used; otherwise, the mandatory argument is used.

However, you need to use $ around \lor, because it's an inherently mathematical command.

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,
  includehead,includefoot
]{geometry}
\usepackage{xcolor,xparse,hyperref}

\hypersetup{colorlinks}

\newcounter{susis}

\ExplSyntaxOn

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

\cs_new_protected:Nn \japseow_hyperlink:nn
 {
  \refstepcounter{susis}\label{susislink\protectlabel{#1}}
  \tl_if_exist:cTF { r@susistarget#1 }
   {
    \hyperlink{#1}{\textcolor{blue}{#2}}
   }
   {
    \hyperlink{#1}{\textcolor{red}{#2}}
   }
 }
\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}

\myHyperlink{Hello}\clearpage

\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}

enter image description here

  • 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
0

The desired functionality can be accomplished by combining these answers:

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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.