「特別な」catcodes (特に `_`) を含む文字列をカスタム NewDocumentCommand コマンドに渡す

「特別な」catcodes (特に `_`) を含む文字列をカスタム NewDocumentCommand コマンドに渡す

アンダースコアを \newcommand に適切に渡すにはどうすればよいですか?_解決策としては、実際のコマンドに展開する前に、の catcode を変更する引数なしのコマンドを使用することを提案しています。

これは、私が を使用したい理由とは逆で \NewDocumentCommand、少し扱いに​​くいですし、引数の型もわかりにくくなります。

\NewDocumentCommandexpl3/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 つの同一文字の間で読み取られます。逐語的な引数は、中括弧 とで囲むこともできます。%\#{}{}逐語的引数を持つコマンドが別の関数の引数内に表示されると、エラーが発生します。

関連情報