9

I'd like to create a LaTeX macro that will auto-create a link to a Wikipedia page for me.

 \usepackage{xstring}
 \usepackage{hyperref}

 % replaces the whitespace with '%20'
 \newcommand{\urlwhitespacereplace}[1]{\StrSubstitute{#1}{ }{\%20}}

 % takes an article name, replaces the whitespace, and concatenates to form the full URL 
 \newcommand{\wikipedialink}[1]{http://en.wikipedia.org/wiki/\urlwhitespacereplace{#1}}

 % takes an article name and gives me a link called 'Wikipedia'
 \newcommand{\anonymouswikipedialink}[1]{\href{\wikipedialink{#1}}{Wikipedia}}

Then I try calling it like this...

 \urlwhitespacereplace{Charlie Bucket} %expected 'Charlie%20Bucket'

 \wikipedialink{Charlie Bucket} %expected 'http://en.wikipedia.org/wiki/Charlie%20Bucket'

 \namedwikipedialink{Charlie Bucket} %expected 'Wikipedia' (hyperlink)

The first two work, returning exactly what you'd expect. However, when I use the third, I get...

! TeX capacity exceeded, sorry [input stack size=5000].
\Hy@href {->\hyper@normalise 
                         \href@ {
l.11 \namedwikipedialink{Charlie Bucket}

Could this have something to do with the the escaped % moving between the macros? Thanks for reading.

2

2 Answers 2

9

Notes:

  • Wikipedia escapes spaces with an underscore (_)
  • Links with space do work, too.
  • Nested macros and hyperlink problem, Martin Scharrer:

    The correct syntax is \hyperlink{<name>}{<text>} where <name> must be a valid, expandable label name. It can not contain any non-expandable commands. […] The name must instead expand directly to some text only.

Code

\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}

%\newcommand{\urlwhitespacereplace}[1]{\StrSubstitute{#1}{ }{\%20}[\wpLink]}
\newcommand{\urlwhitespacereplace}[1]{\StrSubstitute{#1}{ }{_}[\wpLink]}
\newcommand{\wikipedialink}[1]{http://en.wikipedia.org/wiki/#1}% needs \wpLink now
\newcommand{\anonymouswikipedialink}[1]{\urlwhitespacereplace{#1}\href{\wikipedialink{\wpLink}}{Wikipedia}}
\newcommand{\namedwikipedialink}[1]{\urlwhitespacereplace{#1}\href{\wikipedialink{\wpLink}}{#1}}

\begin{document}
\urlwhitespacereplace{Charlie Bucket} \par % no output
\wikipedialink{Charlie Bucket} \par % no substituting
\namedwikipedialink{Charlie Bucket} \par
\anonymouswikipedialink{Charlie Bucket}
\end{document}
1
  • Oh, just a redirect page … :-)
    – Speravir
    Nov 29, 2012 at 23:57
4

My texlinks.sty in the morehype bundle provides macros for links to Wikipedia articles, see Section 8 of texlinks.pdf, e.g., just \Wikiref{⟨lemma⟩} for the "current" language. The documentation proceeds from backbones to smarter macros, which may be difficult to understand quickly, sorry.

\documentclass{minimal} 
\usepackage{hyperref,texlinks} 
\begin{document} 
    Look up \wikiref{Charlie Bucket}{Wikipedia}
        for \Wikiref{Charlie Bucket}. 
\end{document}

That's "fwiw", haven't studied details of the question. The texlinks.sty macros work without xstring, even with blanks in lemmas, I do not know exactly why, just seeing this here. Only referring to sections of Wikipedia articles needs "escaping" of blanks, umlauts, commas, ... I guess it's the Wikimedia (MediaWiki) engine that converts spaces into underscores etc.

3
  • 1
    Thanks to @percusse for help with editing and suggesting that I provide a MWE.
    – Uwe Lück
    Dec 10, 2012 at 20:19
  • No problem. It's my pleasure.
    – percusse
    Dec 10, 2012 at 20:20
  • Welcome to TeX.sx! It’s great to have another TeXpert here.
    – Speravir
    Dec 11, 2012 at 3:55

You must log in to answer this question.

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