4

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}
\href{\verbatiminput{./links/1.txt}}

but I get the error:

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

Here is a minimum working example:

Create File: 1.txt

1.1. Input https://www.youtube.com/

1.2. Close file

Latex Code without pulling from text file

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


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

\begin{document}%
\href{https://www.youtube.com}{\includegraphics[width=\linewidth]{example-image}}

\end{document}%

works great !

now with pulling from text file

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


\usepackage{hyperref}% for the links
\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{hyperref,catchfile}% for the links
\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{hyperref,catchfile}% for the links
\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}}

5
  • what is the format of the link in the external file? – Ulrike Fischer Feb 8 '20 at 21:24
  • @UlrikeFischer It`s just a hyperlink to a website as a text format – james Feb 9 '20 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? – letsfetz Feb 9 '20 at 14:48
  • @letsfetz I added a MWE. Thanks – james Feb 9 '20 at 15:15
  • @UlrikeFischer I added a MWE – james Feb 9 '20 at 15:15
4

You can use catchfile to load the url:

\begin{filecontents*}{1.txt}
https://www.youtube.com/
\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}
https://www.youtube_blub#%.com/
\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}
12
  • 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? – james Feb 9 '20 at 15:28
  • please see edit. Thanks a lot! :) – james Feb 9 '20 at 15:34
  • I was curious, I tested a simple \include instead of catchfile and it did not work. So, catchfile is crucial here. – Oleg Lobachev Feb 9 '20 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 ? – james Feb 9 '20 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. – Ulrike Fischer Feb 9 '20 at 15:49

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.