1

I am trying to create a section header with a bold horizontal line on top and a bold horizontal line at the bottom, and a background color that extends to both ends with a centered title. So far I am able to get everything using the solution found at https://tex.stackexchange.com/a/40051/183272, but I cannot figure out how to get the text to center. I am a complete newbie when it comes to TeX, could someone help me with this code?

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{xcolor}

\titleformat{\section}{\bfseries\center}{}{0em}{\hrule height 0.5mm\colorsection}[\hrule height 0.5mm]

\newcommand{\colorsection}[1]{\colorbox{gray!30}{\parbox{\dimexpr\textwidth-2\fboxsep}{\ #1}}}

\begin{document}
\section{Header}
\end{document}
  • \newcommand{\colorsection}[1]{\colorbox{gray!30}{\parbox{\dimexpr\textwidth-2\fboxsep}{\centering #1}}}might work – leandriis Mar 11 '19 at 20:04
  • Welcome to TeX SX! Your sections shouldn't be numbered? – Bernard Mar 11 '19 at 21:46
1

Here is a very simple way with tcolorbox:

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{titlesec}
\usepackage[table]{xcolor}
\usepackage{tcolorbox}
\usepackage{lipsum}

\newcommand{\colorsection}[1]{{%
\begin{tcolorbox}[colback=gray!15,halign=center, leftrule=0pt, rightrule=0pt, arc=0mm]\thesection.\enspace#1\end{tcolorbox}}}

\titleformat{\section}[block]{\bfseries}{}{0em}{\colorsection}

\begin{document}

\section{Header}
\lipsum[1]

\end{document}

enter image description here

| improve this answer | |

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.