
如何正確地將底線傳遞給 \newcommand ?建議解決方案是使用零參數命令來更改 , 的目錄代碼_
,然後再擴展到實際命令。
這與我想要使用的原因相反 \NewDocumentCommand
,因為它有點尷尬,而且還混淆了參數類型。
有沒有一種方法可以傳遞包含下劃線(順便說一句,文件名)的字串,而不離開我的 expl3/xparse/ \NewDocumentCommand
bubble 的舒適感?
我不想使用v
爭論;它的功能應該像普通的宏,\includechapter{foo_bar}
而不是像\includechapter!foo_bar!
.
我目前的程式碼:
%%% Chapter Inclusion Macros
\RequirePackage{expl3}
\ExplSyntaxOn
% Command to include chapter files, if
% either the exclusive chapter list is empty,
% or said chapter is in there
\cs_set:Npn \cel_includechapter:n #1 {
% Check whether list is empty
\clist_if_empty:NTF
\g_cel_enabled_clist % which list
{\include{#1/#1}} % if empty, just include
{ % else
% check whether argument in list of enabled chapters
\clist_if_in:NnTF
\g_cel_enabled_clist % in which list
{#1} % which element to look for
{\include{#1/#1}} % if in there
{\chapter{#1~(currently~disabled)}} %if not in there
}
}
% user-facing command \includechapter
% includes chaptername/chaptername
% if enabled
\NewDocumentCommand{\includechapter}{m}{
\cel_includechapter:n{#1}
}
\NewDocumentCommand{\enableChapter}{m}{
\clist_put_right:Nn \g_cel_enabled_clist {#1}
}
\ExplSyntaxOff
到達時建置會中斷
\includechapter{foo_bar}
和
! Missing $ inserted.
<inserted text>
$
l.147 \includechapter{foo_bar}
答案1
問題是你說的地方
{\chapter{#1~(currently~disabled)}} %if not in there
因為檔案名稱中的底線會觸發數學模式。
解決辦法:改為
{\chapter{\tl_to_str:n {#1}~(currently~disabled)}} %if not in there
答案2
這種情況下的解決方案是使用v
-type 參數:
\NewDocumentCommand{\includechapter}{v}{
\cel_includechapter:n{#1}
}
\NewDocumentCommand{\enableChapter}{v}{
\clist_put_right:Nn \g_cel_enabled_clist {#1}
}
這仍然需要更多修復,因為如果傳遞的參數包含下劃線,則需要先對其進行處理,然後才能進行排版。
幸運的是,我不想使用這個巨集作為另一個函數的參數(usrguide3.pdf):
v
:以類似 LATEX 2ε 指令的參數的方式,在後面的字元與其下一個出現的字元之間「逐字」讀取參數\verb
。因此,v
在兩個相同字元之間讀取 -type 參數,該字元不能是%
、\
、#
、{
或}
中的任何一個␣
。逐字參數也可以括在大括號{
和之間}
。當帶有逐字參數的命令出現在另一個函數的參數中時,它將產生錯誤。