I have wasted most of my afternoon on this one and can't figure it out. I have prepared an MWE that demonstrates the problem. In my \displaylink
command I invoke \href{raw_url}{anchor}
(from package hyperref
) but I want to build the raw_url with another command, \createurl
, which manipulates its argument with one of the xstring
utilities.
In the MWE there are two versions of \createurl
, one without the command from xstring
and one (\createurlx
) with it. If you comment out the line labeled "fails", and uncomment the line labelled "works", you see that all three commands (\createurl
, \createurlx
and \displaylink
) work fine. If, however, you comment out the "works" like and uncomment the "fails" line, meaning that from \displaylink
you invoke \createurlx
which has the call to \StrGobbleLeft
from xstring
, then things stop working. So it seems to be the nesting of these two commands that's the problem. Why, I have no idea.
BTW my current workaround is that things also work if I put the \StrGobbleLeft
inside \displaylink
, but I don't want to do that because the \createurlx
is a command that's used in other places too. (And of course you realize that these manipulations have been simplified here for the purpose of the MWE---they're somewhat less trivial in the actual document.)
\documentclass{article}
\usepackage{hyperref}
\usepackage{xstring}
\newcommand{\createurl}[1]{
http://www.foobar.com/documents/#1.pdf
}
\newcommand{\createurlx}[1]{
\StrGobbleLeft{#1}{1}[\chopped]
http://www.foobar.com/documents/\chopped.pdf
}
\newcommand{\displaylink}[1]{
% \href{\createurl{#1}}{\mbox{#1}} % works
\href{\createurlx{#1}}{\mbox{#1}} % fails
}
\begin{document}
The generated url looks like this: \createurl{my-stuff}.
The chopped url looks like this: \createurlx{my-stuff}.
The calling code wants to display a hyperlink: \displaylink{my-stuff}.
\end{document}
This fails with:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./mwe2.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 84 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/hyperref.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty))
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/auxhook.sty)
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/kvoptions.sty)
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/pd1enc.def)
(/usr/share/texlive/texmf-dist/tex/latex/latexconfig/hyperref.cfg)
(/usr/share/texlive/texmf-dist/tex/latex/url/url.sty))
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/hpdftex.def
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
(/usr/share/texlive/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/share/texlive/texmf-dist/tex/generic/xstring/xstring.tex)) (./mwe2.aux)
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(./mwe2.out) (./mwe2.out)
! TeX capacity exceeded, sorry [input stack size=5000].
\Hy@href {->\hyper@normalise
\href@ {
l.25 ...o display a hyperlink: \displaylink{stuff}
.
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on mwe2.log.
EDIT: as @egreg correctly surmises in a comment, this is not the actual string manipulation I need. The actual one requires transforming an input like
2010-x03-y10
into 2010x3y10
, that is to say removing the hyphens and any leading zeros---and then adding a constant prefix and suffix. I have had no trouble coding that up as a sequence of xstring
operations and storing the result in a command name (the \chopped
of my MWE), but I have not been able to "hand the obtained string to \href
" (in plain Latex please if possible---but I'm told by @Herbert below that it's not).
xstring
macros are not expandable and cannot be fed into other macros\href
. Can you please be more detailed about what manipulations you really need?