11

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

2 Answers2

11

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

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
Nietzche-jou
  • 14,415
  • 4
  • 34
  • 45
  • “There is no `\part*` command.” – I know. :-( Otherwise I wouldn’t have this problem. – Konrad Rudolph Mar 10 '10 at 16:50
  • So then redefine `\part` like you have and handle both versions? Or dig into the LaTeX source and redefine the underlying starred-`\part` code? – Nietzche-jou Mar 10 '10 at 16:56
  • … but this was the vital hint. I’ve got the it working now. Will post the solution shortly. – Konrad Rudolph Mar 10 '10 at 16:57
  • Could you say why you'd want to redefine `\part*` to add a TOC entry? Because not adding one is sort of the point of providing a starred version in the first place, and it sounds kind of silly. – Nietzche-jou Mar 10 '10 at 17:00
  • @sgm: The starred version of `\part` is also *unnumbered*, which is what I want. The alternative would have been to redefine the unstarred version to be unnumbered but I felt that this would be even less consistent, since starring a command is pretty much the convention to make a command unnumbered, not only in section headings. – Konrad Rudolph Mar 10 '10 at 17:09
  • 1
    If that is the case, I'd suggest the use of the *titlesec* package to modify the styles of headings, which is going to end up being much easier, and unsurprising to other "LaTeXnicians", rather than messing with redefining commands. – Nietzche-jou Mar 10 '10 at 19:44
  • Thanks for the package suggestion, this looks like it could save me some work. Up until now, I’ve always completely redefined header commands when I wanted to style them. – Konrad Rudolph Mar 10 '10 at 22:40
6

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.
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214