2

This question already has an answer here:

Im trying to use the hyperref package for creating a link to a website with a dynamic url. I have a script that outputs an id that should be used in the url.

I have been trying to wrap my head around this for hours trying to figure how to achieve this but as a fairly new user of TeX I must admit defeat now.

Here is an example of what I want to do:

\newcommand{\getid}[0]
{
    \input{|"get-id.sh"}
}

\newcommand{\createlink}[0]
{
    \getid \\ % Just for debugging
    \href{https://www.website.com/\getid}{MyLink}
}

This produces the id correctly but on the next line there is nothing remotely what I want, more exactly just "\let\reserved@d=\bgroup\def{\@@par}".

I have also tried \edef\getid{\input{|"get-id.sh"}} with the same result even though I feel like that should evaluate the input before I try to use the variable.

Using \def\getid{MyStaticID} works just fine so the problem only appears when using \input.

Possible workarounds could be to write the value with TeX code to a file and then import it, alternatively pass it to the code using pdftex. I would however prefer to do it in the TeX file as I am trying to do but is it even possible or a limitation somewhere I will have to live with?

Thanks in advance.

Edit

I build the pdf with pdfTeX from the TeX Live system installed with MacTeX. I use latexmk for building and in my .latexmkrc I have the line

$pdflatex = 'pdflatex --shell-escape -synctex=1 -interaction=nonstopmode';

which should enable input from commands (which it does successfully at times, just not with URLs).

My main document looks like this (stripped from irrelevant stuff for ease of reading but with all packages):

\documentclass[a4paper, 11pt]{report}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{pdfsync}
\usepackage{titlesec}
\usepackage{tocloft}

\newcommand{\getid}[0]
{
    \input{|"get-id.sh"}
}

\newcommand{\createlink}[0]
{
    \getid \\ % Just for debugging
    \href{https://www.website.com/\getid}{MyLink}
} 

\newcommand{\wordcount}[0]
{
   \input{|"texcount -sum -1 content/chapters/*"}
}

\begin{document} 

\wordcount{}
\createlink{}

\end{document}

and the get-id.sh is currently just echoing git rev-parse HEAD.

The wordcount and first part of createlink works just fine so there must be something in the url or hyperref packages that breaks (or just doesn't work the way I want/expect it to).

marked as duplicate by Andrew, Stefan Pinnow, Tiuri, Bobyandbob, Seamus Feb 14 '18 at 13:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SE. It would be helpful if you provided a bit more background information about your document setup. E.g., which document class do you employ? And which TeX format do you use -- pdfLaTeX, XeLaTeX, LuaLaTeX, or something else? – Mico Feb 14 '18 at 2:40
  • Related: tex.stackexchange.com/questions/150584/…. Another approach is to have the shell script get-id.sh do something like echo "\def\getid{$(git rev-parse HEAD)}\input file and then use pdflatex $(get-id.sh). Now \href{https://www.website.com/\getid}{MyLink} will work as expected inside file.tex. – Andrew Feb 14 '18 at 6:14
  • Looked at this again. The solution is to use \begingroup\makeatletter\endlinechar=\m@ne\everyeof{\noexpand} \edef\x{\endgroup\def\noexpand\gitid{\@@input|"./get-id.sh" }}\x . This comes from tex.stackexchange.com/questions/16790/…, which also has an explanation. There are also other solutions using various packages given on this page. – Andrew Feb 14 '18 at 10:28
  • Yes! Thank you, both solutions you referenced works. The first one required some modifications to handle line ending characters from terminal but the second one worked out of the box! The explanations in the second one was really helpful for understanding why my original approach didn't work, live and learn! Would you mind putting it in an answer so I can accept it and close this question? :) – Hampus Feb 14 '18 at 17:08