
我正在嘗試創建一個命令來\everymath
格式化某些指定的字元(自動放入v
等\mathbf
)。這部分我實際上已經弄清楚了,但給我帶來問題的是處理本身。它似乎在數學模式下分隔命令,要么顯示不正確,要么完全破壞。
這是它的最小版本,只是逐步執行並且不改變任何內容(理論上):
\ExplSyntaxOn
\NewDocumentCommand{\mformat}{+m}{%
\exp_after:wN #1 \mode_if_math:TF { \mformat } { }
}
\ExplSyntaxOff
它的使用方式如下:
\everymath{\mformat}
以下是它如何損壞的幾個範例:
\(\vec{v}\)
\(\mathbf{v}\) % Just gives "Missing } inserted." error
有想法該怎麼解決這個嗎?
編輯:我注意到一些新的東西;如果我用大括號將問題命令括起來(例如使用mathbf
,\({\mathbf{v}}\)
工作正常),它們會突然完美地工作。不知道為什麼會發生這種情況。
我已將egreg的答案標記為解決方案,因為它回答了我提出的問題,但對於後代來說,這是我應該提出的問題的解決方案(基於PhelypeOleinik的評論)
\ExplSyntaxOn
% Used to keep track of already active characters
\tl_new:N \g__mformat_mathactive_tl
% Used to figure out which characters need to be made normal again
\tl_new:N \l__mformat_remove_mathactive_tl
% Used to keep track of added characters from *this* iteration
\tl_new:N \l__mformat_used_tl
% Using https://tex.stackexchange.com/a/611898/261875
% and https://tex.stackexchange.com/a/299805/261875
\NewDocumentCommand{\mformat}{m}{
% By default remove all previous active characters
\tl_set_eq:NN \l__mformat_remove_mathactive_tl \g__mformat_mathactive_tl
\tl_set:Nn \l__mformat_used_tl {}
\tl_if_empty:nTF { #1 } {} {
% Parse the formatting
\cs_set:Npn \__mformat_parse:w ##1[##2]##3\relax {
% Process each character in the set
\tl_map_inline:nn { ##2 } {
\tl_if_in:NnTF \g__mformat_mathactive_tl { ####1 } {
% If this character is already active, keep it active
\tl_remove_once:Nn \l__mformat_remove_mathactive_tl { ####1 }
% Check if the character has been used this iteration
\tl_if_in:NnTF \l__mformat_used_tl {####1} {
% Helper needed to have something expandable once
\cs_set_eq:Nc \__mformat_letter_helper:
{ __mformat_letter_new_####1: }
% Add a formatting option to the letter
\cs_set:cx { __mformat_letter_new_####1: } {
\exp_not:N ##1 { \exp_not:o \__mformat_letter_helper: }
}
} {
% Record that this has been used
\tl_put_right:Nn \l__mformat_used_tl { ####1 }
% Define what the letter will now resolve to
\cs_set:cx { __mformat_letter_new_####1: } {
\exp_not:N ##1 {\mathchar\use:c { __mformat_mathcode_####1: }}
}
}
\char_gset_active_eq:nc { `####1 } { __mformat_letter_new_####1: }
} {
% Record that this is now an active character
\tl_gput_right:Nn \g__mformat_mathactive_tl { ####1 }
% Record that this has been used
\tl_put_right:Nn \l__mformat_used_tl { ####1 }
% Record the normal character so it can be used later
\cs_new:cx { __mformat_mathcode_####1: }
{ \the\mathcode`####1 }
% Define what the letter will now resolve to
\cs_new:cx { __mformat_letter_new_####1: } {
\exp_not:N ##1 {\mathchar\use:c { __mformat_mathcode_####1: }}
}
\char_gset_active_eq:nc { `####1 } { __mformat_letter_new_####1: }
% Set the character to be active in math mode
\char_set_mathcode:nn { `####1 } { "8000 }
}
}
% If there's no more character sets, finish, otherwise recurse
\tl_if_empty:nTF { ##3 } { } { \__mformat_parse:w ##3\relax }
}
% Begin recursive parsing
\__mformat_parse:w #1\relax
}
% \tl_show:N \l__mformat_remove_mathactive_tl
% Remove the active status from the characters that need it
\tl_map_inline:Nn \l__mformat_remove_mathactive_tl {
\tl_gremove_once:Nn \g__mformat_mathactive_tl {##1}
% Reset the math code
\char_set_mathcode:nn { `##1 } { \use:c { __mformat_mathcode_##1: } }
% Deregister functions
\cs_undefine:c { __mformat_letter_new_##1: }
\cs_undefine:c { __mformat_mathcode_##1: }
}
}
\NewDocumentCommand{\std}{m}{ \mathchar\use:c { __mformat_mathcode_#1: } }
\ExplSyntaxOff
其用法如下
\mformat{\mathbb[R]\mathbf[vw]}
答案1
這看起來非常像 XY 問題。你說
並沒有改變任何東西(理論上)
抱歉,事實並非如此。第一級擴展
\mode_if_math:TF { \mformat } { }
是
\if_mode_math: \__prg_TF_true:w \fi: \use_ii:nn {\mformat}{}
如果你有$\vec{v}$
你獲得
\vec\if_mode_math: \__prg_TF_true:w \fi: \use_ii:nn {\mformat}{}
所以 的論證\vec
是\if_mode_math:
。例子:
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\mformat}{+m}{%
\exp_after:wN #1 \mode_if_math:TF { \mformat } { }
}
\ExplSyntaxOff
\everymath{\mformat}
\renewcommand{\vec}[1]{\showtokens{#1}}
\begin{document}
$\vec{v}$
\end{document}
在控制台上我得到
No file mform.aux.
> \if_mode_math: .
\vec #1->\showtokens {#1}
l.13 $\vec
如果有的話會更好\exp_last_unbraced:Nf
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\mformat}{+m}{%
\exp_last_unbraced:Nf #1 { \mode_if_math:TF { \mformat } { } }
}
\ExplSyntaxOff
\everymath{\mformat}
\renewcommand{\vec}[1]{\showtokens{#1}}
\begin{document}
$\vec{v}$
\end{document}
控制台列印的地方
> v.
\vec #1->\showtokens {#1}
但這也不是很好:用類似的東西
\mathrm{x}
結果是一長串錯誤。即使你透過做一長串案例來修復它們,例如
\mathrm{abc}
會變成
\exp_last_unbraced:Nf abc { \mode_if_math:TF { \mformat } { } }
這絕對不是你想看到的。