# How to write a latex macro to reference a particular time in a set of youtube videos?

I have a set of educational youtube videos that I want to reference in a document, with links to a particular time in the video. In Python, the code I want would do something like this:

links = {
}

def youTubeRef(text:str, ref:str h:int, min:int, sec:int):
return '<a href="{link}">{text}</a>'  # though in latex it would just be a href



Then, in a latex document, I would like to use this like so:

Here is \youTubeRef{my text}{first}{0}{12}{13}


And the final rendered document would show:

Here is my text

• Do your urls contain characters which have special meanings in LaTeX, like # or % or & or \$ or _ or ^? Mar 19 at 9:48
• No, ID videos on YouTube doesn't contains any of theses characters except _ (e.g. in the URL named "second" in the OP code). See for example: webapps.stackexchange.com/questions/54443/…. Mar 19 at 13:14

In your command you are using mandatory arguments to your command. I would advise against it, since sometimes it might be the case that you don't want to specify a particular time. For this, using optional arguments is a better choice.

\documentclass{article}
\usepackage{hyperref}
\NewDocumentCommand{ \ytref }{ m m O{00} O{00} O{00} }{%
\href{#2=#3h#4m#5s}{%
#1%
}%
}

\begin{document}
\ytref{This}{\first}[0][12][13] video will start from 12
minutes 13 seconds.

\ytref{This}{\first} video will start from the beginning.
\end{document}


Also remember that variables can't be used on the fly like you have done it in your code. You will have to define them with \def command and then you can use it like a variable (and there too, they should start with a \).

• Since you are using consecutive bracket options, you should explain the meaning of \ytref{This}{\first}[12][13] and \ytref{This}{\first}[13]. By the way, this gives a very good example of a justified use of bracket options. Mar 19 at 9:03
• @projetmbc Thanks for the compliment :). For OP: What they mean is, the sequence of these brackets has a specific meaning. The former from the two commands given by them means the video will start at 12h 13m and 00s and the latter will start at 13h. So the first bracket will always represent hours. Note that you can't use blank brackets. You will have to use [0]. Mar 19 at 9:41
• Very helpful, thank you Mar 20 at 19:13

Code:

\documentclass[11pt]{article}
\usepackage{hyperref}

\begin{document}
\end{document}


With the assumption that hours are quite rare in your links, the following makes them an optional argument defaulting to 0. Else this is pretty similar to the other answers, I've opted for a two-step solution with two document-level macros, one to add new links, the other to use them.

\documentclass{article}

\usepackage{hyperref}

\ExplSyntaxOn
\msg_new:nnn { cirian } { unknown-link }
{ The~ link~ named~ "#1"~ is~ unknown. }
{ \prop_put:Nnn \l_cirian_links_prop {#1} {#2} }
\NewDocumentCommand \youtuberef { m m O{0} m m }
{
{ \exp_args:No \href { \l_cirian_link_tl  &=h#3m#4s#5 } {#1} }
{ \msg_error:nnn { cirian } { unknown-link } {#2} }
}
\ExplSyntaxOff

\begin{document}

And the really long video is only worth it \youtuberef{after an hour}{second}[1]{0}{0}.
\end{document}