5

I would like to do a simple \StrSplit (from the xstring package) on a string I have, however, the string in question may contain a \par, and it appears that \@xs@StrSpl@t@@, which I can only imagine is an internal xstring macro, is not a long macro, so it causes an error.

Are there any workarounds? Other packages with similar functionality that accept long strings? A way to remove \par temporarily and put it back later?

EDIT: I only actually need to split off the first character, so if there's a way to do that directly on arbitrary strings (with the semantics of \noexpand), that would work too.

1 Answer 1

2

You can try this to get the first character:

\documentclass{article}
\begin{document}

\makeatletter
\long\def\test#1{%
  \long\def\getfirstchar##1##2;{%
  \def\storeone{##1}
}
 \getfirstchar#1;
}
\makeatother


\test{This is \par something}

\storeone
\end{document}

The macro gets the first letter and stores it in another macro called storeone. I am not sure what you want to do exactly, other than get the first letter, perhaps you can expand on your question a bit. You can also have a look at source2e, there are similar commands such as \@firstoftwo.

4
  • Why the makeatletter there? You don't seem to use @.
    – coppro
    Commented Jan 14, 2013 at 5:58
  • @centrifugal force:) You right they are not needed, I just typed them planning to use @ and then thought I maybe obscuring the concepts.
    – yannisl
    Commented Jan 14, 2013 at 6:19
  • Also, this solution doesn't work if the string contains a semicolon; I need it to work for (nearly) arbitrary text. I was thinking maybe something clever with \futurelet to remove a token from the stream and then put it back. EDIT: picked a dummy command as a delimiter. Still doesn't handle curly-brace constructs well. I'm thinking maybe there's a futurelet trick?
    – coppro
    Commented Jan 14, 2013 at 6:28
  • @copro The delimiting is not a big issue, you can delimit it with anything else you want (say @end). However, if you want to catch any type of character is another issue. Then futurelet is the answer. See how \@ifnextchar works, you need to clarify your case a bit better.
    – yannisl
    Commented Jan 14, 2013 at 6:49

You must log in to answer this question.

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