0

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.

I already got quite a good answer in this question How to add externally stored link text into a hyperlink?

...but I still have a problem:

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


\usepackage{hyperref,catchfile}% for the links
\usepackage{verbatim}% for the input

%To generate a text file for demonstration
\begin{filecontents*}{1.txt}
https://www.youtube.com/
\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}}

james
  • 325

1 Answers1

1
\documentclass{article}%
\usepackage{graphicx}


\usepackage{hyperref,catchfile}% for the links
\usepackage{verbatim}% for the input

%To generate a text file for demonstration
\begin{filecontents*}{1.txt}
https://www.youtube.com/
\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}
Ulrike Fischer
  • 327,261
  • Thanks a lot ! I noticed that there is an issue with the link. The link looks like "https://www.youtube.com%20/" instead of "https://www.youtube.com". Do you have any ideas how to solve that ? – james Feb 09 '20 at 20:19
  • I see no problem when I compile the example. – Ulrike Fischer Feb 09 '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 https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20 – james Feb 10 '20 at 19:22
  • or a space https://www.fullhost.com/blog/what-does-20-mean-in-a-web-address/ – james Feb 10 '20 at 19:26
  • I added \endlinechar=-1 to the setup. – Ulrike Fischer Feb 10 '20 at 23:04
  • Awesome !! Works now! Thank you so much !! Really appreciate your help ! – james Feb 12 '20 at 18:51