I would like a command that takes a word and replaces each letter with a box.
For example, the output of
The mystery word is \mycommand{hello}.
should be
The mystery word is □□□□□.
I would like a command that takes a word and replaces each letter with a box.
For example, the output of
The mystery word is \mycommand{hello}.
should be
The mystery word is □□□□□.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,amssymb}
\Characterdirective{\addcytoks{$\square$}}
\newcommand\mycommand[1]{\tokencyclexpress#1\endtokencyclexpress}
\begin{document}
The mystery word is \mycommand{hello}.
\end{document}
To give an example of fuller possibilities, I provide another example:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,amssymb}
\Characterdirective{\addcytoks{\fbox{#1}\;}}
\newcommand\mycommand[1]{\tokencyclexpress#1\endtokencyclexpress}
\begin{document}
The mystery word is \mycommand{hello \textit{Mom}. \textbf{I love you}.}.
\end{document}
$\square$, even code than employs the replaced token (denoted as #1). I'll give an example.
– Steven B. Segletes
Feb 13 '24 at 13:51
tokcycle package...to iterate over a string of tokens. In these examples, I only alter the result for "Characters". However, Groups, Macros, and Spaces are also detected and can have their own special treatment.
– Steven B. Segletes
Feb 13 '24 at 14:00
For good measure, a LuaLaTeX-based solution.
Note that the argument of \mycommand can contain general utf8-encoded characters, not just ASCII-encoded characters; thus, \mycommand{你好世界} is entirely ok. Note further that because \directlua and \luastring are expandable, so is \mycommand.
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amssymb} % for '\square' macro
\usepackage{luacode} % for '\luastring' macro
\newcommand\mycommand[1]{\directlua{
tex.sprint (( unicode.utf8.gsub ( \luastring{#1} , "." , "$\\square$" ) )) }}
\begin{document}
The mystery word is \mycommand{hello}, or is it \mycommand{你好世界}?
\end{document}
Basically a one-liner with expl3:
\documentclass{article}
\providecommand{\textsquare}{% from amsthm
\begingroup \usefont{U}{msa}{m}{n}\symbol{3}\endgroup
}
\ExplSyntaxOn
\NewDocumentCommand{\mystery}{m}
{
\text_map_inline:nn { #1 } { \textsquare \kern1pt } \unkern
}
\ExplSyntaxOff
\begin{document}
The mystery word is \mystery{hello}
The mystery word is \mystery{équipe}
The mystery word is \mystery{naïve}
\end{document}