# Split a string based on keyword and delimiters

Problem:

I have two words that I would like to split into two variables. For instance:

Word 1 [Word 2]

Minimal Working Example:

I have not been able to generate a minimal working example as I have not been able to find anything similar to this issue, either on Google or SO. The specific issue with this problem is the delimiters [and ].

Desired output:

Based on the delimiters [and ], the output should enable formatting either word in its own way. I have looked into xparse package but could not manage to figure out how to tackle the delimiter issue.

For instance:

Word 1 could be coloured in blue

[Word 2] could be coloured in red (braces to be included in output).

• Do you want a macro to produce such colored output? A macro with optional argument? – Sigur Apr 14 '17 at 16:18
• your question is very unclear. do you mean that the input always is of the form xxx[yyy] and you always split before the [ ? – David Carlisle Apr 14 '17 at 16:19
• @Sigur Yes to both, sorry for not stating that. – kexxcream Apr 14 '17 at 16:20
• @DavidCarlisle Yes, it will always be Word 1 [Word 2]. The split always occur before [. – kexxcream Apr 14 '17 at 16:21
• So [Word 2] is not optional. – Sigur Apr 14 '17 at 16:24

\documentclass{article}

\def\zz#1{\zzz#1}
\def\zzz#1[#2]{\textcolor{blue}{#1}\textcolor{red}{[#2]}}

\usepackage{color}

\begin{document}

\zz{Word 1 [Word 2]}
\end{document}

• Beat me by a minute, though I just went with the \zzz definition, to avoid the need for braces. – Steven B. Segletes Apr 14 '17 at 16:23
• Thanks, but the [ and ] should be included in the output. I have updated my post. – kexxcream Apr 14 '17 at 16:24
• @kexxcream updated answer – David Carlisle Apr 14 '17 at 16:25
• Now update your image, and eliminate the extra space before the red \textcolor – Steven B. Segletes Apr 14 '17 at 16:26
• @StevenB.Segletes give me a second (well, 19, it seems!) – David Carlisle Apr 14 '17 at 16:26