LaTeX:重新定义加星号命令

发布于 2024-08-24 10:09:07 字数 509 浏览 6 评论 0原文

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

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

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

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

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

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

I want to redefine the \part* command so that it automatically adds a contents line. This proves difficult since I want to reuse the original \part* command inside my starred version.

Normally (i.e. for unstarred commands) I would do it like this:

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

That is, I would save the original definition of \part in \old@part and use that.

However, this doesn’t work for starred commands since they don’t define a single lexeme (unlike the \part command in the example above). This boils down to the following question: How can I save a starred command?

Notice that I already know how to redefine a starred command itself, using the \WithSuffix command from the suffix package. This isn’t the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拒绝两难 2024-08-31 10:09:07

没有 \part* 命令。发生的情况是 \part 命令查看其后面的下一个字符(使用 \@ifstar),然后分派到其他两个例程之一,该例程基于以下内容执行实际工作:关于那里是否有星号。

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

There is no \part* command. What happens is the \part command takes a look at the next character after it (with \@ifstar) and dispatches to one of two other routines that does the actual work based on whether there's an asterisk there or not.

Reference: TeX FAQ entry Commands defined with * options

音盲 2024-08-31 10:09:07

感谢@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.

Thanks to @smg’s answer, I’ve cobbled together a solution that works perfectly. Here’s the complete source, along with explanatory comments:

% 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

(This needs the packages suffix and ifthen.)

Now, we can use it:

\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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文