无法使按键列表发挥作用

无法使按键列表发挥作用

我正在尝试使用以下方法制作自己的章节标题格式化命令key=value

\coolchap[
chapstyle=<chapter-word formatting>,
titlestyle=<chapter-title formatting>,
befchap=<vspace before chapter-word>,
afchap=<vspace after chapter-word>,
aftitle=<vspace after chapter-title>]

但一定是出了什么问题,因为它没有效果:我没有收到任何错误消息,但这些值被忽略了。

请注意,如果我将参数定义为强制的,然后传递命令,则会发生同样的情况\coolchap{key1=value1,key2=value2...}

辅助命令本身,即\chapterformat,有效。

平均能量损失

\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}

\makeatletter

\NewDocumentCommand{\chapterformat}{mmmmm}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{#3\p@}% Vertical Space before "Chapter X" (50)
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1 \@chapapp{} \thechapter} % Chapter-word formatting.
                \par
                \vskip #4\p@ % Vertical Space after "Chapter X" (20)
                \fi
                {#2 ##1} % Title formatting.
                \par\nobreak
                \vskip #5\p@}} % Vertical Space after Chapter Title (180)
    }{}
}

\makeatother

\ExplSyntaxOn


\cs_generate_variant:Nn \xcoolchap:nnnnn { VVVVV }
\cs_set_eq:NN \xcoolchap:nnnnn \chapterformat

\NewDocumentCommand{\coolchap}{o}
{
    \group_begin:
      \keys_set:nn { coolchap } { #1 }
      \xcoolchap:VVVVV
        \xcoolchap_chapstyle
        \xcoolchap_titlestyle
        \xcoolchap_befchap
        \xcoolchap_afchap
        \xcoolchap_aftitle
    \group_end:
}
\keys_define:nn { coolchap }
{
    chapstyle .tl_set:N = \xcoolchap_chapstyle,
    titlestyle .tl_set:N = \xcoolchap_titlestyle,
    befchap .tl_set:N = \xcoolchap_befchap,
    afchap .tl_set:N =  \xcoolchap_afchap,
    aftitle .tl_set:N = \xcoolchap_aftitle,
    chapstyle .initial:n = \HUGE\bfseries,
    titlestyle .initial:n = \Huge\bfseries,
    befchap .initial:n = 50,
    afchap .initial:n = 20,
    aftitle .initial:n = 180,
}



\ExplSyntaxOff



\begin{document}
    

    \coolchap[aftitle=360,befchap=100,afchap=40,chapstyle=\HUGE\itshape]
    
    \chapter{essai}
    
    XXX
    
    
\end{document}

在此处输入图片描述

答案1

关于您的代码的一些注释:

  • \par在应用字体更改的组内使用,而不是{\huge Title string}\par使用{\huge Title string\par},否则行间间距最终会错误。
  • 您的定义中有一些不需要的空格,请注意,#1定义中的空格后面的空格不会被忽略,即使在使用时#1会出现\huge类似情况。
  • 你的变量没有遵循expl3命名约定,我建议你纠正这个问题(这些约定是有原因的,即使没有合理的方法让 TeX 强制执行它们)。

我并没有在每个代码块中都更正这些问题,所以不要只是 1:1 地复制这些,而要注意需要修复的地方。


问题是您\@makechapterhead在组内重新定义,而定义在组末尾丢失。您必须在之后进行重新定义\group_end:,您可以像下面这样执行此操作。此外,\coolchap如果您省略可选参数,您的宏会抛出错误,您应该像下面这样使用O{}而不是。o

\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}

\makeatletter

\NewDocumentCommand{\chapterformat}{mmmmm}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{#3\p@}% Vertical Space before "Chapter X" (50)
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1 \@chapapp{} \thechapter} % Chapter-word formatting.
                \par
                \vskip #4\p@ % Vertical Space after "Chapter X" (20)
                \fi
                {#2 ##1} % Title formatting.
                \par\nobreak
                \vskip #5\p@}} % Vertical Space after Chapter Title (180)
    }{}
}

\makeatother

\ExplSyntaxOn


\tl_new:N \l__xcoolchap_redefinition_tl
\NewDocumentCommand{\coolchap}{O{}}
{
    \group_begin:
      \keys_set:nn { coolchap } { #1 }
      \tl_set:Nx \l__xcoolchap_redefinition_tl
        {
          \exp_not:N \chapterformat
            { \exp_not:V \l__xcoolchap_chapstyle_tl }
            { \exp_not:V \l__xcoolchap_titlestyle_tl }
            { \exp_not:V \l__xcoolchap_befchap_tl }
            { \exp_not:V \l__xcoolchap_afchap_tl }
            { \exp_not:V \l__xcoolchap_aftitle_tl }
        }
      \exp_last_unbraced:NV
    \group_end:
    \l__xcoolchap_redefinition_tl
}
\keys_define:nn { coolchap }
{
    chapstyle .tl_set:N = \l__xcoolchap_chapstyle_tl,
    titlestyle .tl_set:N = \l__xcoolchap_titlestyle_tl,
    befchap .tl_set:N = \l__xcoolchap_befchap_tl,
    afchap .tl_set:N =  \l__xcoolchap_afchap_tl,
    aftitle .tl_set:N = \l__xcoolchap_aftitle_tl,
    chapstyle .initial:n = \HUGE\bfseries,
    titlestyle .initial:n = \Huge\bfseries,
    befchap .initial:n = 50,
    afchap .initial:n = 20,
    aftitle .initial:n = 180,
}



\ExplSyntaxOff



\begin{document}
    

    \coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
    
    \chapter{essai}
    
    XXX
    
    
\end{document}

免责声明:我是expkv-cs

请注意,这要容易得多,expkv-cs因为它不适用于作业,因此不存在这样的分组问题:

\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}
\usepackage{expkv-cs}

\makeatletter

\NewDocumentCommand{\chapterformat}{mmmmm}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{#3\p@}% Vertical Space before "Chapter X" (50)
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1 \@chapapp{} \thechapter} % Chapter-word formatting.
                \par
                \vskip #4\p@ % Vertical Space after "Chapter X" (20)
                \fi
                {#2 ##1} % Title formatting.
                \par\nobreak
                \vskip #5\p@}} % Vertical Space after Chapter Title (180)
    }{}
}

\NewDocumentCommand\coolchap{O{}}{\coolchapKV{#1}}
\ekvcSplitAndForward\coolchapKV\chapterformat
  {
     chapstyle  = \HUGE\bfseries
    ,titlestyle = \Huge\bfseries
    ,befchap    = 50
    ,afchap     = 20
    ,aftitle    = 180
  }

\makeatother


\begin{document}
    

    \coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
    
    \chapter{essai}
    
    XXX
    
    
\end{document}

两个代码的输出:

在此处输入图片描述


另一种方法可能是重新定义\chapterformat并在重新定义中使用变量。这样,您无需使用任何扩展技巧,就可以继续使用l3keys

\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}

\makeatletter

\ExplSyntaxOn
\ifdef{\chapter}{%
  \renewcommand{\@makechapterhead}[1]
    {%
      \vspace*{\l__xcoolchap_befchap_tl\p@}% Vertical Space before "Chapter X" (50)
      \begingroup
        \parindent \z@ \raggedright \reset@font
        \ifnum \c@secnumdepth >\m@ne
          {\l__xcoolchap_chapstyle_tl \@chapapp{} \thechapter\par}% Chapter-word formatting.
          \vskip \l__xcoolchap_afchap_tl\p@ % Vertical Space after "Chapter X" (20)
        \fi
        {\l__xcoolchap_titlestyle_tl #1\par}% Title formatting.
        \nobreak
        \vskip \l__xcoolchap_aftitle_tl\p@
      \endgroup
    }% Vertical Space after Chapter Title (180)
}{}

\makeatother


\NewDocumentCommand{\coolchap}{O{}}
  { \keys_set:nn { coolchap } { #1 } }
\keys_define:nn { coolchap }
{
    chapstyle .tl_set:N = \l__xcoolchap_chapstyle_tl,
    titlestyle .tl_set:N = \l__xcoolchap_titlestyle_tl,
    befchap .tl_set:N = \l__xcoolchap_befchap_tl,
    afchap .tl_set:N =  \l__xcoolchap_afchap_tl,
    aftitle .tl_set:N = \l__xcoolchap_aftitle_tl,
    chapstyle .initial:n = \HUGE\bfseries,
    titlestyle .initial:n = \Huge\bfseries,
    befchap .initial:n = 50,
    afchap .initial:n = 20,
    aftitle .initial:n = 180,
}
\ExplSyntaxOff

\begin{document}
    

    \coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
    
    \chapter{essai}
    
    XXX
    
    
\end{document}

相关内容