9

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 - ?

l3regex -

stringstrings - ?

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: Is there a way to have xesearch search and replace a term and have the replacement compiled as LaTeX command?

Related: Automatically format words in a PDFLaTeX document which are in a keyword list

Related: space-after-latex-commands

A Feldman
  • 3,930

1 Answers1

10

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!)

enter image description here

% !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{%
  \directlua{luatexbase.add_to_callback ( 
    "process_input_buffer", replace_dark_with_bright, "replace_dark_with_bright" )}
  \directlua{luatexbase.add_to_callback ( 
    "process_input_buffer", replace_bf_with_em , "replace_bf_with_em" )}}

\begin{document}
Always look on the dark side of life.

To \textbf{emphasize} a word.
\end{document}
Mico
  • 506,678
  • What would a simple example look like? Both for a text substitution and a "command" subsitution. – A Feldman Jan 28 '16 at 04:11
  • 2
    @AFeldman - I've posted an example of Lua code performing text and command substitution. – Mico Jan 28 '16 at 04:46
  • is that as simple as it gets using Lua code? – A Feldman Jan 28 '16 at 14:13
  • 2
    @AFeldman - I gave two examples of text and command substitutions that work on the entire document. (That's what adding the Lua functions to the "process_input_buffer" callback is about.) If one doesn't need this scope, simpler setups are of course possible. It might be helpful if you gave specific applications and tasks you have in mind. – Mico Jan 28 '16 at 16:05
  • Not meant to be a criticism of your answer, which I think is elegant. I was just wondering if it was the minimum as far as lua text and command replacement goes for an entire document. – A Feldman Jan 28 '16 at 20:26
  • @AFeldman - Lua's syntax rules would let you replace the two instructions s = string.gsub ( s, "dark" , "bright" ) and return s with the single instruction return s:gsub ( "dark" , "bright" ). I don't think many CPU cycles are saved with the terser instruction, and (speaking for myself, obviously) I find the terser expression less easy to parse. – Mico Jan 28 '16 at 20:35
  • @AFeldman - For a somewhat more involved (and probably more interesting!) demonstration of the versatility of LuaLaTeX for text and command substitution purposes, see my answer to the posting Underline just words, not the space between them. The point of my answer was to show that the specialized form of underlining that the OP wanted can be achieved without having to load and/or modify an external package such as soul. – Mico Jan 30 '16 at 12:02
  • I checked it out, very very cool :) – A Feldman Jan 30 '16 at 13:31
  • @AFeldman - What's bemusing (or amusing?!) about the posting I referred to in my earlier comment is that the accepted answer and the other answer that currently has more upvotes than mine both actually fail to satisfy one of the OP's stated requirements, viz., that not only whitespace but also punctuation marks should not be underlined. For the task at hand, Lua's pattern-matching capabilities really shine: the %a+ selector matches complete words, i.e., contiguous sequences of uppercase and lowercase letters, and nothing else -- exactly what the OP wanted. – Mico Jan 30 '16 at 15:54
  • 1
    Not bemused or amused. Just expressing admiration. – A Feldman Jan 30 '16 at 18:11