
条件内でそれぞれを直接チェックせずに、トークンが区切り文字であるかどうか、つまり、、、、、、などを確認(
する)
こと|
は可能ですか?\vert
\langle
\rangle
区切り文字の構造については、ここで詳しく説明されています。https://tex.stackexchange.com/a/296650/213149\delimiter
.コマンドを使用するようです
そこで私の質問は、純粋な TeX または LaTeX3 を使用して、このような区切り文字をある程度汎用的な方法で検出する方法です。
たとえば、引数が区切り文字であるかどうかに応じて、ドキュメント内でまたは\isDelimiter{...}
を印刷するカスタム マクロが必要です。true
false
答え1
これがあなたの問題に役立つかどうかはわかりません\veca
。とにかく…
\documentclass{article}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\isDelimiterTF}{mmm}
{
\antshar_isdel:Nnn #1 { #2 } { #3 }
}
% first check whether #1 is a control sequence
\cs_new:Nn \antshar_isdel:Nnn
{
\token_if_cs:NTF #1
{
\__antshar_isdel_cs:Nnn #1 { #2 } { #3 }
}
{
\__antshar_isdel_char:Nnn #1 { #2 } { #3 }
}
}
% it is a control sequence; first check the two exceptional cases \{ and \}
% which return true; otherwise go on: if the token is not expandable return false
\cs_new:Nn \__antshar_isdel_cs:Nnn
{
\str_case:nnF { #1 }
{
{\{}{#2}
{\}}{#2}
}
{
\token_if_expandable:NTF #1
{
\__antshar_isdel_csexp:Nnn #1 { #2 } { #3 }
}
{
#3
}
}
}
% the token is expandable, access its expansion
\cs_new:Nn \__antshar_isdel_csexp:Nnn
{
\__antshar_isdel_exp:onn { #1 } { #2 } { #3 }
}
% if the expansion begins with \delimiter return true, otherwise false
\cs_new:Nn \__antshar_isdel_exp:nnn
{
\__antshar_isdel_exp_aux:w #1 \q_nil \q_stop { #2 } { #3 }
}
\cs_generate_variant:Nn \__antshar_isdel_exp:nnn { o }
\cs_new:Npn \__antshar_isdel_exp_aux:w #1 #2 \q_stop #3 #4
{
\token_if_eq_meaning:NNTF #1 \delimiter { #3 } { #4 }
}
% when the token is a character, look at its \delcode;
% if positive return true, otherwise false
\cs_new:Nn \__antshar_isdel_char:Nnn
{
\int_compare:nTF { \delcode`#1 > 0 } { #2 } { #3 }
}
\ExplSyntaxOff
\begin{document}
\verb|a|: \isDelimiterTF{a}{T}{F}
\verb|(|: \isDelimiterTF{(}{T}{F}
\verb|]|: \isDelimiterTF{]}{T}{F}
\verb|\langle|: \isDelimiterTF{\langle}{T}{F}
\verb-\|-: \isDelimiterTF{\|}{T}{F}
\verb|\{|: \isDelimiterTF{\{}{T}{F}
\verb|\lbrace|: \isDelimiterTF{\lbrace}{T}{F}
\verb|\mbox|: \isDelimiterTF{\mbox}{T}{F}
\end{document}
制御シーケンスが区切り文字であるかどうかをどのように認識するのでしょうか? 最初のレベルの展開は で始まる\delimiter
か、文字の場合は\delcode
正の数である必要があります。
したがって、文字のチェックは明らかです。制御シーケンスの場合、まずそれが拡張可能かどうかを確認する必要があります。ただし、やや特殊な にも注意を払う必要がある\{
ため\}
、これらのケースは自動的に解決されます。
調べている制御シーケンスが展開可能でない場合、それは区切り文字ではありません(フォントパッケージによっては\{
、や などの他の例外\}
を追加する必要があるかもしれません)。展開可能な場合は、呼び出して最初のレベルの展開を調べます。
\__antshar_isdel_exp:onn { #1 } { #2 } { #3 }
引数o
の型は必要な1レベルの拡張を行います。これは次のようになります。
\__antshar_isdel_exp_aux:w #1 \q_nil \q_stop { #2 } { #3 }
の定義\__antshar_isdel_exp_aux:w
は
\cs_new:Npn \__antshar_isdel_exp_aux:w #1 #2 \q_stop #3 #4
{
\token_if_eq_meaning:NNTF #1 \delimiter { #3 } { #4 }
}
したがって、検査している制御シーケンスの展開の最初のトークンは になり#1
、 までの残りは\q_nil
になります#2
。残りの、つまり、 と は、#3
の#4
真と偽のテキストです\isDelimiterTF
。最初の引数は区切られていないため、入力ストリームの最初のトークンが引数として取得されます。2 番目の引数は、TeX が を見つけた時点で終了します\q_stop
。
奇妙なのは\q_nil
、試してみると\isDelimiter{\empty}{T}{F}
展開に何もなくなるからです。この場合、\q_nil
は とみなされ#1
、#2
は空です。しかし、 は\q_nil
ではないので\delimiter
、すべてが通ります。