9

I have the following code:

\usepackage{hyperref}
\newcommand{\version}{0.1.2}
\href{https://example.com}{version \version{}}

It produces a URL like:

version 0.1.2

If I try this instead:

\href{https://example.com/\version{}}{version}

I don't get a functional URL.

Is it possible to do something like this to construct a URL with variables in it?

jamtur01
  • 281
  • 2
    It looks like what's happening is that a link is being generated to https://example.com/0.1.2{} which is an invalid url. If I remove the {} at the end of your \version{}, then I indeed get a functional link pointing to https://example.com/0.1.2. That may solve your problem. (I found this out by putting \tracingall at the top of the file and grepping for the url. I'm leaving this as a comment rather than an answer, because I don't know enough about href or about macro expansion to explain what's going on.) – ShreevatsaR Sep 25 '16 at 06:20

1 Answers1

11

Just remove the trailing {} that forms part of the URL:

\href{https://example.com/\version}{version}

Appending {} to a macro is only needed if you which to set a regular space between a macro and a following word, as in

\newcommand{\arnold}{Arnold Schwarzenegger}

\arnold{} is a \ldots

See Space after LaTeX commands.

Werner
  • 603,163
  • But why doesn't it work with the {}? Could you explain? – ShreevatsaR Sep 25 '16 at 06:38
  • (Incidentally, the top-voted answer at the question you linked to begins with "If you create a macro without arguments you should always invoke it with an empty statement after it: \arnold{}" – following that advice may have been what led to this question.) – ShreevatsaR Sep 25 '16 at 06:39
  • @ShreevatsaR: It does work. Under both Adobe and Sumatra I get a functional URL where {} is replaced with %7B%7D by the browser (Chrome). – Werner Sep 25 '16 at 06:49
  • Well yes (though it doesn't work in OS X Preview, and of course it's a different URL from what the OP wanted so it's arguable what "work" means here), but anyway the question I meant is: why does the url become https://example.com/0.1.2{} rather than https://example.com/0.1.2 with \version{} expanding into 0.1.2 sooner. In other words, what should the user's mental model be, to predict this and not make this mistake? – ShreevatsaR Sep 25 '16 at 06:58
  • 2
    @ShreevatsaR why would you expect the {} to go? they are unconnected with the \version macro, which has no arguments, it is the same as \href{abc{}xyz}{text} the {} would be in the URL. \version expands to 0.1.2 so \version{} expands to0.1.2{} – David Carlisle Sep 25 '16 at 07:03
  • @DavidCarlisle The OP seems to have expected it to go :-) I personally wouldn't expect it (or at least be sure of it), which is why I turned on tracing to make sure. I think what happens is that people see recipes like at the linked answer and pick up rules-of-thumb like "always put {} after your macro ‘invocation’" without understanding what's going on and when they apply, so it may be helpful to provide that understanding instead of more rules-of-thumb. Anyway, guess this is something for the OP to ask. – ShreevatsaR Sep 25 '16 at 07:11
  • @DavidCarlisle BTW, your comment is the sort of explanation I would have wished an answer to contain. (If you see the comment on the question, I left the comment before this answer was posted, and left it as a comment because I didn't consider "just remove your {}" to be a sufficient answer and one which would dispel the OP's confusion. Oh well.) – ShreevatsaR Sep 25 '16 at 08:22
  • Thanks to all and this comment thread was very useful. I did indeed assume \version{} with the {} was mandatory. – jamtur01 Sep 25 '16 at 16:05
  • @ShreevatsaR: URLs may contain many characters which or otherwise considered special in (La)TeX, like _ or #, say. As such, care is taken to allow for this, which means that using content that would typically set nothing if used in a different context - an empty group {} - may actually end up in the URL. – Werner Sep 25 '16 at 18:13