4

I'm trying to have sections within titled-boxes in my document.

My minimum almost-working example is:

\documentclass[a4paper]{article}
\usepackage{lipsum}                                                                                                             
\usepackage{tikz}
\usepackage[left=2cm,right=4cm]{geometry}
\newcommand{\myFrame}[2]{
\begin{tikzpicture}
\node[inner sep=2em, draw=black, text width=\textwidth] (T) {#2};
\node[inner sep=1ex, draw=black,fill=white, 
 anchor=center,overlay, above of=T, yshift=2.5em] {\tikz \node[fill=black!10, text width=7cm,align=center] {\bf\sffamily#1};};
\end{tikzpicture}
}

\begin{document}
\myFrame{Section 1}{\lipsum[10]}
\end{document}

This results in:

enter image description here

However, my code is unstable, that is, if I change document size or use more than one \myFrame it won't work anymore as title boxes will change their places.

I have tried framed and mdframed packages but I couldn't make them show a frame like I need.

Can someone give me advice on how to fix my code to always work consistently or use other ways?

0

1 Answer 1

4

Don't nest tikzpictures inside tikzpictures.

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage[left=2cm,right=4cm]{geometry}
\newcommand{\myFrame}[2]{%
\noindent
\begin{tikzpicture}
\node[inner sep=2em, draw=black, text width=\dimexpr\textwidth-4em-\pgflinewidth\relax] (T) {#2};
\node[inner sep=1ex, draw=white,line width=2pt,fill=black!10,text width=7cm,align=center,font=\bfseries\sffamily,
 anchor=center] (H) at (T.north) {#1};
\draw (H.south west) rectangle (H.north east);
\end{tikzpicture}%
\par
}

\begin{document}
\myFrame{Section 1}{\lipsum[10]}
\myFrame{Section 1}{\lipsum[10]}
\myFrame{Section 1}{\lipsum[10]}
\myFrame{Section 1}{\lipsum[10]}
\end{document}

enter image description here

Here is a tcolorbox version with colours.

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{varwidth}
\usepackage[most]{tcolorbox}
\usepackage[left=2cm,right=4cm]{geometry}
\newtcolorbox{myFrame}[2][]{enhanced,
attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2, yshifttext=2mm-\tcboxedtitleheight/2},arc=0pt,outer arc=0pt,
fonttitle=\bfseries,%minipage boxed title*=-0.7\textwidth,
varwidth boxed title=0.4\linewidth,
colbacktitle=green!45!white,coltitle=green!10!black,colframe=green!50!black,
interior style={top color=yellow!10!white,bottom color=green!10!white},
boxed title style={enhanced,center title,boxrule=0.75mm,arc=0pt,outer arc=0pt,colframe=white,
borderline={0.1mm}{0mm}{green!50!black},
%borderline={0.1mm}{0.75mm}{green!50!black},
interior style={top color=black!10,bottom color=black!10,
middle color=black!10},
drop fuzzy shadow},
title={#2},
breakable,#1}

\begin{document}
\begin{myFrame}{Section 1}
  \lipsum[10]
\end{myFrame}
\begin{myFrame}{Section 1}
  \lipsum[11]
\end{myFrame}
\end{document}

enter image description here

3
  • This is great! Is this going to work if the frame exceeds a page? The frame will continue in the following pages?
    – Marc Kees
    Oct 9, 2015 at 16:42
  • Besides colors and shadow, what are the advantages of using tcolorbox over tikz approach?
    – Marc Kees
    Oct 9, 2015 at 16:45
  • @MarcKees Your question in first comment has the answer in your second comment. tcolorbox version is breakable. Just to mention one advantage :)
    – user11232
    Oct 9, 2015 at 16:47

You must log in to answer this question.

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