0

I'm trying to make a string manipulation by using xstring package.

Fore example \myparam equals to this: 1.1 Optimal solution of RNASP

But string functions doesn't work. For example \StrLeft{\myparam}{10} gives this error: Missing control sequence inserted. ...RNASP.\relax }}{10}{figure.caption.8}

I set this: \def\myparamDet{\detokenize{myparam}}

\myparamDet equals to this: \hyper@linkstart {link}{\Hy@tocdestname }{\numberline {1.1}{\ignorespaces Optimal solution of RNASP.\relax }}\hyper@-linkend

After detokenize this works: \StrLeft{\myparamDet}{10} and result is \hyper@lin

So I need two things:

  • Getting 1.1 from myparam and setting it to a new variable
  • Removing 1.1 from myparam
3
  • With the \detokenize, you discover what \myparam really equals, and it is not a string, but rather a hyperlink macro with various arguments set. Can you indicate whether the intended use will always be for such hyperlinks? Jun 4, 2018 at 18:53
  • 2
    You really need to provide a small compiling example that highlights the issue. Jun 4, 2018 at 18:57
  • Is result of detokenize not sufficient ? I suppose it is an escaped version of the hyperlink macro. Yes this manipulation will only be used for this type of strings.
    – Mark
    Jun 4, 2018 at 19:06

1 Answer 1

2

With the \detokenize, you discover what \myparam really equals, and it is not a string, but rather a hyperlink macro with various arguments set. So you need to set up helper macros to grab the right argument and subargument from the list.

\documentclass{article}
\usepackage{hyperref}
\makeatletter
\def\myparam{\hyper@linkstart {link}{\Hy@tocdestname }{\numberline {1.1}{\ignorespaces Optimal solution of RNASP.\relax }}\hyper@linkend}
\newcommand\getmystring[1]{\expandafter\argfouroffive#1}
\newcommand\argfouroffive[5]{\xdef\NL{\argtwoofthree#4}\xdef\Mys{\argthreeofthree#4}}
\newcommand\argtwoofthree[3]{#2}
\newcommand\argthreeofthree[3]{#3}
\makeatother
\begin{document}
\getmystring\myparam

Number is \NL

String is \Mys
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .