1

I would like to define a command that takes an input, passes through a list to generate another variable and then is fed to \href to give a link. As in the following example:

\documentclass[11pt, a4paper]{article}

\usepackage{hyperref}
\usepackage{forarray}


\DefineArrayVar{Names}{@}
{,}{aaa,bbb,ccc}
{,}{Einstein,Newton,Leibnitz}

\newcommand{\links}[1]
{\csname Names@#1\endcsname\
}

\newcommand*{\newhref}[1]{\href{http://en.wikipedia.org/wiki/\links{#1}}{\links{#1}}}

\begin{document}

Here is a page about \newhref{aaa}.

\end{document}

However the link I obtain is highlighted but doesn't link to anything. What is going wrong? Thank you!

  • 1
    show a complete example, that makes it much easier to test your code. – Ulrike Fischer Oct 28 '19 at 9:09
  • Ok, thanks. I included the \documentclass and end{document} commands. In the exemple above the word Einstein appears linked, but when clicking on it nothing happens. – Francesco Oct 28 '19 at 9:45
2

The problem is with the \ at the end of the line.

\newcommand{\links}[1]{\csname Names@#1\endcsname}

would solve the issue.

I'd use a friendlier way, though.

\documentclass[11pt, a4paper]{article}

\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn

% define the array
\NewDocumentCommand{\definearray}{mm}
 {% #1 = array name, #2 = items
  \prop_new:c { g_francesco_array_#1_prop }
  \prop_gset_from_keyval:cn { g_francesco_array_#1_prop } { #2 }
 }
% generic command for retrieving a value
\NewExpandableDocumentCommand{\getarrayvalue}{mm}
 {% #1 = array name, #2 = array item
  \prop_item:cn { g_francesco_array_#1_prop } { #2 }
 }

\ExplSyntaxOff

\definearray{Names}{
  aaa=Einstein,
  bbb=Newton,
  ccc=Leibnitz,
}

\newcommand*{\newhref}[1]{%
  \href{http://en.wikipedia.org/wiki/\getarrayvalue{Names}{#1}}{\getarrayvalue{Names}{#1}}%
}

\begin{document}

Here is a page about \newhref{aaa}.

\end{document}
| improve this answer | |
  • Thank you very much! It solved my problem! – Francesco Oct 28 '19 at 10:33

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.