# What packages provide text/command substitution for LaTeX, XeTeX and LuaLaTeX?

I am aware of the following:

xesearch - Allows both text and command substitution. Only for XeLaTeX

Simple example:

\usepackage{xesearch}
\SearchList{list1}{Hello}}{hlw}


Any time "hlw" is present in an XeLaTex document it will be expanded into "Hello". A command can be used in place of "Hello" and will work in most cases.

abbrevs - Allows text replacement. I do not think it will do command substitution. To my knowledge LaTeX and LuaLaTeX

Simple example:

 \usepackage{abbrevs)
\newabbrev\hlw{Hello}


Any time "hlw" is present in a LaTeX or LuaLaTeX document it will be expanded into "Hello".

chickenize - Through its "substitutewords" function. Not sure about command substitution. The documentation identifies it as a LuaTeX/LuaLaTeX solution only. It will also replace every word in your document with "chicken" should you desire to "chickenize" your document.

xstring - ?

luacode - Allows for both text and command substitution. It provides macros (e.g., \luaexec and \luastring) and environments (e.g., luacode and luacode*) that allow the programmer to create Lua functions that have full access to Lua's powerful string library. Of course only LuaLaTeX. Thanks to Mico for his below answer and example of both text and command substitution.

What other substitutions packages exist?

For each, is it just for text, or can commands be substituted?

In which TeX variant can the package be used?

What would a simple example look like in each package?

Related: space-after-latex-commands

• you are missing l3regex – David Carlisle Jan 27 '16 at 18:44
• Thanks @DavidCarlisle, I figured that there were a lot more out there that I had no idea about. – A Feldman Jan 27 '16 at 18:48
• but the question is rather under defined, tex is a macro replacement language so "command substitution" is basically all it does. – David Carlisle Jan 27 '16 at 19:32

LuaLaTeX embeds the Lua scripting language, which provides a powerful and flexible set of string manipulation functions. Via the \directlua macro and the environments provided by the luacode package, LuaLaTeX provides easy access to these string functions. By using LuaLaTeX, it's straightforward to roll one's own text and macro substitution routines.

Per the OP's follow-up request, here's an example of Lua code that performs text and command substitution throughout the document. (Aside: The Lua string.gsub function is extremely powerful; the example shown here is certainly not meant to showcase the full power and versatility of string.gsub!)

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode}
\begin{luacode}
function replace_dark_with_bright ( s )
s = string.gsub ( s, "dark" , "bright" )
return s
end
function replace_bf_with_em ( s )
s = string.gsub ( s, "\\textbf" , "\\emph" )
return s
end
\end{luacode}
\AtBeginDocument{%
"process_input_buffer", replace_dark_with_bright, "replace_dark_with_bright" )}