[luatex] \luaescapestring and # tokens

Joseph Wright joseph.wright at morningstar2.co.uk
Mon Jun 2 08:24:31 CEST 2014

Hello all,

The usual approach to emulating \pdfstrcmp in LuaTeX involves escaping
input using \luaescapestring and then doing a Lua-based comparison,
something like:

\catcode`\@=11 %
    function strcmp (A, B)
      if A == B then
        tex.write ("0")
      elseif A < B then
        tex.write ("-1")
        tex.write ("1")
  \long\def\pdf at strcmp#1#2{%
      strcmp ("\luaescapestring{#1}", "\luaescapestring{#2}")
    \let\pdf at strcmp\pdfstrcmp
    \let\pdf at strcmp\strcmp
\message{Value is: \pdf at strcmp{#}{\detokenize{#}}}

However, as is shown by the demo, this may fail to give the same result
as for the 'real' primitive if # tokens (more generally, catcode 6
tokens). The reason is \luaescapestring doesn't double # tokens.

\message{"\luaescapestring{#}" "\detokenize{#}"}

Is this deliberate, and if so is there a reliable way of emulating
\pdfstrcmp in it's entirety?
Joseph Wright

More information about the luatex mailing list