A follow-up to my earlier question: Macro to replace text with random string of same length
Thanks to the answer of @Mico, we now have a macro in Lua to replace a UTF-8 String with random characters. However, one issue is that when presented with a macro, the code assumes that the characters \...{ and }as well as \... are all counted for obfuscation. This is problematic because for wireframing, it results in random strings longer than in the ordinary text. Is there a way to get xyz and \textit{xyz} to have hte same length randomised ASCII output?
The MWE (credit to @Mico) is below:
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment and '\luastring' macro
\begin{luacode}
function rndstring ( inputstring )
local outputstring, choices, mm, nn
mm = unicode.utf8.len(inputstring) -- no. of utf8-encoded characters in input string
-- Place candidate replacement characters in a Lua table:
choices = {
"0","
}--substantially simplified to reduce size -- Number of rows in 'choices' table
nn = #choices
-- Generate the outputstring in a 'for' loop:
outputstring = ""
for i = 1 , mm do
if unicode.utf8.sub ( inputstring , i , i ) == " " then
outputstring = outputstring .. " " -- preserve space char.
else -- choose a new char randomly from 'choices' table
outputstring = outputstring .. choices[ math.random ( nn ) ]
end
end
return ( outputstring )
end
\end{luacode}
%% Define a LaTeX macro to invoke the Lua function
\newcommand\rndstring[1]{\directlua{tex.sprint(rndstring(\luastring{#1}))}}
\begin{document}
\ttfamily
\rndstring{This is a string.}
\rnstring{\textit{This is a String}}
%%%% These two Strings should be (but aren't) the same length
\end{document}

token.put_next(toks)andtoken.set_lua? – projetmbc Feb 02 '22 at 23:03tokenlibrary” in the LuaTeX manual: https://www.pragma-ade.nl/general/manuals/luatex.pdf#%232338 – Henri Menke Feb 03 '22 at 08:41#on non-sequence tables is undefined in Lua 5.2 https://stackoverflow.com/questions/23590885/why-does-luas-length-operator-return-unexpected-values?noredirect=1 (although currently I see lualatex uses Lua 5.3 so no problem there) – user202729 Jul 17 '22 at 14:46\luadefregisters will be allocated incrementally, same as\count,\dimen,\skip, etc. – Henri Menke Jul 17 '22 at 17:35