Text FormattingCustom Formatting for Section Heading

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
skwateur
Posts: 14
Joined: Thu Jun 21, 2012 9:24 pm

Custom Formatting for Section Heading

Post by skwateur »

Hello guys,

I'm having some difficulties to make something like that :
section-formatting.png
section-formatting.png (5.25 KiB) Viewed 4437 times
I've been trying many things but I do not get anything like this...

Any ideas ?

Thanks :)

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Formatting for Section Heading

Post by cgnieder »

On first thought I'd guess it should be doable using the titlesec package.

Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Formatting for Section Heading

Post by cgnieder »

Here is a suggestion:

Code: Select all

\documentclass{article}

\usepackage{xcolor}
\usepackage{titlesec}
% \titleformat{command}[form]{layout}{label-layout}{skip}{before-code}[after-code]

\renewcommand*\thesection{\arabic{section}.}

\newcommand\secformat[1]{%
  \huge\colorbox{gray}{\parbox{\dimexpr\linewidth-6pt}{\bfseries\color{white}\thesection~#1}}} 
\titleformat{\section}[block]{\normalfont\rmfamily}{}{0pt}{\secformat}

\usepackage{kantlipsum}% for some sample text
\begin{document}

\section{Introduction}
\kant[1-3]

\end{document}
Regards

PS: I'm sure I'm missing something obvious: does anybody know why \linewidth is 6pt too wide in the example above?
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Custom Formatting for Section Heading

Post by localghost »

cgnieder wrote:[…] PS: I'm sure I'm missing something obvious: does anybody know why \linewidth is 6pt too wide in the example above?
You have to take the length \fboxsep (default value 3pt) into account. It appears two times here so the width of your \parbox should be \dimexpr\linewidth-2\fboxsep.


Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Formatting for Section Heading

Post by cgnieder »

localghost wrote:You have to take the length \fboxsep (default value 3pt) into account.
Of course! I feel stupid now ... ;)

Thanks
site moderator & package author
skwateur
Posts: 14
Joined: Thu Jun 21, 2012 9:24 pm

Re: Custom Formatting for Section Heading

Post by skwateur »

Thanks a lot guys, I'll try that when I'm done with the footer/margin problem...
skwateur
Posts: 14
Joined: Thu Jun 21, 2012 9:24 pm

Custom Formatting for Section Heading

Post by skwateur »

Wow very nice !

Is there anyway to avoid this style for \tableofcontent ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Custom Formatting for Section Heading

Post by Stefan Kottwitz »

You could use \ifthesection, which is described in the titlesec manual.

Stefan
LaTeX.org admin
skwateur
Posts: 14
Joined: Thu Jun 21, 2012 9:24 pm

Re: Custom Formatting for Section Heading

Post by skwateur »

Hello again,

I've been trying to use \ifthesection but I still can't make it.
Could you please post a small sample showing how to use \ifthesection please ?
Post Reply