# Automatic LaTeX parsing to replace text with macros

I know this title is pretty bad but I couldnt come up with something better. Basically what I want is to be able to write normal text, but that it displays in PDF in a custom alphabet once compiled. This is different than just using a custom font because for example, if I write "la" it should corresponds to one character, and "lo" to another different character. The point is I want to be able to write it without using ten billion macros myself, for example writing \customLA \customLO instead of la lo would be extremely tedious if I want to write lines and lines.

Is there a simple and automatic way to do it with LaTeX? Or should i write my own parsing program in C++ to modifiy the LaTeX file to replace characters combinations with macros?

-
This is rather easy with Xe(La)TeX. Where the “la” and ”lo” characters are taken from? – egreg Jul 16 '13 at 10:41
They would most likely be taken from some scalable vector image file – Fatalize Jul 16 '13 at 10:57
So, no. One could think to make l active and using something similar to babel's shortcuts. But this would break all macros and environments that contain l in their name. – egreg Jul 16 '13 at 11:03
what about making a custom font file with suitable ligatures? it's possible with a vpl (virtual property list) file to devise appropriate instructions, and people have repackaged small (scalable) graphics as type 1 fonts. (i think there was an article in tugboat about the repackaging part, but don't have time to look it up at the moment.) – barbara beeton Jul 16 '13 at 12:32
@barbarabeeton I'll check this, seems like a clever idea. For now I developed a C parser that will do the job just fine, it's just a bit long, repetitive and unclever – Fatalize Jul 16 '13 at 13:06

This is a simple replacement tool at a Lua level. Everything written in \parseme is a target for replacements, but the TeX commands are not expanded and we can hide a portion of text from replacement procedure. The replacements are stored in a simple Lua table, the first column contains the searched terms and the second column contains their replacements. Testing goes from top to bottom. For instance A is replaced quite soon and never again. On the other hand, X is going to be Y, then Y goes to Z and that goes to A. The name of commands will be replaced, e.g. \colorme would become \cojuicerme. I used the \clrme command to illustrate a fast workaround, if needed.

If you need even more advanced tool for string manipulations than the string library can offer, I can recommend you LPeg library, that's a tool which is already installed in LuaTeX. Some examples are mentioned in Programming in LuaTeX article.

If you run my example you will see 1 2 3 4 5 6 three times in the terminal. It means there were 6 replacement tests called three times by the \parseme command during typesetting.

%! lualatex mal-text-parser.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\parindent=0pt
% It fixes beginnings and endings of I/O lines, among other things...
\usepackage{luatextra}
\pagestyle{empty}
\usepackage{luacode}
\usepackage{xcolor}

\begin{document}
\def\parseme#1{% TeX definition...
\directlua{parseme("\unexpanded{#1}")}%
% This is working: "\unexpanded{#1}" but one must write \\  instead of backslash in the \parseme command.
}% End of \parseme command...

\begin{luacode*}
-- A conversion table, from -> to
local maltable={
{"la", "beer"},
{"lo", "juice"},
{"A", "Hello World! I was here! Phantom-as!"},
{"X","Y"},
{"Y","Z"},
{"Z","A"},
}
function parseme(text) -- Lua function
-- Read an argument sent by TeX...
content=text -- Backup of original text...
-- Do all the necessary replacements...
for i=1,#maltable do
tex.sprint("\\message{"..i.."}")
content=string.gsub(content,maltable[i][1],maltable[i][2])
end
-- Typeset the result at the terminal and to the document...
print(content)
tex.print(content)
end
\end{luacode*}

\def\formated{\textbf{lalo}} % This part is not replaced.
\def\clrme#1{{\color{red}#1}} % Definition of the command.
% We use \clrme instead of \colorme because we would get -> \cojuicerme as lo is being replaced...
% lalo in \formated is protected from expansion and replacement...
Text of the paragraph. \parseme{My long \\formated{} sentence. \\clrme{To la red!} \\textbf{Hey!} Ending. lalala lo lo lo A}\par
End of the paragraph. \parseme{My \\clrme{next} try.} The end.\par
Input is X, result is: \parseme{X}; X goes to Y to Z to A, but A is not replaced anymore.

\end{document}


-