字符串替换后,命令不会扩展为其他命令

字符串替换后,命令不会扩展为其他命令

MWE 应该解释这一切(使用 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}

我怎样才能将命令保存在替换字符串中,\ctext以便从中生成列表\hello

答案1

它不能工作,因为只在环境内激活easylist,但是当你进行替换时,角色是§easylist§不是活跃的,处于easylist环境之外。

您必须在§活动的设置中进行替换,并注意不要扩展它:

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

相反,你可以\noexpandarg这样\expandafter

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

相关内容