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 = {
    "first": "https://www.youtube.com/watch?v=3anNG7TfHSQ",
    "second": "https://www.youtube.com/watch?v=cfudXO_vzWk"

def youTubeRef(text:str, ref:str h:int, min:int, sec:int):
    link = f"{links[ref]}&t=h{h}m{min}s{sec}"
    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/….
    – quark67
    Mar 19 at 13:14

3 Answers 3


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.

\NewDocumentCommand{ \ytref }{ m m O{00} O{00} O{00} }{%

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

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

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 \).

  • 1
    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.
    – projetmbc
    Mar 19 at 9:03
  • 2
    @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].
    – Niranjan
    Mar 19 at 9:41
  • Very helpful, thank you Mar 20 at 19:13



Here is \youTubeRef{my text}{first}{0}{12}{13}. % link to https://www.youtube.com/watch?v=3anNG7TfHSQ&t=00h12m13s

enter image description here


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.



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


\youtuberef{Cool video}{first}{1}{20}.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .