3

I'm writing a macro that takes a string and it returns an href based on that string. The string could have 150 different values. My question is how to write this in an efficient way. Using pdflatex.

Here's what I've got right now, shortened to two conditions for explanation:

\documentclass{article}
\usepackage{hyperref}
\newcommand{\myref}[1]{%
   \ifnum\pdfstrcmp{#1}{aaa}=0%
      \href{example.com/aaa}{my aaa link}
   \fi
   \ifnum\pdfstrcmp{#1}{bbb}=0%
      \href{example.com/bbb}{my bbb link}
   \fi
}
\begin{document}
Here is \myref{aaa}.
\end{document}

But of course once I get a success (#1=aaa, say), I'm still testing the other 149 conditions for no reason. How to code this efficiently?

2
  • Are all of the outcomes of the form \href{example.com/<string>}{my <string> link}?
    – Joseph Wright
    Commented May 28, 2019 at 13:13
  • Ah, I simplified too much. It will actually take two args. first arg goes in the link address, the second provides the link text. Like \href{example.com/aaa}{The Title of the AAA Document} (link text will have completely different text on each invocation of the macro.
    – Tim A
    Commented May 28, 2019 at 13:16

3 Answers 3

5

I would e.g. create command names:

\documentclass{article}
\usepackage{hyperref}
\makeatletter
\@namedef{aaa@link}{\href{example.com/aaa}{my aaa link}}
\@namedef{bbb@link}{\href{example.com/bbb}{my bbb link}}

\newcommand{\myref}[1]{%
 \csname #1@link\endcsname}

\begin{document}
Here is \myref{aaa}.
\end{document}
2

let biber do the searching for you:

assuming there is a file test.bib with

@online{tex,
  note={my aaa text},
  url={tex.stackexchange.com}
}

@online{google,
  note={my bbb text},
  url={www.google.com}
}

then

\documentclass{article}
\usepackage{biblatex}

\addbibresource{test.bib}

\DeclareFieldFormat{url}{\href{#1}{\printfield{note}}}
\DeclareCiteCommand{\myref}{}{\usebibmacro{url}}{}{}

\usepackage{hyperref}

\begin{document}
Here is \myref{tex}

\myref{google}

\end{document}
2

You can use xparse:

\pdfcompresslevel=0
\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn

\NewDocumentCommand{\newref}{mmm}
 {% #1 = key, #2 = URL, #3 = description, #4 = options for \href
  \prop_gput:Nnx \g_tima_sites_prop { #1 @ url } { \tl_to_str:n { #2 } }
  \prop_gput:Nnn \g_tima_sites_prop { #1 @ desc } { #3 }
 }

\NewDocumentCommand{\myref}{m}
 {% #1 = key
  \tima_href:xx
   { \prop_item:Nn \g_tima_sites_prop {#1 @ url } }
   { \prop_item:Nn \g_tima_sites_prop {#1 @ desc } }
 }

\prop_new:N \g_tima_sites_prop
\cs_new_protected:Nn \tima_href:nn { \href{#1}{#2} }
\cs_generate_variant:Nn \tima_href:nn { xx }

\ExplSyntaxOff

\newref{texworks}{http://profs.scienze.univr.it/~gregorio/introtexworks}{\TeX works intro}
\newref{arara}{http://profs.scienze.univr.it/~gregorio/introarara}{Arara intro}
\newref{tex.sx}{https://tex.stackexchange.com}{Nice site}

\begin{document}

\myref{texworks}

\myref{arara}

\myref{tex.sx}

\end{document}

You must log in to answer this question.

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