I would like to incorporate a link into a picture using \href. The links are stored in text files. The .tex file should be able to read form those text files (i.e. 1.text) and generate that clickable image.

...but I still have a problem:

% !TeX program = lualatex
\documentclass{article}%
\usepackage{graphicx}

\usepackage{verbatim}% for the input

%To generate a text file for demonstration
\begin{filecontents*}{1.txt}
\end{filecontents*}

\usepackage{hyperref,catchfile}
\newcommand\urlFromFile[1]{%
\CatchFileDef\myurl{#1}{\catcode\#=12\catcode\%=12}%
\expandafter\url\expandafter{\myurl}}

\begin{document}
\href{\urlFromFile{1.txt}}{\includegraphics[width=\linewidth]{example-image}}
\end{document}
%


I get:

line 16: TeX capacity exceeded, sorry [parameter stack size=10000]. \href{\urlFromFile{1.txt}}

\documentclass{article}%
\usepackage{graphicx}

\usepackage{verbatim}% for the input

%To generate a text file for demonstration
\begin{filecontents*}{1.txt}
\end{filecontents*}

\usepackage{hyperref,catchfile}
\newcommand\hrefFromFile[1]{%
\CatchFileDef\myurl{#1}{\catcode\#=12\catcode\%=12\endlinechar=-1}%
\expandafter\href\expandafter{\myurl}}

\begin{document}
\hrefFromFile{1.txt}{\includegraphics[width=\linewidth]{example-image}}
\end{document}

• Thanks a lot ! I noticed that there is an issue with the link. The link looks like "youtube.com%20" instead of "youtube.com". Do you have any ideas how to solve that ? – james Feb 9 '20 at 20:19
• I see no problem when I compile the example. – Ulrike Fischer Feb 9 '20 at 20:46
• Do you get redirected to youtube when you click on the image ? I don´t. – james Feb 10 '20 at 19:12
• I think the problem is that catchfile somwhoe adds a new line to the url and this then gets converted into %20, see stackoverflow.com/questions/1634271/… – james Feb 10 '20 at 19:22
• – james Feb 10 '20 at 19:26