expl3: lowercase a token list
Update 2020-01-14
The LaTeX kernel now makes \\
robust out-of-the-box. When used with the latest expl3
function \text_lowercase:n
, this works with no additional adjustments. The comment in the original answer about strings versus text remains valid: you are case-changing text.
Original answer
The function \str_lowercase:n
is for making strings lower case, and is for programmatic data not for text. You want \text_lowercase:n
. The only issue is that \\
is not engine robust and the implementation of \text_lowercase:n
expects 'text' to be either character tokens, things which expand to character tokens or engine-robust commands. We can solve that by locally making \\
engine-robust:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \LowerCase { m }
{
\group_begin:
\cs_set_protected:Npx \\ { \exp_not:o \\ }
\text_lowercase:n {#1}
\group_end:
}
\ExplSyntaxOff
\begin{document}
\LowerCase{This is my\\Text}
\end{document}
If you make \\
robust globally then everything stays expandable
\documentclass{article}
\usepackage{etoolbox}
\robustify\\
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand \LowerCase { m }
{ \text_lowercase:n {#1} }
\ExplSyntaxOff
\begin{document}
\LowerCase{This is my\\Text}
\end{document}
Note that there is no need to store #1
in a token list variable in either case.