我想测试一个角色,而不考虑其当前的 catcode(即是否处于活动状态)。这与如何测试一个字符,而不考虑 catcode?,但是我不能使用,\pdf@strcmp
因为我可能有“半”宏(例如,\emph
还没有参数)。
\documentclass{minimal}
\usepackage[czech]{babel}% makes - active
\begin{document}
\def\aaa{%
\if a\noexpand\lookahead%
(the letter a)%
\else\if -\noexpand\lookahead%
(a minus)%
\else%
(special treatment)
\fi\fi%
}
\futurelet\lookahead\aaa -
\futurelet\lookahead\aaa a
\futurelet\lookahead\aaa \emph{c}
\end{document}
对我来说,更改前后的 catcode 并不是真正的解决方案,因为仅通过未来观察事物就会丢失原来的含义。
答案1
\ifx
可用于直接比较标记。以下示例将标记存储在宏\aaaHyphenActive
和中\aaaHyphenOther
。在比较中,连字符标记通过解包\expandafter
。
\documentclass{minimal}
\usepackage[czech]{babel}% makes - active
\begingroup
\catcode`\-=\active
\gdef\aaaHyphenActive{-}%
\catcode`\-=12 %
\gdef\aaaHyphenOther{-}%
\endgroup
\begin{document}
\def\aaa{%
\if a\noexpand\lookahead
(the letter a)%
\else\expandafter\ifx\aaaHyphenActive\lookahead
(an active minus)%
\else\expandafter\ifx\aaaHyphenOther\lookahead
(a minus)%
\else
(special treatment)
\fi\fi\fi
}
\futurelet\lookahead\aaa -
\futurelet\lookahead\aaa a
\futurelet\lookahead\aaa \emph{c}
\end{document}
答案2
您可以使用expl3
。这是一个粗略的例子。
\documentclass{article}
\usepackage[czech]{babel}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\dosomething}{m}
{
\martin_do_something:n {#1}
}
\cs_new_protected:Npn \martin_do_something:n #1
{
\peek_charcode:NTF a % test the first token against "a"
{
(the~letter~a)
}
{
\peek_charcode:NTF - % test the first token against "-" (charcode only)
{
(a~hyphen)
}
{
(special~treatment)
}
}
#1
}
\ExplSyntaxOff
\begin{document}
\dosomething{abc}
\dosomething{-abc}
\dosomething{\emph{abc}}
\the\catcode`- % check that the hyphen was active
\catcode`-=12 % make it non active and try again
\dosomething{-abc}
\end{document}