Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
Welcome to TeX.sx! You didn't provide your code for \namedwikipedialink, please try to provide it in a minimal working example (MWE) (starting with \documentclass{...} and ending with \end{document}) so we reproduce your situation and find out what the issue is when we see compilable code. –  Silex Nov 29 '12 at 22:00
    
Related/duplicate: Nested macros and hyperlink problem –  Qrrbrbirlbel Nov 29 '12 at 22:20

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
    
Oh, just a redirect page … :-) –  Speravir Nov 29 '12 at 23:57

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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