# Is there a way to have xesearch search and replace a term and have the replacement compiled as LaTeX command?

I have used the package xesearch https://www.ctan.org/pkg/xesearch?lang=en xesearch in my header to automatically replace shorthand notations:

\usepackage{xesearch}
\SearchList{list1}{Very Long Search Replacement 1}{SRa}
\SearchList{list2}{Very Long Search Replacement 2}{SRb}


Which when put into the header acts to search and replace:

"SRa and SRb" into

"Very Long Search Replacement 1 and Very Long Search Replacement 2"

in the resulting document.

Which is great!

But it did occur to me that if I could have xesearch replace the placeholders with LaTeX commmands, and run them, at compile time that would be useful, eg:

\usepackage{xesearch}
\SearchList{list1}{\SomeGreatLaTeXCommand[with options]}{SRa}
\SearchList{list2}{\AReallyFantasticLaTeXCommand}{SRb}


So that putting SRa in your code would result in \SomeGreatLaTeXCommmand[with options] being compiled and same with SRb compiling as \AReallyFantasticLaTeXCommand

I am aware that one can make substitute LaTeX commands as follows:

 \newcommand{\bc}{\begin{center}}


Does anybody know a way for the xesearch package to be used in this manner? If xesearch cannot be put to work this way, is there another package that could do this type of automatic search and replace? Is there a way to do this without XeLaTeX? Or, is this a really very stupid question, because LaTeX/TEX is a programming language?

• xesearch is very cool, but wouldn't sed or awk be more appropriate for string manipulation? (Off-topic: I would avoid using \bc in that way. In the end it just obfuscates the code.) – jon Dec 7 '15 at 6:35
• I wouldn't know how to use sed or awk, at least not at this point :) – A Feldman Dec 8 '15 at 6:32

Yes, one can use commands in the replacement texts. Actually almost all examples in the xesearch documentation use commands. I have not tried to see if commands can be used which examine what comes next, but things like \begin{center} work.

Update adds an example showing that not everything is possible (at least out of the box):

% compile with xelatex
\documentclass{article}
\usepackage{xesearch}
\usepackage{fontspec}
\usepackage{geometry}
\begin{document}\Huge

\SearchList{bc}{\begin{center}}{bc}

\SearchList{ec}{\end{center}}{ec}

Before

bc
Hello World!
ec

After
\end{document}


% compile with xelatex
\documentclass[varwidth, preview, border=10pt]{standalone}
\usepackage{xesearch}
\usepackage{fontspec}
\usepackage{geometry}
\begin{document}\Huge
\begin{preview}
\SearchList{newlinel}{\\}{nl}

This is some nl nonsensical text.

We may even try nl[30pt] crazy things but it does not\\[30pt] work.
\end{preview}
\end{document}


• Thanks so much. I guess I had "assumed" that it did not do that without some kind of extra wizardry. – A Feldman Dec 16 '15 at 15:28
• @AFeldman I understand, because it does look magical ... – user4686 Dec 16 '15 at 16:31