Redefining commands in LaTeX (\section in particular)

\section is not defined in the latex kernel it is defined (or not) in each class file. The implementation in article for example is just one definition

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

\section is a display heading in article just because the {2.3ex \@plus.2ex} argument is positive, if you put a negative space there it will be a run-in heading (as is \paragraph so

\makeatletter

\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {-1em}%
                                   {\normalfont\Large\bfseries}}

\makeatother

in the preamble for example would have that effect.

The titlesec package gives a slightly more declarative interface to making the same changes, but again it basically has to assume the original definition is more or less like the one in article class.

Some other classes (such as memoir I think) start with more involved definitions that allow easier customisation.


Supplemental Answer

Running texdoc article opens the classes.pdf file, which explains David Carlisle's answer pretty well. Look for the following section:

Chapters and Sections

where \@startsection and \secdef are explained.

Also see What does \z@ do?

Also note the texdoc source2e source2e.pdf document:

\def\@minus{minus} seemed absurd to me at first glance, but is just a way of speeding up the process by expanding once macro instead of reading 5 tokens (which ones? I wish the document would explain that better)


The file can also be viewed here:

http://mirror.ctan.org/macros/latex/base/classes.pdf

Other phenomenon / Headache Savers

If you decide to use the etoc package, but you have defined custom sectioning commands, you'll need to add a level to your custom sectioning command for etoc.

Why does my custom sectioning command crash on second run when using etoc?


Here is my way of redefining sections (with /def command):

\let\oldsection\section
\makeatletter
\def\msection{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{#2}%
}
\def\@StarredWithout#1{
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\oldsection{#1}%
}
\makeatother

The only change my section will provide is that it will accept starred sections with argument for the toc (and ignore that argument :P) Of course it isn't any real change but you can use these commands to redefine sections for any reason. A newspaper as example could add an if statement and if the section title contains words like freedom or slaves etc... could ignore these sections from table of contents and from the contents of sleeping people lives :P.