How to capitalise every letter in odd position as in memes?

A LaTeX3 version, adapted from egreg's answer here:

\documentclass{article}
\usepackage{xparse}
\usepackage{expl3}

\ExplSyntaxOn
\NewDocumentCommand{\markletters}{m}
 {
  \int_zero:N \l_tmpa_int
  \tl_set:Nn \l_tmpa_tl { #1 }
  % replace spaces with something different
  \tl_replace_all:Nnn \l_tmpa_tl { ~ } { \c_space_tl }
  \tl_map_inline:Nn \l_tmpa_tl
   {
    \tl_if_blank:eTF { ##1 }
     { ~ } % don't advance the counter and issue a space
     {
      \int_if_odd:nTF { \l_tmpa_int } { \tex_lowercase:D } { \tex_uppercase:D } { ##1 }
      \int_incr:N \l_tmpa_int
     }
   }
 }
\prg_generate_conditional_variant:Nnn \tl_if_blank:n { e } { T,F,TF,p }
\ExplSyntaxOff

\begin{document}

\markletters{something like this}

\end{document}

enter image description here


A bit shorter with the xstring package:

\documentclass{article}
\usepackage{xstring}
\newif\ifupper\uppertrue
\newcommand{\mixedcase}[1]{%
\StrSplit{#1}{1}{\currentchr}{\tailchr}%
\ifupper\MakeUppercase{\currentchr}\upperfalse\else\MakeLowercase{\currentchr}\uppertrue\fi%
\IfStrEq{\tailchr}{}{\uppertrue}{\mixedcase{\tailchr}}%
}
\begin{document}
\mixedcase{something like this}
\end{document}

Same output as Andrews answer.

Explanation: split the string at the first character, make this character upper or lower case depending on a toggle (\ifupper), switch the toggle, check if there are characters left in the string (the second part of the split is not empty), if yes call the command recursively, if no then reset the toggle for the next time and stop.


Here's a slightly different LaTeX3 implementation using regular expressions. A space character is treated as a letter so that the two lines

\CaPiTaLiSe{Something like this}
\CaPiTaLiSe{Somethinglikethis}

produce slightly different capitalisations:

enter image description here

Here is the code:

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \l_evan_letters_seq
\cs_new_protected_nopar:Nn \evan_make_upper:n {
  \str_uppercase:n {#1}
  \cs_set_eq:NN \evan_capitalise:n \evan_make_lower:n
}
\cs_new_protected_nopar:Nn \evan_make_lower:n {
  \str_lowercase:n {#1}
  \cs_set_eq:NN \evan_capitalise:n \evan_make_upper:n
}
\NewDocumentCommand\CaPiTaLiSe{m}{
  \regex_split:nnN {} {#1} \l_evan_letters_seq
  \cs_set_eq:NN \evan_capitalise:n \evan_make_upper:n
  \seq_map_inline:Nn \l_evan_letters_seq {\evan_capitalise:n {##1} }
}
\ExplSyntaxOff
\begin{document}

  \CaPiTaLiSe{Something like this}

  \CaPiTaLiSe{Somethinglikethis}

\end{document}

Rather than incrementing a counter I have a dummy macro \evan_capitalise:n that swaps between choosing upper and lower case.

Tags:

Fonts