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.

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%
    }
share|improve this question
add comment

2 Answers

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:

enter image description here

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

share|improve this answer
    
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
add comment

Do you mean something like this?

enter image description here

\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}
share|improve this answer
add comment

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.