[Tex/LaTex] Frame/Box around \chapter-, \section-headings,

koma-scriptmdframedscrbooktitlesec

Does anyone know how to put a frame around \chapter and \section-headings? I'm using Koma-Script (scrbook) but there it's rather difficult to find a solution …

Now I found the package 'titlesec'. I think it must be possible to solve my problem with it but unfortunately I don't get it.

Here's a little code-example with some (hanging!) \chapter and \section's where I want a frame around …

\documentclass[oneside,openright,BCOR=5mm,paper=a4,fontsize=11pt]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[automark]{scrpage2}
\usepackage{xcolor}

\usepackage{geometry}
\geometry{a4paper, top=35mm, left=30mm, right=60mm, bottom=25mm, headsep=15mm, footskip=12mm}

\usepackage[demo]{graphicx}
\usepackage{textpos}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{titlesec}

\usepackage{mparhack}

\setlength{\marginparwidth}{3.2cm}
\setlength{\marginparsep}{0.7cm}
\setheadwidth[0pt]{textwithmarginpar}
\setfootwidth[0pt]{head}

\titleformat{\chapter}[hang]{}{\parbox{1.5cm}{\thechapter}}{0pt}{}{}
\titleformat{\section}[hang]{}{\parbox{1.5cm}{\thesection}}{0pt}{}{}
\titleformat{\subsection}[hang]{}{\parbox{1.5cm}{\thesubsection}}{0pt}{}{}
\titleformat{\subsubsection}[hang]{}{\parbox{1.5cm}{\thesubsubsection}}{0pt}{}{}

\begin{document}

\chapter{Lorem ipsum}
One
\section{dolor}
Two
\subsection{amet}
Three

\end{document}

I also added an image where you can see the result that I want to get … but unfortunately don't know either how to manage. That's how it should look like

On the other hand I've already a solution by redefining \makechapterhead and defining a new \Section (capital 's') for framed section headings. Unfortunately I neither wasn't able to add the 'spacing' between chapter/section-number and chapter/section-heading in this redefinitions nor to redefine section in the way that I don't have to use this new command 'Section' 🙁

\renewcommand\section{\@startsection{section}{1}{\z@}%
  {-0.0ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\setlength{\parfillskip}{\z@
      plus 1fil}\fi
    \raggedsection\normalfont\sectfont\size@section}%
}

\renewcommand\@@makechapterhead[1]{
    \begin{mdframed}[linewidth=0.0pt, innerleftmargin=0, innerrightmargin=0, innertopmargin=2pt, innerbottommargin=0, backgroundcolor=lightgray]
  {%
    \setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
    \normalfont\sectfont\nobreak\size@chapter{}%
    \if@chapterprefix
      \let\@tempa\raggedsection
    \else
      \let\@tempa\@hangfrom
    \fi
    \@tempa{\ifnum \c@secnumdepth >\m@ne%
        \if@mainmatter
          \if@chapterprefix
            \expandafter\size@chapterprefix
          \else
            \expandafter\size@chapter
          \fi
          {\chapterformat}%
          \if@chapterprefix
            \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
          \fi
        \fi
      \fi
    }%
    {\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}}%
  \nobreak
    \end{mdframed}
    \vspace*{0.5\baselineskip}
}


\newcommand\Section[2][]{\begin{mdframed}[linewidth=0.5pt, innerleftmargin=0, innerrightmargin=0, innertopmargin=2pt, innerbottommargin=2pt, linecolor=gray, skipabove=24pt]%
  \ifx\relax#1\relax\section{#2}\else\section[#1]{#2}\fi
  \end{mdframed}}

It would be great if someone could help me!
Thank you in advance and greetings from Austria.


There's the picture I mentionned below: The headings should be at the same position.

— Code Example for @Herbert —

\documentclass[oneside,openright,BCOR=5mm,paper=a4,fontsize=11pt]{scrbook} 

\usepackage[T1]{fontenc} 
\usepackage[latin1]{inputenc} 
\usepackage[ngerman]{babel} 
\usepackage[automark]{scrpage2} 
\usepackage{xcolor} 

\usepackage{geometry} 
\geometry{a4paper, top=35mm, left=30mm, right=60mm, bottom=25mm, headsep=15mm, footskip=12mm} 

\usepackage[demo]{graphicx} 
\usepackage{textpos} 
\usepackage{blindtext} 
\usepackage{lipsum} 

\usepackage{mparhack} 

\setlength{\marginparwidth}{3.2cm} 
\setlength{\marginparsep}{0.7cm} 
\setheadwidth[0pt]{textwithmarginpar} 
\setfootwidth[0pt]{head} 

\begin{document} 

\pagestyle{scrheadings} 
\clearscrheadfoot 

\pagenumbering{Roman} 
\cfoot[\hspace{10.5cm}\today]{\hspace{10.5cm}\today} 
\ofoot[\pagemark]{\pagemark} 

\chapter{Lorem ipsum} 
\lipsum[1-3] 
\newpage 

\pagenumbering{arabic} 
\setcounter{page}{1} 
\clearscrheadfoot 

\ifoot[% 
  {% 
     \par% sonst gibt es eine Warnung 
     \begin{textblock*}{0mm}(0.78\paperwidth,-0.25\paperheight)% 
    \includegraphics[scale=0.30, angle=90]{demo} 
    \end{textblock*} 
  }% 
]{% 
   {% 
     \par% sonst gibt es eine Warnung 
     \begin{textblock*}{0mm}(0.78\paperwidth,-0.25\paperheight)% 
    \includegraphics[scale=0.30, angle=90]{demo} 
    \end{textblock*} 
   }% 
}% 
\cfoot[\hspace{10.5cm}\today]{\hspace{10.5cm}\today} 
\ofoot[\pagemark]{\pagemark} 

\chapter{Super} 
\blindtext 
\par 
\blindtext 

\end{document} 

— @Herbert

In the code example before there has to be included \usepackage{titletoc} and the following code block before \begin{document}:

\titlecontents{chapter}[2.5cm]{\addvspace{0.5cm}\bfseries}{\contentslabel{3.5cm}}{\hspace*{-1.5cm}}{\hfill\contentspage}[\addvspace{0.25cm}]
\titlecontents{section}[2.5cm]{\small}{\contentslabel{3.5cm}\small}{}{\titlerule*[0.12cm]{.}\contentspage}
\titlecontents{subsection}[2.5cm]{\small}{\contentslabel{3.5cm}\small}{}{\titlerule*[0.12cm]{.}\contentspage}
\titlecontents{subsubsection}[2.5cm]{\small}{\contentslabel{3.5cm}\small}{}{\titlerule*[0.12cm]{.}\contentspage}

In the Latex-Body the table of contents is going to be printed by using \tableofcontents. So far so good, now – with all redefinitions of \chapter \section etc., the table of contens looks like enter image description here

… but, it should look like (this result I get without the redefinitions) enter image description here

Best Answer

You can play with the vertical spaces. I defined all section commands new:

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[automark]{scrpage2}
\usepackage{xcolor}

\usepackage{geometry}
\geometry{a4paper, top=35mm, left=30mm, right=60mm, bottom=25mm, headsep=15mm, footskip=12mm}

\makeatletter
\newcommand\makeSecHead[4][\fbox]{%
  \@namedef{#2}{\@ifnextchar*{\@nameuse{#2@i}}{\@nameuse{#2@ii}}}
%
    \expandafter\def\csname#2@i\endcsname*##1{\par\vspace{#4}\noindent
       #1{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
         \normalfont\normalsize#3\makebox[40pt][l]{}~##1}}\par\vspace{#4}}%
%
    \expandafter\def\csname#2@ii\endcsname{\@ifnextchar[{\@nameuse{#2@iii}}{\@nameuse{#2@iv}}}%
%
    \expandafter\def\csname#2@iii\endcsname[##1]##2{\par\vspace{#4}\noindent
      #1{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
        \refstepcounter{#2}\normalfont\normalsize#3\makebox[40pt][l]{\@nameuse{the#2}}~##2}}%
        \addcontentsline{toc}{#2}{\@nameuse{the#2}~##1}\par\vspace{#4}}%
%
   \expandafter\def\csname#2@iv\endcsname##1{\par\vspace{#4}\noindent
     #1{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
       \refstepcounter{#2}\normalfont\normalsize#3\makebox[40pt][l]{\@nameuse{the#2}}~##1}}%
       \addcontentsline{toc}{#2}{\@nameuse{the#2}~##1}\par\vspace{#4}}%
}
\makeatother    

\makeSecHead[\colorbox{gray!30}]{chapter}{\Huge\bfseries}{20pt}
\makeSecHead{section}{\LARGE\bfseries}{15pt}
\makeSecHead{subsection}{\Large\bfseries}{12pt}
\makeSecHead{subsubsection}{\large\bfseries}{10pt}

\begin{document}
\tableofcontents

\bigskip
\chapter*{Test unnumbered chapter}
Text
\section*{Test unnumbered section}
Text
\subsection*{Test unnumbered subsection}
Text

\chapter{Test numbered chapter}
Text
\section{Test numbered section}
Text
\section*{Test unnumbered section}
Text
\subsection{Test numbered subsection}
Text
\subsection*{Test unnumbered subsection}
Text

\end{document}

enter image description here

And here is some code for placing images without textpos:

\ifoot[{\makebox[\linewidth][r]{%
  \makebox(0,0){\put(10,200){\includegraphics[width=2cm,height=4cm]{tiger}}}}}]
   {\makebox(0,0){\put(-100,200){\includegraphics[width=2cm,height=4cm]{tiger}}}}