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

james
  • 325

1 Answers1

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}
Ulrike Fischer
  • 327,261
  • 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 "https://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 09 '20 at 15:28
  • please see edit. Thanks a lot! :) – james Feb 09 '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 09 '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 09 '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 09 '20 at 15:49
  • @james I added an example with a command. – Ulrike Fischer Feb 09 '20 at 16:16
  • Great! thank you so much ! Now, the only issue that I have is that catchfile cannot find the file for some reason. I have stored it in the same directory as the tex document and named it 1.txt. I get the error: Package catchfile Error: File `1.txt' not found. \urlFromFile{1.txt} – james Feb 09 '20 at 16:22
  • well if you named it 1.tex you shouldn't try to use 1.txt. – Ulrike Fischer Feb 09 '20 at 16:23
  • Sorry, my mistake, I have named it 1.txt. Typo, edited the comment just now – james Feb 09 '20 at 16:23
  • Found the error, I indeed misspelled the file. Thank you so much for your kind and patient help !! – james Feb 09 '20 at 16:27
  • I am almost there... please see edit 3, if you still have the time. :) – james Feb 09 '20 at 16:32
  • well the command contains an \url command, so it can't be used in a \href command. You will have to define a similar \hreffromfile command in which you replace \url by \href. – Ulrike Fischer Feb 09 '20 at 16:36