10 在xparse定义的`\foo`命令中界定局部作用域后声明局部变量 `\l_course_sep_tl`,为什么重复使用`\foo`命令会报错?

发布于 2024-09-21 21:10:52

在xparse定义的\foo命令中用group_begin:group_end:界定局部作用域后声明局部变量\l_course_sep_tl,为什么重复使用\foo命令会报错提示变量已经定义?

The following is MWE:

\documentclass{article}

\begin{document}

\ExplSyntaxOn
\NewDocumentCommand{\foo}{}{
    \group_begin:
    \tl_new:N \l_course_sep_tl
    \group_end:
}
\ExplSyntaxOff

\foo,\foo

\end{document}
Control sequence \l_course_sep_tl already defined.

Additional, 我却可以不声明直接\tl_set:N \l_course_sep_tl,但总觉得怪怪的.

查看更多

关注者
0
被浏览
241
W5
W5 2024-09-21
这家伙很懒,什么也没写!

参见texdoc interface3

image.png

这是expl3的规则。new是全局的。不能重复new。
这个错误与 https://ask.latexstudio.net/ask/question/17353.html 回答中出现的错误一致。哈哈

我的理解:

  • group_begin:group_end:之间的set是局部的。所有set的变量都不会传到group_begin:group_end:的外部。
  • gset,new在任何地方出现都是全局的。不同包new相同变量也会出问题。所以new变量时要注意规范,把包名也加上去。
1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览