代码之家  ›  专栏  ›  技术社区  ›  Konrad Rudolph

乳胶:重新定义星状命令

  •  9
  • Konrad Rudolph  · 技术社区  · 16 年前

    我想重新定义 \part* 命令,以便自动添加内容行。这证明是困难的,因为我想重用原来的 \零件*

    \let\old@part\part
    \renewcommand\part[2][]{
      \old@part[#1]{#2}
      … rest of definition}
    

    也就是说,我会保留 \part 在里面 \old@part 用这个。

    \零件 命令)。这归结为以下问题: 如何保存星号命令?

    \WithSuffix 来自 suffix 包裹这不是问题所在。

    2 回复  |  直到 16 年前
        1
  •  11
  •   0xC0000022L    7 年前

    根本没有 \part* \part 命令查看它后面的下一个字符(带 \@ifstar )并分派到另外两个例程中的一个,根据是否有星号执行实际工作。

    Commands defined with * options

        2
  •  5
  •   Konrad Rudolph    16 年前

    多亏了@smgs answer,我拼凑出了一个完美的解决方案。以下是完整的资料来源以及解释性意见:

    % If this is in *.tex file, uncomment the following line.
    %\makeatletter
    
    % Save the original \part declaration
    \let\old@part\part
    
    % To that definition, add a new special starred version.
    \WithSuffix\def\part*{
      % Handle the optional parameter.
      \ifx\next[%
        \let\next\thesis@part@star%
      \else
        \def\next{\thesis@part@star[]}%
      \fi
      \next}
    
    % The actual macro definition.
    \def\thesis@part@star[#1]#2{
      \ifthenelse{\equal{#1}{}}
       {% If the first argument isn’t given, default to the second one.
        \def\thesis@part@short{#2}
        % Insert the actual (unnumbered) \part header.
        \old@part*{#2}}
       {% Short name is given.
        \def\thesis@part@short{#1}
        % Insert the actual (unnumbered) \part header with short name.
        \old@part*[#1]{#2}}
    
      % Last, add the part to the table of contents. Use the short name, if provided.
      \addcontentsline{toc}{part}{\thesis@part@short}
    }
    
    % If this is in *.tex file, uncomment the following line.
    %\makeatother
    

    suffix ifthen

    现在,我们可以使用它:

    \part*{Example 1}
    This will be an unnumbered part that appears in the TOC.
    
    \part{Example 2}
    Yes, the unstarred version of \verb/\part/ still works, too.