Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX: Redefining starred command

Tags:

latex

tex

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.

like image 826
Konrad Rudolph Avatar asked Mar 10 '10 16:03

Konrad Rudolph


2 Answers

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

like image 175
Nietzche-jou Avatar answered Sep 20 '22 09:09

Nietzche-jou


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.
like image 23
Konrad Rudolph Avatar answered Sep 21 '22 09:09

Konrad Rudolph