How can I pass underscore to \newcommand properly? suggests the solution is to use a zero-argument command that changes the catcode of _
, before expanding to the actual command.
That's kind of the opposite of why I'd want to use \NewDocumentCommand
, as it's kind of awkward, and also obfuscating argument types.
Is there a way to pass strings containing underscores (file names, by the way) without leaving the comfort of my expl3/xparse/\NewDocumentCommand
bubble?
I don't want to use v
arguments; it should really function like a normal macro, \includechapter{foo_bar}
and not like \includechapter!foo_bar!
.
My code currently:
%%% 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
The build breaks when reaching
\includechapter{foo_bar}
with
! Missing $ inserted.
<inserted text>
$
l.147 \includechapter{foo_bar}