8

I want to format Wikipedia entries in my document in a standard way:

\href{http://en.wikipedia.org/wiki/Foo}{Foo}

So I thought about using \newcommand*:

\documentclass{article}
\usepackage{ifthen}
\usepackage{hyperref}
\newcommand*{\WWW}[2]{\href{http://en.wikipedia.org/wiki/#2}%
  \ifthenelse{\equal{#1}{}}{{#2}}{{#1}}%
}
\begin{document}
\WWW{Foo}
\end{document}

This command should set the text Foo linked to the Wikipedia article and one can enter a special title. However this oesn't work. Can you see what is wrong here?

3 Answers 3

8

You have two problems. The first is that you forgot to specify that \WWW takes an optional argument, for which you'd need [2][] rather than just [2] (with the second [] specifying the empty default); the second problem is that you need to group \ifthenelse, but not its arguments. (I think the problem is that when \ifthenelse expands, it doesn't introduce a group, and so the wrong token is being grabbed.) Also, on the "just my opinion" side of things, if this is just for Wikipedia links, I'd probably call it \wiki instead. Anyway, putting that together yields

\documentclass{article}
\usepackage{ifthen}
\usepackage{hyperref}
\newcommand*{\wiki}[2][]{\href{http://en.wikipedia.org/wiki/#2}%
                              {\ifthenelse{\equal{#1}{}}{#2}{#1}}}
\begin{document}
  The Wikipedia article about \wiki[the metasyntactic variable foobar]{Foobar}
is shorter than the one about \wiki{Einstein}.
\end{document}

(Also, note that this doesn't necessarily play well with Wikipedia's underscore-for-space convention, though that's certainly fixable.)


Edit 1: Actually, if you use the xstring package, it's very easy to take care of underscores automatically. This revised command will automatically replace all spaces in its argument with underscores, so you don't have to. If you still want a different title, you can provide it. So

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

\makeatletter
\newcommand*{\wiki}[2][]{%
  \StrSubstitute{#2}{ }{_}[\wiki@article]%
  \href{http://en.wikipedia.org/wiki/\wiki@article}%
       {\ifthenelse{\equal{#1}{}}{#2}{#1}}}
\makeatother

\begin{document}
  The Wikipedia article about \wiki[the metasyntactic variable foobar]{Foobar}
is shorter than the one about \wiki{Albert Einstein} or the one about \wiki[the
creator of \TeX]{Donald Knuth}.
\end{document}

renders as

The Wikipedia article about the metasyntactic variable foobar is shorter than the one about Albert Einstein or the one about the creator of TeX.

4
  • I wanted to use a second argument to work around the underscore convention. So I can write \WWW[Foo Bar]{Foo_Bar}. Besides underscore one would have to work around several special characters (like ' in Foo's Law).
    – qbi
    Oct 15, 2010 at 16:46
  • 1
    @qbi: You can actually take care of underscores automatically with the xstring package; see my edit. And most other special characters aren't problematic: if you specify e.g. \wiki{Boyle's law}, then you'll get linked to en.wikipedia.org/wiki/Boyle's_law, which is a perfectly valid URL. Oct 15, 2010 at 17:58
  • xstring seems to do exactly the job I want it to do. Thanks a lot for your answer.
    – qbi
    Oct 15, 2010 at 18:11
  • 2
    Spaces aren’t a problem, Wikipedia will do the redirect automatically; no need to escape them. Proof: en.wikipedia.org/wiki/Boyle's law. Oct 17, 2010 at 11:04
2

This works:

\newcommand{\WWW}[1]{%
    \href{http://en.wikipedia.org/wiki/#1}{#1}%
}

But of course, it is not sufficient if you want to be able to specify a title... how about a second, separate command for when you want to do that?

2
  • He wants an optional text. I.e. \WWW{Foo} will display "Foo" and link to the wikipedia article on Foo, while \WWW[Foo]{Bar} will display "Foo" while linking to the Wiki entry on Bar. Oct 15, 2010 at 15:53
  • Yep, I understood it as soon as I clicked, and then edited my answer ;) Oct 15, 2010 at 15:59
1

Here's a slightly different approach.

\usepackage{hyperref}
\makeatletter
\newcommand*\wiki{%
        \@ifstar{\wiki@iii\wiki@ii}%
                {\wiki@iii\wiki@i}%
}
\newcommand*\wiki@iii[1]{%
        \begingroup
        \catcode`\_12
        \catcode`\%12
        #1%
}
\newcommand*\wiki@i[1]{\href{http://en.wikipedia.org/wiki/#1}{#1}\endgroup}
\newcommand*\wiki@ii[2]{\href{http://en.wikipedia.org/wiki/#2}{#1}\endgroup}
\makeatletter
\begin{document}
\wiki*{Foo's Bar}{Foo's_Bar}
\wiki{Foo}
\wiki*{Foo (Bar)}{Foo_%28Bar%29}

Here, you'd use \wiki* if you want to give it two arguments. An even better approach would be to read it character by character and perform replacement as needed. It'd take a bit more work though.

0

You must log in to answer this question.

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