# How can set \rule after \section if \section is renew?

How can I set \rule after \section if \section is renewed?

This doesn't work:

    \let\oldsection\section
\renewcommand{\section}{%
\newpage%
\oldsection%
\rule{\textwidth}{4pt}%
}


This works, but the line is before title:

    \let\oldsection\section
\renewcommand{\section}{%
\newpage%
\rule{\textwidth}{4pt}%
\oldsection%
}

-

The reason for this is because \section takes some arguments, and these arguments have to be read before you can place the horizontal rule. You can use xparse to help capture the arguments first and then decide on the type of sectioning you want to do:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{xparse}% http://ctan.org/pkg/xparse
\makeatletter
\NewDocumentCommand{\Section}{s o m}{%
\clearpage
\IfBooleanTF{#1}
{% \Section*
\section*{#3}%
}{% \Section
\IfNoValueTF{#2}
{% \Section[]{}
\section[#2]{#3}%
}{% \Section{}
\section{#3}%
}%
}%
\noindent\rule{\textwidth}{4pt}\par\noindent
}
\makeatother
\begin{document}
\Section{A section}\lipsum[1-3]
\Section{Another section}\lipsum[1-3]
\end{document}


The {s o m} macro identifier requests an (optional) star, an optional argument and a mandatory argument. So, something of the form \Section*[..]{...} and branches accordingly using the conditionals \IfBooleanTF and \IfNoValueTF.

Of course, this is a very elementary example. You can play around with the spacing of the horizontal rule. Also, other packages allow does this kind of thing for you automatically, like titlesec or sectsty, for example.

-
ou, my text is renerated by pandoc, I can't use Newcommand. Ok, let see on titlerec thx for your answer –  Uki Jul 13 '12 at 7:21

Do you mean something like this?

\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}[block]
{\bfseries\filcenter}
{\textup{\thesection}}% label
{0.5em}%                sep
{}%                     before
[\titlerule]%           after
\begin{document}
\section{This is a section}
This is a sentence.
\end{document}

-