I repeatedly use certain keywords such as var in my document which I highlight using \src{var}. This is cumbersome to type and gets annoying after a while. Repeatedly search-and-replacing var to \src{var} and then \src{\src{var}} to \src{var} also seems needlessly complicated. I want to tell LaTeX to replace all occurrences of var with \src{var}. I found \def that reduces the typing to \var{}, which is not much better. Something similar to the C preprocessor maybe: #define var \src{var}. I found the "Replacing all the dots"-example but failed to modify it to "Replacing all the vars". Thanks for the help.


2 Answers 2


Here's a LuaTeX-based solution. It (i) defines a Lua function that changes all instances of "var" (note the spaces before and after "var") to "\src{var}" and (ii) registers this function with the process_input_buffer callback. This callback operates on the entire input stream before TeX processes it. Separately, I define a TeX macro called \src so that TeX knows what to do when it encounters the instruction \src{var}.

With this setup, words such as "aardvark", "bivariate", and "covariance" are left alone. In the second input line below, the first and last instances of "var" are also left unchanged since they're not preceded and followed by a space. Finally, should there already be instances of \src{var} in your input file, they won't be operated on by the Lua function either -- they'll just get executed by TeX as one would expect.

Observe that nothing is written back to the .tex input file, i.e., your input file won't get littered with lots of \src statements.

By the way, I assume that the real string that's supposed to be operated on by \src in your file is a bit more unique than var. If so, you can probably dispense with the requirement that the string be both preceded and followed by a space.

enter image description here

% !TEX TS-program = lualatex

local function vartosrcvar ( line )
  return string.gsub(line, " var " , " \\src{var} ") 
luatexbase.add_to_callback( "process_input_buffer",  vartosrcvar, "var_to_srcvar")

\newcommand\src[1]{\textcolor{red}{#1}} % define "\src" to suit your needs

aardvark bivariate covariance varnish

var uno var due var tre var
  • I think this is the way I will do it. I still need to skip the vars inside wavy brackets, define a list of keywords and a list of word seperators and let the script handle the rest. I am more comfortable with python and there is a Latex python package that I will use. Thanks.
    – nwp
    Commented Mar 14, 2014 at 7:43
  • @nwp - Thanks! The pythonTeX package is indeed very nice. Just be aware that its output, unlike that of LuaTeX, won't be "real-time." I.e., you'll need (at least) one LaTeX run, one python run, and one more LaTeX run to fully propagate its output. That's not a strike against pythonTeX, of course.
    – Mico
    Commented Mar 14, 2014 at 7:54
  • @Mico So, when I try to compile your code with Lualatex, it seems we can significantly speed up the process (ommit loading a thousand or more fonts on the first run, by removing \usepackage{fontspec})? + I would like to ask: How (most elegantly) to have multiple pre-defined strings of text being looked for?
    – O0123
    Commented Apr 19, 2016 at 19:49
  • @VincentVerheyen - The very first time one runs LuaLaTeX and loads the fontspec package, compilation indeed takes a fair amount of time, as the luaotfload-tool utility has to create a hash table of all properties of all fonts. The good news is that this is done only once. Moreover, FWIW, the font indexing program is a lot faster now than it used to be even just two or three years ago... On to your second question: Lua does pattern matching, not regexpes. Hence, the general answer to you question is, it can't be done. Can you give an example of the string searches you have in mind?
    – Mico
    Commented Apr 19, 2016 at 19:58
  • @Mico I found a way to have multiple pre-defined strings of text being looked for. The following does it for 2 (but it is easy to repeat the process for more): duplicate everything inside the luacode-environment, into a second luacode-environment below and then change var in return string.gsub(line, " var " , " \\src{var} ") appropriately according to your purpose + change the name for each consecutive line of strings in "var_to_srcvar" arbitrarily.
    – O0123
    Commented Apr 19, 2016 at 20:08

TeX is a macro expansion system (like, but more powerful than) C's pre-processor but macros are introduced by \ so use


then zz zzz \var\ zzzz

Or you could define (say) | to be an active character


then zz zzz |var| zzzz

It is, in theory, in very restricted circumstances, possible to get TeX to read ahead and look for var and use the syntax zz zzz var zzzz but I wouldn't seriously consider that, the contortions necessary to make it work will make your document massively fragile and incompatible with more or less any package.

Personally I'd use \src{var} Together with auto-completion in the editor so you don't have to type it all every time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .