Document ClassesRedefining \@sect

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Redefining \@sect

Post by rspringuel »

I'm writing a class file in which I need the \@sect macro to function just like it does normally most of the time, but under certain conditions (as determined by the evaluation of an \if statement) to do something extra. If this was just a normal command, I would use the following kind of code:

Code: Select all

\let\old@sect\@sect
\renewcommand{\@sect}{\old@sect\if<condition><do stuff>\fi}
However, \@sect is a macro built into the kernel so I'm not sure if this is entirely kosher, especially since I'd like the <do stuff> to be able to use arguments #7 and #8 from \@sect (the short and long name for the section being created). It's this last part in particular that prevents me from just trying my luck since argument #7 is optional for \@sect and \renewcommand requires that the first argument be the optional one, if any.

Is it even possible to play around with this macro the way I want to? If so, any hints or suggestions that I can try?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
rf
Posts: 21
Joined: Mon Jul 20, 2009 5:27 pm

Redefining \@sect

Post by rf »

rspringuel wrote:I'm writing a class file in which I need the \@sect macro to function just like it does normally most of the time, but under certain conditions (as determined by the evaluation of an \if statement) to do something extra. If this was just a normal command, I would use the following kind of code:

Code: Select all

\let\old@sect\@sect
\renewcommand{\@sect}{\old@sect\if<condition><do stuff>\fi}
However, \@sect is a macro built into the kernel so I'm not sure if this is entirely kosher, especially since I'd like the <do stuff> to be able to use arguments #7 and #8 from \@sect (the short and long name for the section being created). It's this last part in particular that prevents me from just trying my luck since argument #7 is optional for \@sect and \renewcommand requires that the first argument be the optional one, if any.

Is it even possible to play around with this macro the way I want to? If so, any hints or suggestions that I can try?
it's certainly possible, but not using \renewcommand (as you guessed). the good news is that the fact that #7 is (seemingly) optional has been ironed out by the time we get into \@sect. so it's all actually rather straightforward, except that we have to use the primitive \def command in place of \renewcommand:

Code: Select all

\let\@old@sect\@sect
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \@old@sect{#1}{#2}{#3}{#4}{#5}{#6}[#7]{#8}%
  \if<something><do something>\fi
}
i haven't tested this: give it a whirl and if it doesn't work, come back and complain. (note, i've never yet managed to read this forum from home, so i may not respond to complaints until monday.)
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Re: Redefining \@sect

Post by rspringuel »

That indeed worked.

In the end, however, I ended up using \def to redefine \@sect whole sale so that I could embed the \if statements in the original code. It made the code a bit more understandable (at least to me).
Post Reply