0

假设每个 [ 都有一个匹配的 ] 并且 [ 和 ] 之间的任何大括号是平衡的,我如何编写一个 (La)TeX 命令来替换所有 [ 用 { 和所有 ] 用 }?它需要能够处理嵌套括号。

例如,我希望能够编写\mynewcommand一个\mynewcommand{{[[{1}{2}][{3}{4}]]}}\mycommand{{{{{1}{2}}{{3}{4}}}}}.

4

1 回答 1

2

可能最简单的方法是使用 e-TeX 和 \scantokens

\newcommand*\mycommand[1]{%
  \begingroup
    \everyeof{\noexpand}%
    \endlinechar=-1\relax
    \catcode`\[=1\relax
    \catcode`\]=2\relax
    \edef\temp{\scantokens{#1}}%
  \expandafter\endgroup
  \expandafter\def\expandafter\temp\expandafter{\temp}%
}

这将使用#1 中的材料定义 \temp,但每个“[”...“]”对都变成一个 TeX 大括号组(“{”...“}”)。然后,您可以使用 \temp 做任何您想做的事情。正如我所说,这需要 e-TeX,它在所有现代 TeX 系统中都可用。

于 2010-06-27T07:40:13.907 回答