UP | HOME

Adding a list of links to a LaTeX document

(dark mode)

I use the hyperref package a lot and it creates beautiful document. But I want to make sure that those links are still usable when people print the document.


PDF (drucken)

I worked on making 10 ways GNU Guile is 10x better easier to print. But that article uses lots of links and a printout from the standard PDF export over LaTeX loses the links. They become just colored letters.

In contrast, plain text export shows a list of links at the end. That’s what I wanted for the nicer to read PDF, too.

Thanks to a tip from J$ (mastodon.nl/@js) I now have a clean solution:

\documentclass[a5paper]{scrartcl}
\usepackage{hyperref}

% re-defining href to add contents lines
\usepackage{tocloft}
\newcommand{\listlinksname}{List of Links}
\newlistof{links}{lnks}{\listlinksname}
\let\oldhref\href
\renewcommand{\href}[2]{\oldhref{#1}{#2}\addcontentsline{lnks}{figure}{#2: \url{#1}}}
\usepackage{xurl}\urlstyle{same}\renewcommand\cftsecaftersnum{\newline}
% end re-defining

\begin{document}
Something \href{http://interesting.example.com}{interesting}!

% inserting the list of links
\addcontentsline{toc}{section}{\listlinksname}\listoflinks
% end inserting

\end{document}

I use it via org-mode on this page:

# Add a list of links
#+latex_header: \usepackage{tocloft}
#+latex_header: \newcommand{\listlinksname}{List of Links}
#+latex_header: \newlistof{links}{lnks}{\listlinksname}
#+latex_header: \let\oldhref\href
#+latex_header: \renewcommand{\href}[2]{\oldhref{#1}{#2}\addcontentsline{lnks}{figure}{#2: \url{#1}}}
#+latex_header: \usepackage{xurl}\urlstyle{same}\renewcommand\cftsecaftersnum{\newline}

#+latex: \addcontentsline{toc}{section}{\listlinksname}\listoflinks

ArneBab 2023-02-12 So 00:00 - Impressum - GPLv3 or later (code), cc by-sa (rest)