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.

I use rule so as to separate the differents sections. I would like to automate this so as to use the following syntax :

\section*{One day}

\noindent\lipsum[1]

\section*{Another day}

\noindent\lipsum[1]

\section*{One more day}

\noindent\lipsum[1]

\newpage
\section*{One last day}

\noindent\lipsum[1]

There are two problems to solve :

  1. The first section isn't preceded by a rule.
  2. If one section is at the begining of one new page, it isn't preceded by a rule.

Code to ameliorate

\documentclass[12pt]{article}
    \newcommand{\myRule}{%
        \vspace{0.5cm}\hrule%
    }

    \usepackage{lipsum}


\begin{document}

% No rule ! 
\section*{One day}

\noindent\lipsum[1]

\myRule
\section*{Another day}

\noindent\lipsum[1]

\myRule
\section*{One more day}

\noindent\lipsum[1]

\newpage
% No rule !
\section*{One last day}

\noindent\lipsum[1]

\end{document}
share|improve this question
    
Two questions: (1) Will you be using unnumbered sections? (2) Will you be using a force \newpage to flush the page? –  Werner Nov 30 '11 at 20:34
    
The newpage is for testing... I have to use unnumbered sections because I will also use the following formatting : tex.stackexchange.com/questions/36554/… . –  projetmbc Nov 30 '11 at 20:45

4 Answers 4

up vote 6 down vote accepted

By combining Mico's and Alan Munn's answer, here's a simpler code that should accomplish what's requested:

\usepackage{titlesec}
\titleformat{\section}
  {\leaders\vrule width \textwidth\vskip 0.4pt\vspace{.8ex}%
   \normalfont\Large\bfseries}
  {}
  {0em}
  {}

The space between the rule and the section title is .8ex (adjust to suit you).

share|improve this answer
    
Good ! I choose your solution because that's the smaller one. –  projetmbc Nov 30 '11 at 23:40

The following code is based on this posting to comp.text.tex in May 2011. No rule will be drawn if the section header is at the top of a page, including the very first page. This method works with both numbered and unnumbered section headers.

The amount of space between the end of a section and the subsequent rule and between the rule and the next section header, is controlled by the commands \vspace{2\bigskipamount} and \vspace{\smallskipamount}; change the arguments of the two \vspace commands to suit your preferences. The width of the rule by default is 0.4pt. (0.4pt seems to be something of a standard width for many lines in LaTeX...) You can change this setting too, of course.

Note that the command \separatorrule can be used on its own anywhere in the text, not just right before a section heading. Its two main distinguishing properties are (i) no rule will be drawn at the top of a page and (ii) no rule will be drawn later on in a page if the command is followed by an implicit or explicit \clearpage command, or similar-effect command such as \cleardoublepage.

\documentclass{article}
\newcommand{\separatorrule}{% 
    \par\vspace{2\bigskipamount}
    \leaders\vrule width \textwidth\vskip 0.4pt 
    \vspace{\smallskipamount}\nointerlineskip}    
\let\origsection\section
\renewcommand\section{\separatorrule \origsection}
\usepackage{lipsum} % for filler text

\begin{document}
\section*{A} \lipsum[1]
\section{B}  \lipsum[2]
\section*{C} \lipsum[3]
\clearpage
\section*{D} \lipsum[4]
\section{E}  \lipsum[5]
\end{document}

Finally, I have a strong hunch that the provider of the code for the separator rule in the comp.text.tex group, "Enrico Gregorio", is the very same person as egreg in the TeX Stack Exchange group. At any rate, the originality of my contribution to this posting is clearly minimal.

share|improve this answer
    
Actually the code was for another purpose, but it's possible that the OP wants the rule to disappear if a section falls at a page start. –  egreg Nov 30 '11 at 21:13
    
@egreg: Good news, your code deals with the start-of-page concern as well. :-) –  Mico Nov 30 '11 at 21:22
    
That's what I said, actually: leaders behave like glue, so they disappear at a page break. It depends on what the OP likes. –  egreg Nov 30 '11 at 21:41
    
On the other hand, \section adds a break point, so the rule might get at the end of the page and the title on the subsequent page. –  egreg Nov 30 '11 at 22:43

This is only a partial answer, since there are still some spacing issues that need to be resolved. At the very least, it could guide to a better solution.

Using a combination of titlesec and some code from needspace it is possible to condition on page availability.

enter image description here

\documentclass[12pt]{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{titlesec}% http://ctan.org/pkg/titlesec
\newcounter{tmpcntr} \preto\newpage{\setcounter{tmpcntr}{0}}%

% Taken from the needspace package (http://ctan.org/pkg/needspace)
\makeatletter
\newcommand{\needspace}[3]{\par \penalty-100\begingroup
  \setlength{\dimen@}{#1}%
  \dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
  \ifdim \dimen@>\dimen@ii
    #3
  \else
    #2
  \fi\endgroup}
\makeatother

\titleformat{\section}{\bfseries\large\stepcounter{tmpcntr}}{\thesection}{\quad}{%
  \needspace{\dimexpr0.4pt+5mm+1.5\baselineskip\relax}%
    {\ifnumequal{\value{tmpcntr}}{1}{}{\titlerule\nobreak\kern5mm\nobreak}}% enough space
    {\break\vspace*{\topskip}}% not enough space
}

\begin{document}

% No rule ! 
\section*{One day}

\noindent\lipsum[1]

\section*{Another day}

\noindent\lipsum[1]

\section*{One more day}

\noindent\lipsum[1]

\newpage
% No rule !
\section*{One last day}

\noindent\lipsum[1]

\end{document}
​

The modified code from needspace provides

\needspace{<len>}{<less than len>}{<more than len>}

which executes <less than len> if there is less than <len> available on the page, otherwise it issues <more than len>.

Additionally, the tmpcntr counter is meant to be a reference of whether or not you start a new page, or the start of your document with a \section. If this counter is 1 (incremented within \section) no \titlerule is typeset.

share|improve this answer
    
Thanks for this solution. –  projetmbc Nov 30 '11 at 21:27
    
@projetmbc: It's partial, but I'm sure you could tweak it to fit your needs. –  Werner Nov 30 '11 at 21:28
    
I like the possibility to play with titlesec. Thanks for this ! –  projetmbc Nov 30 '11 at 21:32

Here's a partial answer using the titlesec package. I've added a conditional to remove the rule for the first section. If this applied to chapters, you'll need to add \ruledfalse to the definition of \chapter. This doesn't solve the top of page problem, however, but I'll leave it up as an answer, since others who arrive here may not have that requirement.

\documentclass{article}
\usepackage{titlesec}
\newif\ifruled
\titleformat{\section}
    {\ifruled\titlerule\else\global\ruledtrue\fi\vspace{.8ex}%
     \normalfont\bfseries} {}{0em}{}

\begin{document}
\section*{A section}
\section*{Another section}
\end{document}

output of code

share|improve this answer
    
But I think the OP does not want the rule for the first section, or one at the top of a page –  Peter Grill Nov 30 '11 at 20:36
    
That's right, it's one critical and technical necessity... The same is true for sections at the beggining of one page. –  projetmbc Nov 30 '11 at 20:47
    
Oh, I'm sorry, I misunderstood the problem, I thought that you wanted these. I'll fix the answer (or delete it). –  Alan Munn Nov 30 '11 at 20:49

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.