[Dev-luatex] Example: Sorting in LuaTeX

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


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'')
function explode(str,div)
	if (div=='') then
  		return false
	local pos,arr = 0,{}
	% For each divider found
	for st,sp in function() return string.find(str,div,pos,true) end

		% Attach chars left of current divider
		% Jump past current divider
		pos = sp + 1
	% Attach chars right of last divider
	return arr

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

% #1 = continuation (called with the sorted list as its only
% parameter), #2 = delimiter, #3 = <delimiter>-separated list
		% We prevent expansion and then escape all characters
		% to lua in the parameters. This is necessary to prevent
		% 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}}",
		% We sort the table in-place:
		% 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
		tex.print("\luaescapestring{\unexpanded{#1}}{" ..
implode(t," ") .. "}")

% Output tokens without expansion

% 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.}


------------------------------------- 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
in TeX.

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

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


More information about the dev-luatex mailing list