2

I want to make an expl3 command that puts a space (~) between letters, and makes every letter uppercase.

\documentclass{article}

\ExplSyntaxOn

    % \NewDocumentCommand \addspaces { m }
    % Some code

\ExplSyntaxOff

\begin{document}

The command \verb|\addspaces{helLo9   $\Delta$  @}| should produce: \\
 
H E L L O 9 $\Delta$ @

\end{document}

enter image description here

Edit: I want it to work inside this environment:

\makebox{} \\
{\centering
  \fontsize{0.023753\paperwidth}{0.023753\paperwidth} \makebox[\textwidth][s]{ \bfseries 
  {\scshape \addspaces{\the\month} \ \  \addspaces{\the\year}  \ \ \addspaces{edition} }}
\par}

But with @Skillmon 's answer I get this:

enter image description here

2
  • Please, make a complete specification. You have an example where there's text interspersed with “backslash-space” commands, but then your example of usage just applies \addspace to single parts. Some clarification is needed.
    – egreg
    Oct 30, 2022 at 10:00
  • 1
    are you maybe looking for microtype's \textls command?
    – Robert
    Oct 30, 2022 at 10:47

3 Answers 3

4

The task can be solved without Expl3 too:

\def\addspaces#1{\uppercase\expandafter{\expandafter\addspacesA#1\end}}
\def\addspacesA#1{\ifx#1\end \else #1 \expandafter\addspacesA\fi}

Of course, you don't have to upgrade your TeXlive 2022 because only TeX primitives are used here and they are more than 40 years in TeX.

5
  • I have a command for displaying the month in another language like this tex.stackexchange.com/a/153775/199568 . Do I need some extra \expandafter to be able to use this command inside \addspaces?: \addspaces{\monthGermanEnglish}? Currently i get an error.
    – Vebjorn
    Oct 30, 2022 at 9:38
  • Yes, the macro supposes only single expand step because you show its usage for \the\month. If your macro ` \monthGermanEnglish` is fully expandable then you can try to use \expanded primitive, for example \def\addspaces#1{\uppercase\expandafter{\expandafter\addspacesA\expanded{#1}\end}} Moreower, you need to use luatex or xetex in order to be correct \uppercase of März.
    – wipet
    Oct 30, 2022 at 9:51
  • Unfortuantley that did not work. I get no errors, but also no output.
    – Vebjorn
    Oct 30, 2022 at 9:55
  • You can test, if the macro is expandable, by \message{\monthGermanEnglish}. I am unable to do this test because I don't know where the \monthGermanEnglish macro is defined.
    – wipet
    Oct 30, 2022 at 10:10
  • Oh, nevermind. It worked with the definition I had, but not the example I gave. It worked in the end.
    – Vebjorn
    Oct 30, 2022 at 10:48
4

The following produces the output you show in your question.

\documentclass{article}

\ExplSyntaxOn
\tl_new:N \l__vebjorn_text_tl
\NewDocumentCommand \addspaces { m }
  {
    \tl_set:Nx \l__vebjorn_text_tl { \text_uppercase:n {#1} }
    \tl_remove_all:Nn \l__vebjorn_text_tl { ~ }
    \exp_last_unbraced:Ne \vebjorn_gobble_space:w
      { \text_map_function:nN \l__vebjorn_text_tl \__vebjorn_addspaces_aux:n }
  }
\use:n { \cs_new:Npn \vebjorn_gobble_space:w } ~ {}
\cs_new:Npn \__vebjorn_addspaces_aux:n #1 { ~ \exp_not:n {#1} }
\ExplSyntaxOff

\begin{document}

The command \verb|\addspaces{helLo9   $\Delta$  @}| should produce:
 
H E L L O 9 $\Delta$ @

Proof:

\addspaces{helLo9   $\Delta$  @}
\end{document}

enter image description here

4
  • I get 4 errors and the output ELLO9∆@ running your exaple. I use TeX live version 2022. The errors are: 1) Undefined control sequence. \text_map_function:nN 2) Argument of \__vebjorn_addspaces_aux:n has an extra }. 3) Runaway argument? ! Paragraph ended before \__vebjorn_addspaces_aux:n was complete. 4) Use of \vebjorn_gobble_space:w doesn't match its definition.
    – Vebjorn
    Oct 30, 2022 at 1:12
  • @Vebjorn update your TeXLive.
    – Skillmon
    Oct 30, 2022 at 7:20
  • I use Overleaf and there is no newer version than TeXLive 2022.
    – Vebjorn
    Oct 30, 2022 at 9:47
  • 1
    @Vebjorn then I'm sorry, the two functions to iterate through text were added to l3text on 2022-08-04. I'm afraid there is no viable solution that works in all engines and respects Unicode in Overleaf yet. Use either an offline installation which supports this, or one of the other answers, but be aware of their limitations.
    – Skillmon
    Oct 30, 2022 at 11:36
3

I know the request was for an expl3 approach. However, the tokcycle package can also provide this behavior using 2e syntax.

\documentclass{article}

\usepackage{tokcycle}
\newcommand\addspaces[1]{%
  \tokencycle
  {\addcytoks{\uppercase{##1}}%
   \tcpeek\z\ifcat A\z\addcytoks{~}\else
   \ifcat 0\z\addcytoks{~}\fi\fi}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}#1\endtokencycle
}

\begin{document}

The command \verb|\addspaces{helLo9   $\Delta$  @}| should produce: \\
 
H E L L O 9 $\Delta$ @

\addspaces{helLo9   $\Delta$  @}

\end{document}

enter image description here

2
  • There is a big difference in the tokcycle behaviour to the expl3 one using the l3text module, though: Try the input \addspaces{helälo} for instance in pdfTeX.
    – Skillmon
    Oct 29, 2022 at 23:00
  • @Skillmon True...using tokcycle, proper output is only achieved with xelatex and/or lualatex for the case you cite. Oct 30, 2022 at 0:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .