tags:

views:

342

answers:

2

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.

+3  A: 

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.

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

Cirno de Bergerac
“There is no `\part*` command.” – I know. :-( Otherwise I wouldn’t have this problem.
Konrad Rudolph
So then redefine `\part` like you have and handle both versions? Or dig into the LaTeX source and redefine the underlying starred-`\part` code?
Cirno de Bergerac
… but this was the vital hint. I’ve got the it working now. Will post the solution shortly.
Konrad Rudolph
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.
Cirno de Bergerac
@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
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.
Cirno de Bergerac
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
+2  A: 

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