# Command doesn't expand into other commands after string substitution

The MWE should explain it all (use LuaLaTeX):

\documentclass[11pt]{memoir}

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{xstring}
\usepackage{easylist}
\begin{document}
\newcommand{\hello}{ hello I am a mouse}
\StrSubstitute{\hello}{ }{ § }[\ctext]

This is to show that the substitute works:
\ctext

\vfill

I can also use this text to make a list:
\begin{easylist}
§ hello § I § am § a § mouse
\end{easylist}

\vfill

But this does not work:
\begin{easylist}
\ctext
\end{easylist}

\vfill

or this:
\edef\hellot{\ctext}
\begin{easylist}
\hellot
\end{easylist}
\end{document}

How can I keep commands in the substituted string, \ctext, so that the list is generated from \hello?

• No need to load the fontenc package when using LuaLaTeX. – Mico Mar 1 '15 at 5:35

It can't work, because easylist makes § active only inside the easylist environment, but when you do the substitution the character § is not active, being outside an easylist environment.

You have to make the substitution in a setting where § is active and be careful not to expand it:

\documentclass[11pt]{memoir}

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{xstring}
\usepackage{easylist}
\begin{document}

\begingroup\catcode§=\active
\newcommand{\hello}{ hello I am a mouse}
\noexpandarg
\expandafter\StrSubstitute\expandafter{\hello}{ }{ § }[\temp]
\global\let\ctext\temp
\endgroup

This is to show that the substitute works:
\texttt{\meaning\ctext}

\bigskip

I can also use this text to make a list:
\begin{easylist}
§ hello § I § am § a § mouse
\end{easylist}

\bigskip

Also this works:
\begin{easylist}
\ctext
\end{easylist}

\end{document}

Instead of \noexpandarg and \expandafter you can do

\begingroup\catcode§=\active\let§\relax
\newcommand{\hello}{ hello I am a mouse}
\StrSubstitute{\hello}{ }{ § }[\temp]
\global\let\ctext\temp
\endgroup
• Is there a way to ... activate it? Or is there an equivalent way to do this using enumerate and \csname/\endcsname? (I tried and just got bizarre TeXy messages from \StrSubstitute.) – Simon Kuang Feb 28 '15 at 23:20
• @SimonKuang Yes, I added it. – egreg Feb 28 '15 at 23:22
• Thank you so much! Now, just for the sake of my learning TeX, is there a simpler or more idiomatic way to build a list before printing it? – Simon Kuang Feb 28 '15 at 23:48
• @SimonKuang It much depends on what you want to do. – egreg Feb 28 '15 at 23:52