Text FormattingChapter and Section Style

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Blaze
Posts: 6
Joined: Sat Jan 21, 2012 10:44 pm

Chapter and Section Style

Post by Blaze »

Hello everybody. This is my first post on this forum. First of all, I´m very grateful to this forum, ´cause I've been able to solve most of my LaTeX problem looking at the posts where written on here.

Now I have a problem with the chapter and section style that I can not solve, and I couldn't find the solution. I hope someone here could help me.

My problem comes from the style of the section and chapter, specially the section. I want to fill the background of the section part, and I have no idea how to do this. I´ll upload a part of a document where you can see what I mean.
chaptersection.png
chaptersection.png (39.23 KiB) Viewed 3581 times
I wish someone could help me to find the codes to make this kind of section and title.

Greetings from Spain.
Last edited by Blaze on Sun Jan 22, 2012 6:17 pm, edited 3 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Chapter and Section Style

Post by 5gon12eder »

With the help of the titlesec package (which is from Spain too I think) you can do pretty cool things. The usual way of typesetting material on a colored background with a specified width is to put it in a minipage and wrap that one into a \colorbox. This is neither convenient nor nice but at least it works.

I have tried to make an example that resembles what your picture looks like but there is surely much room for improvements.

Code: Select all

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}

% Only these three packages are specific to the solution:
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{calc}

\definecolor{titlecolor}{HTML}{008B72}

\newcommand{\sectioncolorbox}[1]
           {%
             \setlength{\fboxsep}{1ex}%
             \colorbox{titlecolor}%
                      {%
                        \begin{minipage}{\linewidth-2\fboxsep}
                          \sffamily\Large\slshape\color{white}%
                          \thesection.\quad#1
                        \end{minipage}
                      }%
           }

\titleformat
    {\chapter}
    [block]
    {\sffamily\huge\slshape\color{titlecolor}}
    {\chaptertitlename~\thechapter.\hfill}
    {0pt}
    {\MakeUppercase}

\titleformat
    {\section}
    [block]
    {}
    {}
    {0pt}
    {\sectioncolorbox}
    []

\titlespacing*{\chapter}{0pt}{0pt}{*8}
\titlespacing*{\section}{0pt}{*10}{*4}

\renewcommand{\thesection}{\arabic{section}}

\begin{document}
  \chapter{First Chapter}
  \section{First Section Therein}
  \lipsum
  \section{Second Section Therein}
  \lipsum[4]
  \chapter{Second Chapter}
  \lipsum
\end{document}
As you can see, the last mandatory argument to \titleformat is somewhat special as it accepts the title material as its argument which I have used for the above code. However, if it is not a command that takes an argument, it is also okay.

Best
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
Blaze
Posts: 6
Joined: Sat Jan 21, 2012 10:44 pm

Re: Chapter and Section Style

Post by Blaze »

Just, simply and obsolutely perfect 5gon12eder. I need to look the minipage environment slowly for understanding, and look for what I can change with this package. But, anyway, that´s exactly what I was looking for.

Thanks.

Blaze.
Post Reply