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?


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

\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 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
  • 1
    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

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

\StrSubstitute{\animal}{ }{}[\mylabel]
| improve this answer | |

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.