2

Posting a question for the first time so please let me know if I missed anything.

I am working in a LaTeX document and here is what I want to achieve using the hyper ref package:

Have a URL that is linked to my website and has today's date embedded for referral tracking. For example, the URL should look like:

https://example.com?ref=text-2ndOct2020

Basically, adding "ref=text-date" allows me to track where the link was clicked from to get to my website. I want to use this for manual A/B testing of the content, the date in the referral helps me determine the version that did better.

Here is a MWE:

\documentclass[letter,oneside,10pt]{article}
\usepackage{hyperref}
\usepackage[ddmmyyyy]{datetime}

\begin{document}

\href{https://example.com?ref=text-date}{example.com}

\end{document}

This works, but what I'd ideally like to do is:

\href{https://example.com?ref=text-\today}{example.com}

so that the date can auto-update when I compile it for that particular day, the key being using \today to add today's date automatically to the link.

When I do use \today in \href in this particular solution, I get the following error:

Undefined control sequence. }
line 107: Undefined control sequence. }
line 107: Undefined control sequence. }
.
.
.
line 107: Undefined control sequence. }

I am using TeXStudio with a 2019 TeX-Live distribution, and compelling my document using XeLaTeX.

I am open to any solution but using \today is what came to my mind, I'm sure there are better ways to do this (if the \hyperref package supports it). Thanks!

1
  • 1
    Welcome to TeX.SE. This is an excellent first question. Usually, hyperref should be one of the last packages loaded (with very few exceptions). But I doubt that is what is causing your problem. – Teepeemm Oct 2 '20 at 15:10
1

Here's what I tried:

\documentclass[letter,oneside,10pt]{article}
\usepackage{hyperref}
\usepackage[ddmmyyyy]{datetime}

\begin{document}

\href{https://example.com?ref=text-\today}{example.com}

\end{document}

Running this (I have a newer install), I don't get the errors that you're getting, but the URL comes out as:

https://example.com/?ref=text-%5Cprotect%20%5C@day%20=%5Cday%20%5Crelax%20%5C@month%20=%5Cmonth%20%5Crelax%20%5C@year%20=%5Cyear%20%5Crelax%2000/00/0

which is obviously not what we want.

The problem here is that the datetime package is doing all kinds of generalized things and expecting to produce text and not a token sequence.

So instead, let's go for a specialized but simplified version of what datetime does. Drop the \usepackage and instead do:

\renewcommand{\today}{\ifnum\day<10 0\fi\the\day \ifnum\month<10 0\fi\the\month\the\year}

which gives the desired URL.

2
  • 1
    Thank you so much. I tried the MWE in Overleaf & got the garbled URL you mentioned above. I then tried your solution in my local env and it wasn't working, even though it worked in Overleaf. So I updated my TeXStudio from 2.12.6 to 3.0.1 and to Tex-Live 2020. It still didn't work, so I used your \renewcommand and changed it to: \newcommand{\trackdate}{\ifnum\day<10 0\fi\the\day \ifnum\month<10 0\fi\the\month\the\year} and then also changed \today to \trackdate when using \href. Thanks so much for your perfect solution! I wouldn't have been able to figure it out. – Siddhant Khera Oct 2 '20 at 16:31
  • Wanted to add further to this already wonderful solution by Don Hosek. The current date format gives "DDMMYYY" –– what I was trying to achieve. I further amended the command to match "YYYY-MM-DD", so just leaving this solution out there for anyone else wanting to do a similar thing in the future. Here is the updated command: \newcommand{\trackdate}{\the\year-% \ifnum\month<10 0\fi\the\month-% \ifnum\day<10 0\fi\the\day} – Siddhant Khera Oct 2 '20 at 17:59

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.