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}}

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}
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.