【问题标题】:LaTeX: Redefining starred commandLaTeX:重新定义星号命令
【发布时间】:2010-03-10 16:13:56
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: latex tex


    【解决方案1】:

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

    参考:TeX FAQ entryCommands defined with * options

    【讨论】:

    • “没有\part* 命令。” - 我知道。 :-( 否则我不会有这个问题。
    • 那么像你一样重新定义\part 并处理这两个版本?还是深入研究 LaTeX 源代码并重新定义底层的 starred-\part 代码?
    • ……但这是至关重要的提示。我现在可以正常工作了。很快就会发布解决方案。
    • 您能说一下为什么要重新定义\part* 以添加目录条目吗?因为不添加一个首先是提供一个加星标的版本,这听起来有点傻。
    • 如果是这样的话,我建议使用 titlesec 包来修改标题的样式,这最终会容易得多,而且不足为奇给其他“LaTeXnicians”,而不是搞乱重新定义命令。
    【解决方案2】:

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

    % 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 2010-10-03
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      相关资源
      最近更新 更多