# How to create a pre-processing command that accepts plain text and generates formatted text (based on configurable options)

Looking for the best way of creating a string-processing command in latex to achieve things like below:

• Given a long piece of normal text, and a set of keywords, automatically format the matching words inside the text (e.g. by adding \emph or \textbf etc..) based on some configurable options
• Given a long piece of normal text, convert each line into a list item (e.g. convert each \\ into an \item and enclose the whole inside \begin{itemize} etc...

I am creating a style package that needs this feature to auto-format the plain text supplied by my package users (based on their keywords and other options supplied through command parameters etc.)

What is the best approach? It looks like the \SearchList is only for xelatex. Interested in a generic implementation.

Does latex have string replacement commands that help achieve this? Any existing packages or code samples would be of great help.

• This could be an approach for you: tex.stackexchange.com/a/45791/10570. This example uses LuaLaTeX and replaces only single letters. But I think it is easy to adopt the methods to replace whole words or phrases (The Lua command string.gsub will be your friend). Another example you can find here: wiki.luatex.org/index.php/Process_input_buffer. – Holle Jul 17 '13 at 8:53
• If your workflow allows non-TeX processing there are many easy scripting language solutions - awk or perl or python or ... – Ethan Bolker Jul 17 '13 at 12:10
• Thank you - I would like this to be a latex package - so looking for natural latex constructs. Something on the lines of 'string search' and 'replace' should do. Any 'string' manipulation package in latex? (like the luaLatex gsub one suggested above?) – Gopalakrishna Palem Jul 17 '13 at 13:28

After searching and asking here and there found something suitable for the purpose. Recording it here, just in case anyone looking for similar functionality.

In the code below, the command FormatKeyWords accepts keywords and the normal text (along with optional format specification) converting each instance of the keyword inside the input text into the format specified.

\RequirePackage{xstring}    % defines string manipulations for keyword replacement
\RequirePackage{tikz}   % defines foreach to loop over keywords
\RequirePackage{xargs}  % allows optional parameters in commands

\makeatletter
%
% Default format style used by \CVM@FormatKeyWords
% [1]: the text to be formatted
\newcommand{\CVM@Format}[1]{\textbf{\small#1}}
%
% Arguments:
% [1]: [optional]format specification
% [2]: [optional]0 or number indicating the number of occurrences to replace
% [3]: Keywords (as comma separated values)
% [4]: Text containing the keywords
\newcommandx{\FormatKeyWords}[4][1=\CVM@Format,2=0]{
\expandarg
\foreach \keyword in #3
{
\begingroup\edef\x{\endgroup
\unexpanded{\StrSubstitute[#2]{#4}{\keyword}}
{\noexpand#1{\keyword}}}\x[\temp]
\global\let#4\temp
}
#4
}
\makeatother
%


Usage is as below:

\newcommand{\MyFormat}[1]{\emph{\LARGE#1}}

\def\KeyWords{one, two, three}
\def\MyText{this is one, next is two then three and one two three}

\FormatKeyWords[\MyFormat][1]{\KeyWords}{\MyText} % format the first occurrence of each keyword


In case one is interested, this is part of the CVMaker project for laying out dynamic CVs.