# Problems with StrSubstitute from xstring and hyperref

I am trying to define a label from a string with blanks to use with hyperref. But I get an error. The example below does not compile if the two lines with comments are uncommented. Is there a remedy for this?

\documentclass{article}
\usepackage{xstring}
\usepackage{color}
\usepackage{hyperref}

\def\animal{zebra with stripes}
\def\mylabel{\StrSubstitute{\animal}{ }{}}
\def\thelabel{zebrawithstripes}

\begin{document}
\hypertarget{\thelabel}{\noindent Here is a zebra.} \\
Go to \hyperlink{\thelabel}{\textcolor{blue}{\animal}}.\\

%\hypertarget{\mylabel}{\noindent Here is a zebra.} \\
%Go to \hyperlink{\mylabel}{\textcolor{blue}{\animal}}.\\

\mylabel

\end{document}

• \mylabel my look like "zebrawithstripes" but it actually consists of \let \@xs@assign \@xs@expand@and@assign \@xs@StrSubstitute@zebra with stripes – John Kormylo Mar 3 '18 at 3:52
• Ok. Can I turn it into "zebrawithstripes"? – Rasmus Mar 3 '18 at 3:54
• Possibly, but not using StrSubstitue. Also, you can us \animal as a hypertarget label. You don't need to remove the spaces. – John Kormylo Mar 3 '18 at 3:58
• Ah! That's great news. (I thought an earlier error came from using spaces in the label, but I was apparently wrong.) – Rasmus Mar 3 '18 at 4:01
• If you still want to remove spaces, you might look at tex.stackexchange.com/questions/233085/basics-of-parsing – John Kormylo Mar 3 '18 at 4:25

## 1 Answer

The non-expandable macros of package xstring have usually a tailing optional argument for a command that will contain the result:

\StrSubstitute{\animal}{ }{}[\mylabel]