3

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 □□□□□.

Garf
  • 510

3 Answers3

7
\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}

enter image description here

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}

enter image description here

  • Good start, thank you. Is it possible to make the squares instead editable tcolorboxes or things like that? – Garf Feb 13 '24 at 13:48
  • @Garf I am not sure what you mean by "editable"; however, code may be inserted in the character directive in place of $\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
  • Thanks! I used editable to mean the ability to manipulate the size of the box or other properties etc. This answers my question I think the main challenge was knowing how to tell latex to iterate over each character given. – Garf Feb 13 '24 at 13:58
  • @Garf That is the function of the 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
5

For good measure, a LuaLaTeX-based solution.

enter image description here

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}

Mico
  • 506,678
5

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}

enter image description here

egreg
  • 1,121,712