I need to split a string into one or more substrings. I know that I could use the xstring package, but I'd like to do it using only inbuilt TeX/LaTeX commands. So, if I say

\def\MyTeXKnowledge{Not good enough}

what is the simplest way to extract the substrings "Not", "good" and "enough" from the macro \MyTexKnowledge and store them in variables?

2 Answers 2


You need to define a macro which has the separation character in the parameter text:

\def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) }
\testthreewords{Now good enough}

If you want to be able to provide a macro as argument you need to expand it first. This can be either done once (only first macro is expanded once):


or completely:


The \relax here is used as an end marker and must not occur in the argument, otherwise a different macro should be used, like \@nnil. The grouping is added to keep the temporary definitions local.

However this setup fails with an error if the two spaces are not included in the argument. To be on the safe side you should read every substring on its own and add the separation character to the end as a fail-safe. Then you test if the end was reached:

\def\readwords#1 #2\relax{%
      \doword{#1}%  #1 = substr, #2 = rest of string
      \ifx\relax#2\relax  % is #2 empty?
         \def\next{\endgroup\endtestwords}% your own end-macro if required

\testwords{Now good enough}% Gives `(Now)(good)(enough)`
\testwords{Now good}% Gives `(Now)(good)`


  • thanks. With your help I have achieved the result that I needed. I think a modification is needed before I accept your answer, though. If I define \MyTeXKnowledge as above and then say \testthreewords{\MyTeXKnowledge} I get an error (presumably because \MyTeXKnowledge counts as only one argument). Mar 6, 2011 at 22:49
  • @Ian: I wasn't sure about the exact interface you want to use. You need to expand the macro first. I will update my answer. Mar 6, 2011 at 22:53
  • 1
    I have edited the wording of my question to clarify. Mar 6, 2011 at 23:09
  • 2
    Any chance of a MWE? This is such a good example - but rather complicated for a newbie like myself. An MWE will allow me to play around with it and help me understand it!
    – 3kstc
    Jul 24, 2019 at 4:40

Another way : the words are stocked in macros \worda \wordb etc.


\def\multiwords#1 #2\@nil{% 
 \advance\nbofwords by  1 %   
 \expandafter\edef\csname word\@alph\nbofwords\endcsname{#1}% 

   \nbofwords=0 %
   \expandafter\next#1 \@nil %

 \def\MyTeXKnowledge{Not good  enough the end}

There are \the\nbofwords\  words:  \worda; \wordb; \wordc;\wordd;\worde.


Now \MyTeXKnowledgeis accepted.

  • @Ian: Now \MyTeXKnowledgeis accepted and the substrings from the macro \MyTexKnowledge are stored in variables. Mar 7, 2011 at 7:48
  • @AlainMatthes: I really like this solution since we can use it for an arbitrary number of words, right? I'm just learning TeX but will try to understand better this code since it appears to solve some other question I posted. May 12, 2014 at 15:58

You must log in to answer this question.

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