从外部文本文件读取链接并包含在超链接图像中

从外部文本文件读取链接并包含在超链接图像中

我想使用 \href 将链接合并到图片中。链接存储在文本文件中。.tex 文件应该能够读取这些文本文件(即 1.text)并生成可点击的图像。

我已经在这个问题上得到了很好的答案如何将外部存储的链接文本添加到超链接中?

...但我仍然有一个问题:

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

我得到:

第 16 行:超出 TeX 容量,抱歉 [参数堆栈大小=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}

相关内容