[Tex/LaTex] Redefining commands in LaTeX (\section in particular)


A few days ago I asked a question about how to redefine the \section command to be able to write content just after the head of the section with no line breaking: Redefining \section command (I strongly recommend you to have a look to the link attached as it could be a good reference point to understand with this question).

Although we arrived at a satisfactory answer, I would like to ask a related question. I think that is similar but not quite the same so I have opened a new post (correct me if I'm wrong).

Here is my question:

I would like to change the \section command in the article class itself.
I haven't found any place where it says that it has optional options and the only way I found that allow you to change it substantially is with the titlesec package.

How can I change it to make it work differently? The question itself is more about how to change a LaTeX command in its core than just about the \section command. But we can work with it for the sake of clarity.

Best Answer

\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}%

\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


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


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.

Related Question