0

I would like to hyperlink any subsection to a relevant Wikipedia page while clicking on it. let's say that the name of the subsection is:

" Row and column vectors (wiki) "

when clicking on (wiki) lead to https://en.wikipedia.org/wiki/Row_and_column_vectors or even https://www.google.com/search?q=Row+and+column+vectors will work.

just wondering if someone knows a nice way to do it automatically, instead of hyperlinking each subsection one and one manually?

1
  • I have edited my answer a bit to make the result closer to your original question.
    – Marijn
    Aug 22, 2022 at 13:04

1 Answer 1

2

You can create a new command to add the link after the argument. In the MWE below I did not replace the spaces by underscores. Officially a space is not allowed in a URL, but most likely your pdf viewer and/or browser will automatically convert the spaces to the url encoding %20. Wikipedia then replaces %20 with _.

To remove the (wiki) part from the Table of Contents and PDF bookmarks you can use the optional argument of \subsection, i.e., \subsection[title in the toc]{full title with hyperlink}.

MWE:

\documentclass{article}
\usepackage{hyperref}
\newcommand{\wikisubsection}[1]{\subsection[#1]{#1 \href{https://en.wikipedia.org/wiki/#1}{\textbf{(wiki)}}}}
\begin{document}
\tableofcontents
\wikisubsection{Row and column vectors}
\end{document}

Result (tooltip shows the link target):

enter image description here

3
  • Great answer! I didn't know about the automatic encoding conversion. By the way, to redefine section headings globally, one could add \let\oldsubsection\subsection \renewcommand\subsection[1]{\oldsubsection{\wikititle{#1}}}
    – Quae
    Aug 22, 2022 at 8:54
  • @Quae I chose this solution because it's the simplest. However, since it violates the official specification for urls it may not work universally across viewers/browsers/OSes, especially older ones.
    – Marijn
    Aug 22, 2022 at 9:26
  • Future visitors: note that Quae's comment applies to an earlier revision of this answer.
    – Marijn
    Aug 22, 2022 at 13:12

You must log in to answer this question.

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