11

我想重新定义\part*命令,以便它自动添加内容行。这证明很困难,因为我想\part*在我的加星号版本中重用原始命令。

通常(即对于未加星号的命令)我会这样做:

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

也就是说,我会保存\partin的原始定义\old@part并使用它。

但是,这不适用于带星号的命令,因为它们没有定义单个词位(与\part上面示例中的命令不同)。这归结为以下问题:如何保存已加星标的命令?

请注意,我已经知道如何使用包中的\WithSuffix命令重新定义带星号的命令本身suffix。这不是问题。

4

2 回答 2

11

没有\part*命令。发生的情况是该\part命令查看它之后的下一个字符(带有\@ifstar),并根据那里是否有星号分派到其他两个执行实际工作的例程之一。

参考:TeX FAQ entry使用 * 选项定义的命令

于 2010-03-10T16:43:55.207 回答
5

感谢@smg 的回答,我拼凑了一个完美运行的解决方案。这是完整的源代码以及解释性评论:

% 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

(这需要包suffixifthen。)

现在,我们可以使用它:

\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.
于 2010-03-10T17:06:09.647 回答