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

% Default format style used by \CVM@FormatKeyWords
% [1]: the text to be formatted
% 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
    \foreach \keyword in #3

Usage is as below:


\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 

Auto-format Results

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.