
我想知道在expl3
文法(不使用最新的添加內容,比如說 2018 年後的添加內容 [2018 年沒有什麼特別的,這只是一個任意的日期不太近]) 如何測試某事一旦完全展開包含屬於第 11 類或第 12 類的任何字符?
\documentclass[preview = true, varwidth = true]{standalone}
\usepackage{xparse}
\NewExpandableDocumentCommand{\mytest}{m m m}{
% CODE HERE
}
\newcommand{\myempty}{}
\newcommand{\mywhitespace}{ }
\newcommand{\myquad}{\qquad}
\newcommand{\myrelax}{\relax}
\newcommand{\mystring}{ x }
\begin{document}
\begin{tabular}{c}
\mytest{\myempty}{true}{false} \\ % false
\mytest{\mywhitespace}{true}{false} \\ % false
\mytest{\myquad}{true}{false} \\ % false
\mytest{\myrelax}{true}{false} \\ % false
\mytest{\mystring}{true}{false} \\ % true
\end{tabular}
\end{document}
如果我使用\tl_if_blank:nTF
,它不起作用:
\documentclass[preview = true, varwidth = true]{standalone}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_if_blank:nTF{eTF}
\NewExpandableDocumentCommand{\mytest}{m m m}{\tl_if_blank:eTF{#1}{#3}{#2}}
\ExplSyntaxOff
\newcommand{\myempty}{}
\newcommand{\mywhitespace}{ }
\newcommand{\myquad}{\qquad}
\newcommand{\myrelax}{\relax}
\newcommand{\mystring}{ x }
\begin{document}
\begin{tabular}{c}
\mytest{\myempty}{true}{false} \\ % false: OK
\mytest{\mywhitespace}{true}{false} \\ % false: OK
\mytest{\myquad}{true}{false} \\ % true: PROBLEM
\mytest{\myrelax}{true}{false} \\ % true: PROBLEM
\mytest{\mystring}{true}{false} \\ % true: OK
\end{tabular}
\end{document}
答案1
假設我們只能在頂層處理大括號組,而且{E}
相當於E
,那麼我們可以這樣做
\ExplSyntaxOn
\prg_new_conditional:Npnn \vincent_if_blank:n #1 { p , T , F , TF }
{ \exp_args:Ne \__vincent_if_blank:n {#1} }
\cs_new:Npn \__vincent_if_blank:n #1
{
\tl_if_blank:nTF {#1}
{ \prg_return_true: }
{
\tl_map_function:nN {#1} \__vincent_if_blank_aux:n
\prg_return_true:
}
}
\cs_new:Npn \__vincent_if_blank_aux:n #1
{
\tl_if_single_token:nTF {#1}
{
\bool_lazy_or:nnT
{ \token_if_letter_p:N #1 }
{ \token_if_other_p:N #1 }
{ \tl_map_break:n { \use_i:nn \prg_return_false: } }
}
{ \tl_map_break:n { \use_i:nn \prg_return_false: } }
}
\NewExpandableDocumentCommand{\mytest}{m m m}{\vincent_if_blank:nTF{#1}{#3}{#2}}
\ExplSyntaxOff
可以對支撐處理進行細化,但該問題沒有明確定義這方面的期望。
問題變得更加棘手,例如\qquad
擴展為\hskip 2em\relax
,然後確實包含 catcode-11 和 12 標記。如果我們假設此類情況可以被涵蓋
\usepackage{etoolbox}
\robustify\qquad
然後我們可以繼續使用建議的程式碼來獲得所需的輸出。