1

I'd like the section title to look just as it does by default, except the following changes:

  • No spacing (before/after)
  • Text is inside black filled box
  • Text color is white

I've tried to use the titlesec package as done in the firt answer to the following question:

How to place a shaded box around a section label and name

However, I'm really having a hard time getting things to work with titlesec (it's not so intuitive in it's settings for me).

  • 2
    Could you show us what you tried that did not work, in a short but complete document (MWE)? That makes it easier for the community to identify problems and provide solutions for you. – Marijn Jul 9 '16 at 9:58
  • A small picture would be helpful also. – MaestroGlanz Jul 9 '16 at 10:03
  • My first attempt is this: \let\mysection\section \def\section#1{ \vskip-\bigskipamount \mysection{#1} \vskip-\bigskipamount } but I dont know the name of the space above. – MaestroGlanz Jul 9 '16 at 10:04
  • @Marijn I couldn't do much more than what is provided in the example link i sent you. I've tried to move the spacings and put the section numbers inside - but I didn't succeed. – ndrizza Jul 9 '16 at 10:27
1

Here is a solution

\documentclass{article}
\usepackage{mwe}

\usepackage{xcolor}
\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {0pt}%
                                   {0pt}%
                                   {\normalfont\Large\bfseries\mybox}}
\makeatother

\newcommand*{\mybox}[1]{%
    \noindent\colorbox{blue!20}{%
        \parbox{\dimexpr\columnwidth-2\fboxsep\relax}{%
            \textcolor{white}{#1}}}}

\begin{document}
\lipsum[1]
\section{Foo bar baz bla bla}
\lipsum[1]
\end{document}

Update if we need some vertical space before and after

\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3pt}%
                                   {3pt}%
                                   {\normalfont\Large\bfseries\mybox}}

enter image description here

  • Thanks that worked! Do you know how to change the solution to add a bit of space? Like 3pts before and after? – ndrizza Jul 10 '16 at 11:00
  • @ndrizza try with \renewcommand\section{\@startsection {section}{1}{\z@}% {-3pt}% {3pt}% {\normalfont\Large\bfseries\mybox}} see update. – touhami Jul 10 '16 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.