Skip to main content

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords\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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Now good enough}% Gives `(Now)(good)(enough)` \testwords{Now good}% Gives `(Now)(good)` 
 

 

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Now good enough}% Gives `(Now)(good)(enough)` \testwords{Now good}% Gives `(Now)(good)` 

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

\def\testthreewords#1{\threewords#1\relax} \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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Now good enough}% Gives `(Now)(good)(enough)` \testwords{Now good}% Gives `(Now)(good)` 
 

 

edited body
Source Link
Martin Scharrer
  • 270.1k
  • 66
  • 795
  • 956

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{NotNow 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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{NotNow good enough}% Gives `(NotNow)(good)(enough)` \testwords{NotNow good}% Gives `(NotNow)(good)` 

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{Not 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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Not good enough}% Gives `(Not)(good)(enough)` \testwords{Not good}% Gives `(Not)(good)` 

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Now good enough}% Gives `(Now)(good)(enough)` \testwords{Now good}% Gives `(Now)(good)` 
minor grammar fixes
Source Link
Hendrik Vogt
  • 39.4k
  • 11
  • 141
  • 215

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{Not 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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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

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

\def\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Not good enough}% Gives `(Not)(good)(enough)` \testwords{Not good}% Gives `(Not)(good)` 

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{Not 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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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

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

\def\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Not good enough}% Gives `(Not)(good)(enough)` \testwords{Not good}% Gives `(Not)(good)` 

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

\def\testthreewords#1{\threewords#1\relax} \def\threewords#1 #2 #3\relax{ First: (#1), Second: (#2), Third: (#3) } \testthreeowords{Not 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):

\def\testthreewords#1{\expandafter\threewords#1\relax} 

or completely:

\def\testthreewords#1{% \begingroup \edef\@tempa{#1}% \expandafter\endgroup \expandafter\threewords\@tempa\relax } 

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\testwords#1{% \begingroup \edef\@tempa{#1\space}% \expandafter\endgroup \expandafter\readwords\@tempa\relax } \def\readwords#1 #2\relax{% \doword{#1}% #1 = substr, #2 = rest of string \begingroup \ifx\relax#2\relax % is #2 empty? \def\next{\endgroup\endtestwords}% your own end-macro if required \else \def\next{\endgroup\readwords#2\relax}% \fi \next } \def\doword#1{(#1)} \def\endtestwords{} \testwords{Not good enough}% Gives `(Not)(good)(enough)` \testwords{Not good}% Gives `(Not)(good)` 
Updated code to expand argument first; added 5 characters in body
Source Link
Martin Scharrer
  • 270.1k
  • 66
  • 795
  • 956
Loading
added 37 characters in body
Source Link
Martin Scharrer
  • 270.1k
  • 66
  • 795
  • 956
Loading
Source Link
Martin Scharrer
  • 270.1k
  • 66
  • 795
  • 956
Loading