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}