LaTeX:重新定义星号命令

时间:2010-03-10 16:13:57

标签: latex tex

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

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

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

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

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

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

2 个答案:

答案 0 :(得分:9)

没有\part*命令。会发生什么是\part命令查看它之后的下一个字符(使用\@ifstar)并根据是否存在星号来调度到执行实际工作的其他两个例程之一。

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=cmdstar

答案 1 :(得分:4)

感谢@ 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.
相关问题