![「特別な」catcodes (特に `_`) を含む文字列をカスタム NewDocumentCommand コマンドに渡す](https://rvso.com/image/449643/%E3%80%8C%E7%89%B9%E5%88%A5%E3%81%AA%E3%80%8Dcatcodes%20(%E7%89%B9%E3%81%AB%20%60_%60)%20%E3%82%92%E5%90%AB%E3%82%80%E6%96%87%E5%AD%97%E5%88%97%E3%82%92%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0%20NewDocumentCommand%20%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AB%E6%B8%A1%E3%81%99.png)
アンダースコアを \newcommand に適切に渡すにはどうすればよいですか?_
解決策としては、実際のコマンドに展開する前に、の catcode を変更する引数なしのコマンドを使用することを提案しています。
これは、私が を使用したい理由とは逆で \NewDocumentCommand
、少し扱いにくいですし、引数の型もわかりにくくなります。
\NewDocumentCommand
expl3/xparse/バブルの快適さを離れずに、アンダースコアを含む文字列 (ちなみにファイル名) を渡す方法はありますか?
引数は使いたくありません。のようにではなく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
もならない 2 つの同一文字の間で読み取られます。逐語的な引数は、中括弧 とで囲むこともできます。%
\
#
{
}
␣
{
}
逐語的引数を持つコマンドが別の関数の引数内に表示されると、エラーが発生します。