\aftergroup 标记列表

\aftergroup 标记列表

\aftergroup执行当前组结束后的下一个标记。是否有一个变体,\Vaftergroup{\some\tokens\a\b}可以将其放在\some\tokens\a\b当前组结束后?当然可以\aftergroup\some\aftergroup\tokens\aftergroup\a\aftergroup\b,但是当列表较长时,它会变得混乱。

这是出于一个有效但丑陋的答案

答案1

与赫伯特的回答大致相同,同样的想法的“预构建”版本由etextools包装:见\AfterGroup。我猜这个\aftergroup很少用,所以需求不大:-)

答案2

不知道这是否有帮助。

\documentclass{article}

\def\ta{Hello}
\def\tb{ World. }
\def\tc{How are you?}
%\def\Tokens{\ta\tb\tc}

\begin{document}

\begingroup
\gdef\Tokens{\ta\tb\tc}% or like above with def
\aftergroup\Tokens
\let\ua=\tc
\let\ub=\tb
\let\uc=\ta
\ua\ub\uc---\par
\endgroup

\end{document}

答案3

把这句话写在你的序言里

% Usage
% \Aftergroup{ balanced text }
% \Aftergroup token text token 
%                    where text is any row of tokens and token is a character that is not in text (compare \verb)
%
% Example:
% \Aftergroup {\aaa{bbb}} and \Aftergroup @\aaa{bbb}@ put \aaa{bbb} after the endgroup
% whereas \Aftergroup }\aaa{bbb}} and \Aftergroup \egroup\aaa{bbb}} put \aaa{bbb after the second "}"
%
% For the unbalanced text a problem is that we can not distinguish "{" and "\bgroup" (or 
% "}"and "\egroup". Hence by
% Aftergroup @\bgroup \egroup@ the tokens inserted after the group are "{}" and by
% Aftergroup \bgroup \egroup @\bgroup the tokens "}@ are put after the group.  

\makeatletter

\def\Aftergroup{\bgroup
    \let\Bgroup={\let\bgroup}%give \bgroup a different meaning, } to balance
    \@ifnextchar\Bgroup
        {\egroup\@AGBalancedText}
        {\@ifnextchar\egroup
            {\egroup\def\@AGdelimiter{\egroup}\afterassignment\@AGDelNextToken\let\@let@token=}
            {\egroup\@AGDelimited}}
}

\def\@AGDelimited#1{\def\@AGdelimiter{#1}\@AGDelNextToken}

\def\@AGDelNextToken{%
    \expandafter\@ifnextchar\@AGdelimiter{\let\@let@token=}%end
       {\@ifnextchar\bgroup
        {\aftergroup{\iffalse}\fi\afterassignment\@AGDelNextToken\let\@let@token=}
        {\@ifnextchar\egroup
            {\iffalse{\fi\aftergroup}\afterassignment\@AGDelNextToken\let\@let@token=}
            {\@AGDeltoken}
}}}

\def\@AGDeltoken#1{\aftergroup#1\@AGDelNextToken}

\def\@AGBalancedText#1{\@AGBalNextToken#1{\@AGBalNextToken}}

\def\@AGBalNextToken#1#{\@AGBalNotOpen#1\@AGBalNotOpen\@AGBalClose}

\def\@AGBalNotOpen#1{\ifx\@AGBalNotOpen#1\else\aftergroup#1\expandafter\@AGBalNotOpen\fi}

\def\@AGBalClose#1{%
    \ifx\@AGBalNextToken#1\relax%
    \else\ifx\@AGBalClose#1\relax\iffalse{\else\aftergroup}\fi
            \expandafter\expandafter\expandafter\@AGBalNextToken%
        \else\iftrue\aftergroup{\else}\fi\@AGBalNextToken#1{\@AGBalClose}%
        \fi
    \fi
}

\makeatother

下面的代码

{\Aftergroup }\def\aaa{123}}\relax}\show\aaa
{\Aftergroup {\def\aaa{123\bgroup\egroup}}}\show\aaa
{\Aftergroup @\def\aaa{123\bgroup\egroup}@}\show\aaa

然后给出

 > \aaa=macro:
 ->123\relax .
 l.102 {\Aftergroup }\def\aaa{123}}\relax}\show\aaa

 ? 
 > \aaa=macro:
 ->123\bgroup \egroup .
 l.103 ...p {\def\aaa{123\bgroup\egroup}}}\show\aaa

 ? 
 >\aaa=macro:
 ->123{}.
 l.104 ...p @\def\aaa{123\bgroup\egroup}@}\show\aaa

在 TeX 的输出窗口中。

答案4

下面是一个替代方案。\Tokens仍然是本地的。

\documentclass{article}

\def\ta{Hello}
\def\tb{ World. }
\def\tc{How are you?}
%\def\Tokens{\ta\tb\tc}

\begin{document}

\begingroup
\def\Tokens{\ta\tb\tc}% or like above with def
\let\ua=\tc
\let\ub=\tb
\let\uc=\ta
\ua\ub\uc---\par
\expandafter\endgroup\Tokens

\show\Tokens
\end{document}

相关内容