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:
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?