16

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.

If you needed any clarification just let me know it. Thank you all!

  • You can have a look at our starter guide to familiarize yourself further with our format. – DG' Jun 24 '15 at 10:25
  • @DG' I'll have a look, I didn't know that there was this post. Thanks! – Eduard Garriga Viudes Jun 24 '15 at 10:33
  • I am not sure what you mean by \section is an implemented command in most of the LaTeX versions. ? \section is not defined in the latex format, but is defined in many classes (for example article) but not in all (for example not in letter) – David Carlisle Jun 24 '15 at 10:41
  • @DavidCarlisle That is true, let me change it in the post. What I meant is that it is defined in many classes and you don't need to include any package to use it. I'm not sure how could this be explained properly. – Eduard Garriga Viudes Jun 24 '15 at 10:50
  • 2
    @EduardGarrigaViudes yes but the definitions in different classes are not necessarily related in any way so you can not have a generic mechanism, you need to know what you are redefining. Unless you want to use a package that completely redefines the sectioning commands ignoring any original class definitions, rather than just customising an existing layout. – David Carlisle Jun 24 '15 at 11:05
17

\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.

  • Really good answer. It is very helpful and clear. What would you say about \@startsection, it is a command by itself or it is related to renewcommand for sections? – Eduard Garriga Viudes Jun 24 '15 at 11:02
  • \@startsection is the core format-provided interface for defining sectioning commands, it allows definition of the hierarchy the {1} there is what makes section come between chapter and subsection, the space before and after, whether the heading is run in or displayed, whether the following text is indented. Most classes use it for all levels except chapter which often has a fancier heading layout, more like \title. – David Carlisle Jun 24 '15 at 11:08
  • I think I've got your point. I have seen these numbers to show the level of dephts for defining sections. Are they related to any kind of units? Or it is just a label? – Eduard Garriga Viudes Jun 24 '15 at 11:20
  • @EduardGarrigaViudes they are the number used by \setcounter{tocdepth} to specify whether that level appears in table of contents, and similarly \setcounter{secnumdepth} to specify if that level is numbered. If you set tocdepth to 0 then the {1} above means that only chapters (ie, nothing in article class) appear in the table of contents. – David Carlisle Jun 24 '15 at 11:39
  • 1
    @MaestroGlanz I'm not sure I understand your comment but \@startsection handles writing to the toc (or not) depending on secnumdepth – David Carlisle Jul 1 '16 at 12:52
4

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?

4

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.

0

The xparse package contains facilities for parsing an optional star, this is useful if you want to capture the entire \section command including its arguments.

The documentation has an example:

\NewDocumentCommand \chapter { s o m }
{
\IfBooleanTF {#1}
{ \typesetstarchapter {#3} }
{ \typesetnormalchapter {#2} {#3} }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.