I have a bunch of links which are individually stored in text files. I would like to create a LaTeX document in which those files are accessed using relative paths and the link included into the final PDF. The reason is that later in the future I might want to change the links in the files and the generated PDF should update accordingly.

This is what I have tried:

\usepackage{verbatim}


but I get the error:

TeX capacity exceeded, sorry [input stack size=5000].

Here is a minimum working example:

Create File: 1.txt

1.2. Close file

Latex Code without pulling from text file

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

\usepackage{verbatim}% for the input

\begin{document}%

\end{document}%


works great !

now with pulling from text file

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

\usepackage{verbatim}% for the input

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

\end{document}%


I get the errors:

line 10: Argument of @include has an extra }. \href{\include{1.txt}}

line 10: Paragraph ended before @include was complete.

\href{\include{1.txt}} line 10: Paragraph ended before \href@ was

complete. \href{\include{1.txt}} line 10: Extra }, or forgotten

\endgroup. \href{\include{1.txt}} line 10: Overfull \hbox (15.0pt too

wide) in paragraph

EDIT:

based on @Ulrike Fischer, I tried

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

\usepackage{verbatim}% for the input

\begin{document}%

\CatchFileDef\myurl{1.txt}{}
\href{\myurl}{\includegraphics[width=\linewidth]{example-image}}

\end{document}%


but I got:

Package catchfile Error: File 1.txt' not found. \CatchFileDef\myurl{1.txt}{} Suppressing link with empty target Overfull \hbox (15.0pt too wide) in paragraph

EDIT 2:

Also tried to create a new command, but did not work: http://www.emerson.emory.edu/services/latex/latex_19.html

\newcommand\urlFromFile[1]{\CatchFileDef{#1}{}}


EDIT 3:

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

\usepackage{verbatim}% for the input

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

• what is the format of the link in the external file? Feb 8, 2020 at 21:24
• @UlrikeFischer Its just a hyperlink to a website as a text format Feb 9, 2020 at 14:34
• \href{}{} expects two inputs. \url{} works with one. Both are from the package hyperref. Can you please provide a MWE with the contents of both files? Feb 9, 2020 at 14:48
• @letsfetz I added a MWE. Thanks Feb 9, 2020 at 15:15
• @UlrikeFischer I added a MWE Feb 9, 2020 at 15:15

You can use catchfile to load the url:

\begin{filecontents*}{1.txt}
\end{filecontents*}

\documentclass{report}
\usepackage{hyperref,catchfile}
\CatchFileDef\myurl{1.txt}{}
\begin{document}
\expandafter\url\expandafter{\myurl}
\end{document}


If the url contains special chars like % or _ or # you can use the last argument of the command to setup the catcodes.

An extended example with some special chars and a command:

\begin{filecontents*}{1.txt}
\end{filecontents*}

\documentclass{report}
\usepackage{hyperref,catchfile}
\newcommand\urlFromFile[1]{%
\CatchFileDef\myurl{#1}{\catcode\#=12\catcode\%=12}%
\expandafter\url\expandafter{\myurl}}
\begin{document}
\urlFromFile{1.txt}
\end{document}

• Thank you very much ? Three questions: 1. I have a lot of theses files (1.txt,2.txt,3.txt,4.txt etc.), how can I include them with your method ? 2. My urls look like that "youtu.be/r54oAmvqt5I?t=368", how can I include the special characters ? 3. Do I put the \myurl command into the \href where the link would normaly go? Feb 9, 2020 at 15:28
• please see edit. Thanks a lot! :) Feb 9, 2020 at 15:34
• I was curious, I tested a simple \include instead of catchfile and it did not work. So, catchfile is crucial here. Feb 9, 2020 at 15:34
• @OlegLobachev Can you have a look at my second edit, maybe you know how to change the command so that it works ? Feb 9, 2020 at 15:42
• @OlegLobachev \include is the completly wrong command. This is not a simple command. Beside others it issues a \newpage and write an aux-file. Feb 9, 2020 at 15:49