# [Dev-luatex] Example: Sorting in LuaTeX

Jonathan Sauer Jonathan.Sauer at silverstroke.com
Thu Aug 9 10:59:20 CEST 2007

Hello,

yesterday I experimented a little with \directlua, and I decided to
code a sorting routine, since sorting in TeX (as i.e. implemented in
the experimental LaTeX 3 code or in Bernd Raichle's paper "Sorting
in TeX's mouth") is quite slow.

The example is in PlainTeX.

------------------------------------- CUT
-----------------------------------

% We preload' the necessary functions into the Lua state
%
% Note that comments inside \directlua must be TeX comments (%'');
% Lua comments (--'') do not work
%
% The sorting code has been copied from the Lua users wiki (page
% MakingLuaLikePhp'')
\directlua0{
function explode(str,div)
if (div=='') then
return false
end
local pos,arr = 0,{}
% For each divider found
for st,sp in function() return string.find(str,div,pos,true) end

do
% Attach chars left of current divider
table.insert(arr,string.sub(str,pos,st-1))
% Jump past current divider
pos = sp + 1
end
% Attach chars right of last divider
table.insert(arr,string.sub(str,pos))
return arr
end

function implode(t,div)
return table.concat(t,div)
end
}

% #1 = continuation (called with the sorted list as its only
% parameter), #2 = delimiter, #3 = <delimiter>-separated list
\def\sort#1#2#3{%
\directlua0{%
% We prevent expansion and then escape all characters
special
% to lua in the parameters. This is necessary to prevent
them
% from (a) executed right now and (b) being
misunderstood by
% Lua.
%
% We convert the string into a table, delimited by a
% (unexpanded, escaped) delimiter:
local t = explode("\luaescapestring{\unexpanded{#3}}",

"\luaescapestring{\unexpanded{#2}}")
% We sort the table in-place:
table.sort(t)
% We output the table as a string.
%
% We use a single call to tex.print to prevent spaces in

% the output, since all tex.print calls except the last
% an \endlinechar (LuaTeX manual 4.1.10.1):
tex.print("\luaescapestring{\unexpanded{#1}}{" ..
implode(t," ") .. "}")
}%
}

% Output tokens without expansion
\def\typeout#1{\message{\unexpanded{[#1]}}}

% Example (text taken from Wikipedia, Hyperreality'')
\sort{\typeout}{ }{%
In semiotics and postmodern philosophy, the term hyperreality
characterizes the inability of consciousness to distinguish reality
from fantasy, especially \in technologically advanced postmodern
cultures. Hyperreality is a means of characterising the way
consciousness defines what is actually real' in a world where a
multitude of media can radically shape and filter the original event
or experience being depicted. Some famous theorists of hyperreality
include Jean Baudrillard, \Albert Borgmann, Daniel Boorstin, and
Umberto Eco.
%
Most aspects of hyperreality can be thought of as reality by proxy.'
For example, a viewer watching pornography begins to live in the
non-existent world of the pornography, and even though the pornography
is not an accurate depiction \of sex, for the viewer, the reality of
sex' becomes something non-existent. Some examples are simpler: the
McDonald's M' arches create a world with the promise of endless
amounts of identical food, when in reality' the M' represents
nothing, and the food produced is neither identical nor infinite.
%
Baudrillard in particular suggests that the world we live in has been
replaced by a copy world, \where we seek simulated stimuli and nothing
more. Baudrillard borrows, from Jorge Luis Borges (which Borges also
borrowed from Lewis Carroll), the example of a society whose
cartographers create a map so detailed that it covers the very things
it was designed to \represent. When the empire declines, the map fades
into the landscape and there is neither the representation nor the
real remaining -- just the hyperreal.
%
Baudrillard's idea of \hyperreality was heavily influenced by
phenomenology, semiotics, and Marshall McLuhan.}

\bye

------------------------------------- CUT
-----------------------------------

Sorting in Lua is really, really fast, even though the token list has to
be converted into a table and back. At least, it is a lot faster than
sorting
in TeX.

Caveat: In TeX, braces can be used to hide the <delimiter>; this is not
possible
in the above code. I plan to code a "string.findOutsideBraces" function
to
handle braces correctly, but I wonder if this should not be a library
routine.

In any case: Imagine the possibilities when using Lua not only for
manipulating
the node list, but also for parsing user input!

Delighted,
Jonathan

`