Latex-提取子字符串/忽略字符

Latex-提取子字符串/忽略字符,latex,parsing,token,tex,if-statement,Latex,Parsing,Token,Tex,If Statement,我有以下问题。我定义了一个宏,\func,如下所示 \newcommand{\func}[1]{% do something with #1 X #1 Y } 现在我想定义另一个宏 \newcommand{\MyFunc}[1]{ % parse #1 and if it contains "\func{....}", ignore all except this part % otherwise ignore #1 } 有人能告诉我如何实现\MyFunc 以下是应该发生的事情:

我有以下问题。我定义了一个宏,
\func
,如下所示

\newcommand{\func}[1]{% do something with #1  
X #1 Y
}
现在我想定义另一个宏

\newcommand{\MyFunc}[1]{  
% parse #1 and if it contains "\func{....}", ignore all except this part
% otherwise ignore #1 
}
有人能告诉我如何实现
\MyFunc

以下是应该发生的事情:

\MyFunc{123abcdefg}              % should print nothing
\MyFunc{123\func{abcd}efg}       % should print X abcd Y

我只能更改
\MyFunc
的代码<代码>\func应保持原样。

这可以通过标准的LaTeX编程完成。比如:

\documentclass{article} \newcommand\func[1]{X #1 Y} \makeatletter \newcommand\MyFunc[1]{% \in@{\func}{#1}% \ifin@ \ignore@all@but@func#1\@nil \fi } \def\ignore@all@but@func#1\func#2#3\@nil{\func{#2}} \makeatother \begin{document} [\MyFunc{123abcdefg}] % should print nothing [\MyFunc{123\func{abcd}efg}] % should print X abcd Y \end{document} \documentclass{article} \纽科曼\func[1]{X#1y} \马克特莱特 \newcommand\MyFunc[1]{% \在{\func}{1}%中 \伊芬@ \ignore@all@but@func#1\@零 \fi } \def\ignore@all@but@func#1\func#2#3\@nil{\func{2} \Maketother \开始{document} [\MyFunc{123abcdefg}]%不应打印任何内容 [\MyFunc{123\func{abcd}efg}]%应打印X abcd Y \结束{document}
确切地说,你为什么要这样做?我在算法中有一个标题,定义为标题{blah blah\func{algorithm_name}blah blah blah blah blah}。当我创建ToC时,我只想提取算法名称(以及由于\func而产生的格式),忽略其余部分。试试
\caption[\func{algorithm_name}}{blah blah\func{algorithm algorithm u name}blah blah}
。不,这太不相关了。问题是文档已经用数千个标题编写。调用\caption时,我可以调用另一个例程(在上面的示例中,\MyFunc)但是我不能为每个算法重写实际的标题。我不能编辑实际文档的原因是我没有文档某些部分的编辑权限。我可以编辑根文件,其中
MyFunc
func
在此处定义并工作。有其他方法可以获得结果,但这应该是正确的最简单的。谢谢!!这正是我要找的:)有没有学习(La)TeX编程的好资源?我应该提到的是,我必须将代码中的
\newcommand
更改为
\DeclareRobustCommand
,然后一切都完美无瑕!(这当然与实际的文档有关,而不是上面的玩具示例)@amit:不,没有什么好地方可以学习这些东西。我建议您阅读软件包源代码和最新的X2E源代码(
texdoc source2e
),并提出许多问题:)Fwiw,我不知道缺少\makeatother有什么问题,但您是对的。省略它是我的一个坏习惯。