这是进食命令\e
(我从那里获得了我正尝试调整以使其工作的代码\ep
。
\e
e
如果是,则将下一个标记重写为\f
,否则保持不变。
\ep
应该做同样的事情,但如果下一个标记是\h
,那么它应该跳过它,然后尝试重写下一个标记。我唯一能做的就是重写\ep\h\f
→ ,\h\ep\f
而我想要的是\ep\h\f
→ \h\f\ep
。
\epb
通过添加额外的参数设法实现\epb\h\f
→ ,但是它失败了,因为→ (或看起来如此)而不是→ 。\h\f\epb
\epb\h{\f\f}
\h\f\f\epb
\epb\h{\f\f}
\h{\f\f}\epb
有没有什么办法可以解决这个问题?
预先感谢您的帮助。
\makeatletter
\newcommand{\f}{f}% food
\newcommand{\s}{}% stop eating
\newcommand{\h}[1]{h}% hide food
\newcommand{\e}[1]{% eat
\ifx\f#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{e}%
{#1}%
}
\newcommand{\ep}[1]{% eat past (hidden food)
\ifx\f#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{e}%
{%
\ifx\h#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#1\ep}%
{#1}%
}%
}
\newcommand{\epb}[2]{% eat past (hidden food) bis
\ifx\f#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{e#2}%
{%
\ifx\h#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#1#2\ep}%
{#1#2}%
}%
}
\makeatother
% f represents food
% e represents eaten food
% [v] means it works as expected
% [x] means it doesn't
% [v] Eat food
[\e\f]% [e]
% [v] Stop eating
[\e\s\f]% [f]
% [v] Hide food
[\h\f]% [h]
% [v] Eat hidden food
[\e\h\f]% [h]
% [v] Eat past hidden food
[\e\h\f\f]% hf
% [x] Eat past hidden food
[\ep\h\f\f]% hff
% I expected he
% [x] Eat past hidden food
[\epb\h\f\f]% he
% [x] Eat past hidden food
[\epb\h{\f\f}\f]% hfe
% I expected he
答案1
使用 expl3,但完全可以使用普通的 (La)TeX 宏来实现。
\documentclass{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand \ep { m }
{ \xavierm_eat:N #1 }
\cs_new:Npn \xavierm_eat:N #1
{
\str_case:nnF { #1 }
{
{ \f } { \xavierm_eat_f: }
{ \h } { \xavierm_eat_h:n }
}
{ #1 }
}
\cs_new:Npn \xavierm_eat_f: { e }
\cs_new:Npn \xavierm_eat_h:n #1 { \h { #1 } \ep }
\ExplSyntaxOff
\newcommand*\f{f}% food
\newcommand*\s{}% stop eating
\newcommand*\h[1]{h}% hide food
\begin{document}
% f represents food
% e represents eaten food
% [v] means it works as expected
% [x] means it doesn't
% [x] Eat past hidden food
[\ep\h\f\f]% hff
% I expected he
% [x] Eat past hidden food
[\ep\h\f\f]% he
% [x] Eat past hidden food
[\ep\h{\f\f}\f]% hfe
% I expected he
\end{document}