Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In my scrartcl document I want to change the spacing between sections and the first paragraph. Since I am using the parskip package, the parskip is set to a non-zero value. The \section command uses \parskip as well, but I want to remove that.

This could be achieved with the following code:

\section{Introduction}
\vskip-\parskip

This works perfectly fine. How can I append that code to the \section command in LaTeX without redefining all the rest? There is the etoolbox with commands like \appto, but applying it as follows results only into errors:

\appto{\section}{\vskip-\parskip}

I guess because \section takes arguments, what would be right command or is there an alternative solution?

share|improve this question
    
What \documentclass are you using? And what about \subsections and other sectional units? –  Werner Oct 3 '12 at 17:11
    
@Werner Thanks for pointing out. I am using the scrartcl document class and for other sectional units I would simply replicate the command. –  platzhirsch Oct 3 '12 at 17:21

2 Answers 2

up vote 7 down vote accepted

It's not because of \section taking an argument (or multiple arguments) per se. It's because \section acts as an intermediate function before processing the arguments following it. In fact, in the standard document classes \section takes no arguments at all (from article.cls, but similar for scrartcl.cls):

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

As such, appending code to \section actually inserts it between functions that expect certain arguments which, as you've found out, causes problems. So, while \@startsection is defined to take 6 arguments and does above, it's actually \@sect (for numbered) and \@ssect (for unnumbered or starred) that sets the sectional title. Even further "down the line", \@xsect is called to set the after-heading functionality, so I've patched that below:

enter image description here

\documentclass{scrartcl}% http://ctan.org/pkg/koma-script
\usepackage{parskip}% http://ctan.org/pkg/parskip
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}

\section{Introduction}% Original untouched \section
Here is some text.

\section{Introduction}% \section with manual modification
\vskip-\parskip
Here is some text.

\makeatletter
\patchcmd{\@xsect}% <cmd>
  {\ignorespaces}% <search>
  {\vskip-\parskip\relax\ignorespaces}% <replace>
  {}{}% <success><failure>
\makeatother

\section{Introduction}% Modified \section after patch
Here is some text.
\end{document}

Since this modification is done on a macro called by all sectioning units, it will hold for all.

share|improve this answer
    
Interesting explanation about the sectioning process used by LaTeX, i read the source2e.pdf (page 285) but i didn't understand until i read this answer. –  skpblack yesterday

In scrartcl.cls you will find the following definition.

\newcommand\section{\@startsection{section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
    \setlength{\parfillskip}{\z@ plus 1fil}\fi 
    \raggedsection\normalfont\sectfont\nobreak\size@section}%
}

The second and third lines determine the size of the space before and after a section heading, respectively. You could use \patchcmd from the etoolbox package, or you could redefine the \section directly. Either way, you need \makeatletter because the code contains @ symbols.

\documentclass{scrartcl}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\section}{2.3ex \@plus.2ex}{1pt}{}{}
\makeatother
\begin{document}
\section{A section}
Some text
\section{Another section}
\end{document}

.

\documentclass{scrartcl}
\makeatletter
\renewcommand\section{\@startsection{section}{1}{\z@}%
      {-3.5ex \@plus -1ex \@minus -.2ex}%
      {1pt}%
      {\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
        \setlength{\parfillskip}{\z@ plus 1fil}\fi 
        \raggedsection\normalfont\sectfont\nobreak\size@section}%
}
\makeatother
\begin{document}
\section{A section}
Some text
\section{Another section}
\end{document}
share|improve this answer
    
Great, but why should I choose \renewcommand over \patchcmd here? –  platzhirsch Oct 3 '12 at 17:29
    
@platzhirsch --- No particular reason. I only use etoolbox in this way if I want to patch a very long definition, but that's just my personal preference. –  Ian Thompson Oct 3 '12 at 17:31
    
What I don't understand is how parskip affects the space after a section heading. It seems these are only semi-fixed values: -2.3ex and .2ex. Anyway, could you add the complete patchcmd to your answer for completeness? I am having troubles with it: \patchcmd{\section}{2.3ex \@plus.2ex}{1pt}{}{false} does not work. –  platzhirsch Oct 3 '12 at 17:37
    
\par crops up in lots of places, including in the definition of \@startsection. Changing \parskip can have nasty side effects. –  Ian Thompson Oct 3 '12 at 18:08
    
Maybe I should go back to using \vskip 2ex then every time I want a paragraph. Just didn't seem convenient for a clean solution. –  platzhirsch Oct 3 '12 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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