# Capitalise certain words in document

I have a very large set of documents where some keywords are not always spelled correctly. Is is possible to tell latex to always write certain words in a certain way? (in this case it'd be to capitalise them, but i may consider adding \emph or so)

I could write a script for that, but i am also interested in this kind of a-posteriori editing more in general.

Intuitively, the text is something like:

This causes a Damage roll, that causes a damage roll.


So i'd like to tell latex that "damage roll" is to be written "Damage Roll", so the output of the text above is:

This causes a Damage Roll, that causes a Damage Roll.


even though the source differs.

• have you see : tex.ac.uk/FAQ-casechange.html – flav Oct 11 '16 at 13:30
• i know those commands, but i don't want to browse through the loads of text, i want to use something that automates that for me! – Squera Oct 11 '16 at 13:32
• Perhaps LuaLaTeX may help here to perform a search-and-replace on the input; not sure what might happen when a line-break occurs between two words though. Otherwise (and perhaps more suited) would be to parse the input text using something like sed. – Werner Oct 11 '16 at 15:17

Mixing everything up you get:

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

\usepackage{polyglossia}
\usepackage{mfirstuc} % << fot capitalisewords command
\usepackage{luacode} % << for luacode environment
\setdefaultlanguage{english}

%% Lua code - Credits to Mico
\begin{luacode}
function textreplace ( buff )
buff = string.gsub ( buff, "key sentence one", "\\capitalisewords{%0}" )
buff = string.gsub ( buff, "key sentence two",       "\\capitalisewords{%0}" )
buff = string.gsub ( buff, "key sentence three",   "\\capitalisewords{%0}" )
return buff
end
\end{luacode}
\newcommand{\TextreplaceOn}{\directlua{luatexbase.add_to_callback( "process_input_buffer" , textreplace , "textreplace" )}}
\newcommand{\TextreplaceOff}{\directlua{luatexbase.remove_from_callback( "process_input_buffer" , "textreplace" )}}

\begin{document}
Writing your document normally with key sentence one, other key sentence two and maybe also a key sentence three and the output is:

\TextreplaceOn
Writing your document normally with key Sentence one, other key sentence two  and maybe also a key dentence three and the output is
\end{document}


For each new key sentence add a new buff = string... line. But you have to compile with LuaLaTeX, obviously (I say but because there is few downsides on doing so).

Sources (in case you want to do further customizations):

EDIT: Taking into account what Werner had said in the comments: since the Lua code is processed before TeX does its magic the key sentence when TeX encounters it is longer key sentence, it's \capitalise{key sentence}, therefore it can span multiple lines and still the compilation will occur normally. Furthermore a hyphenation package can be still be used (polyglossia since there's need for LuaLaTeX). Also, from the third source I've posted, the Lua code can be activeted and deactiveted. I've updated the code to show this effects and implemented the activation macro.

Here is the compiled example:

• What happens if the search string spans multiple lines? – Werner Oct 11 '16 at 15:54
• Nothing happens, it compiles normally. Since the lua code acts before TeX does by the time TeX gets a hold of the key sentence it's already inside \capitalisewords. Good point though. – Guilherme Zanotelli Oct 11 '16 at 16:13

I have found this: https://stackoverflow.com/questions/1812911/replace-strings-in-latex. I think you'd better use other tools (such as perl, sed, etc), though LuaTeX seems to provide a solution: Macro: Replace all occurrences of a word.

While you can write code that will look for words and format them, it is probably an easier job for a text search tool. I would just use my favorite editor (or command line tool) to do a find and replace of "damage roll" with something like \mykeyword{damage roll} and then you can redefine \mykeyword to do whatever formatting you want.