18

I'm trying to put a box around a section, both the title and the contents. I've found ways of doing one or the other, but not both.

+-----------------------+
| Title                 |
|                       |
| content of section    |
| ...                   |
+-----------------------+

Something like the above, a single box around both the section title, and the section content.

3 Answers 3

19

Try the mdframed package.

\documentclass{article}
\usepackage{mdframed}
\begin{document}
\begin{mdframed}
\section{This is a framed section}
This is the text.
\end{mdframed}
\end{document}

mdframed

2
  • Tried that. Ended up with strange results. Some sections had to lines, others did not. Some had left lines, others did not. Some had bottom lines, others did not. No single section had four lines all the way around it. Actually, on closer look, the lines do go all the way around, but such a thin line that Adobe Acrobat does not render the line in all cases. How can I make the line thicker?
    – AgilePro
    Oct 22, 2016 at 19:57
  • 1
    Use something like \begin{mdframed}[linewidth=5pt] (see the mdframed package documentation for details). Oct 24, 2016 at 22:51
10

Another option, using the tcolorbox package:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\begin{document}

\section{A test section}
\lipsum[4]
\begin{tcolorbox}[breakable,colback=white,colframe=cyan,width=\dimexpr\textwidth+12mm\relax,enlarge left by=-6mm]
\section{A framed test section}
\lipsum[1-2]
\end{tcolorbox}

\end{document}

enter image description here

2

Here's a solution using ConTeXt. First a text background is defined using \definetextbackground. In contrast to a \framed, the text background permits page breaks. All options except location are optional. The background is then hooked into the chapter.

\setuppapersize [A6]  %% only for the screenshot

\definetextbackground
  [sectionbackground]
  [location=paragraph,
   rulethickness=2pt,
   topoffset=2em,
   leftoffset=2em,
   corner=round,
   radius=1cm,
   background=none]

\setuphead
  [chapter]
  [before=\startsectionbackground,
   aftersection=\stopsectionbackground]

\starttext
  \startchapter [title=Lorem ipsum dolor…]
    \input knuth
  \stopchapter
\stoptext

screenshot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .