我的情况比较特殊。我想将命令输出的某些部分“标记”为前缀或后缀。例如:
\newcommand{\L}[1]{L\postfix{_i}}
然后可以做这样的事情:
\newcommand{\est}[1]{\prefixof{#1}\hat{\mainbodyof{#1}}\postfixof{#1}}
理想情况下,这应该适用于普通文本,例如\est{\lambda}
应该扩展为\hat{\lambda}
。此外,\L
应该扩展为L_i
,并且\est{\L}
应该扩展为\hat{L}_i
。
我知道使用 TeX 可以做到这一点(它是图灵完备的),但我不知道从哪里开始,或者除了 之外还要在问题中添加什么标签macros
。
在这种情况下,@DavidCarlisle 的答案似乎不起作用:
\documentclass{article}
\usepackage{etoolbox}
\newcommand\zzfix[3]{#1#2#3}%pre-main-post
\def\prefixof#1{\expandafter\zzpre#1\relax{#1}}
\def\postfixof#1{\expandafter\zzpost#1\relax{#1}}
\def\mainbodyof#1{\expandafter\zzmain#1\relax{#1}}
\def\zzpre#1{\ifx\zzfix#1\expandafter\oneoffive\else\expandafter\gobblepastrelax\fi}
\def\zzpost#1{\ifx\zzfix#1\expandafter\threeoffive\else\expandafter\gobblepastrelax\fi}
\def\zzmain#1{\ifx\zzfix#1\expandafter\twooffive\else\expandafter\afterrelax\fi}
\def\oneoffive#1#2#3\relax#4{#1}
\def\twooffive#1#2#3\relax#4{#2}
\def\threeoffive#1#2#3\relax#4{#3}
\def\afterrelax#1\relax#2{#2}
\def\gobblepastrelax#1\relax#2{}
\newcommand{\queueState}[1][]{\zzfix{}{\ell}{\ifstrempty{#1}{}{_{#1}}}}
\newcommand{\baseQuantity}[1]{\prefixof{#1}\bar{\mainbodyof{#1}}\postfixof{#1}}
\begin{document}
But this does not work: $\baseQuantity{\queueState[1:N]}$
\end{document}
也许也可以让它发挥作用?
\show\queueState
输出:
> \queueState=macro:
->\@protected@testopt \queueState \\queueState {}.
答案1
\documentclass{article}
\newcommand\zzfix[3]{#1#2#3}%pre-main-post
\renewcommand{\L}{\zzfix{}{L}{_i}}
\newcommand{\est}[1]{\prefixof{#1}\hat{\mainbodyof{#1}}\postfixof{#1}}
\def\prefixof#1{\expandafter\zzpre#1\relax{#1}}
\def\postfixof#1{\expandafter\zzpost#1\relax{#1}}
\def\mainbodyof#1{\expandafter\zzmain#1\relax{#1}}
\def\zzpre#1{\ifx\zzfix#1\expandafter\oneoffive\else\expandafter\gobblepastrelax\fi}
\def\zzpost#1{\ifx\zzfix#1\expandafter\threeoffive\else\expandafter\gobblepastrelax\fi}
\def\zzmain#1{\ifx\zzfix#1\expandafter\twooffive\else\expandafter\afterrelax\fi}
\def\oneoffive#1#2#3\relax#4{#1}
\def\twooffive#1#2#3\relax#4{#2}
\def\threeoffive#1#2#3\relax#4{#3}
\def\afterrelax#1\relax#2{#2}
\def\gobblepastrelax#1\relax#2{}
\begin{document}
Ideally, this should work on normal text, e.g. $\est{\lambda}$ should
expand to $\hat{\lambda}$. Also, $\L$ should expand to $L_i$, and $\est{\L}$
should expand to $\hat{L}_i$.
\end{document}