3

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@[email protected]}\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

3 Answers 3

3

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}}}} 
13
  • Thank's a lot Herbert! That also looks pretty fine! (Except the position of the chapter/section-header positions. - Therefore I added a picture in my question.) But anyway! Thanks a lot!
    – eniem
    May 19, 2014 at 13:58
  • see edited answer. If you do not want the space for teh unnumbered sections delete the first \makebox[40pt]{}
    – user2478
    May 19, 2014 at 14:05
  • With your solution, the first line of the first paragraph after sectional unit titles is now indented and it shouldn't be. Any way to correct this? May 19, 2014 at 16:53
  • For the numbered section simply add \noindent at the edn: ...}\par\vspace{#4}\noindent}. The unnumbered sections like \tableofcontents and so on needs a redefinition.
    – user2478
    May 19, 2014 at 17:26
  • @Herbert: could it be that there are some incompatibilities with 'textpos'? (I use this for the poisitioning of a picture in my footer but now it doesn't work anymore ... ?)
    – eniem
    May 20, 2014 at 8:39
2

Here's a possible solution using titlesec, having in mind Incompatibilities between KOMA-Script and titlesec. The \widestnum length controls the separation between the numbers and the titles; I set it to be 3em, but you can change it to be the value that best suits your needs:

\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[explicit]{titlesec}

\usepackage{mparhack}

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

\newlength\widestnum
\setlength\widestnum{3em}

\titleformat{\chapter}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \colorbox{gray!20}{%
        \parbox{\textwidth}{%
          \parbox[t]{\widestnum}{\thechapter}%
          \parbox[t]{\dimexpr\textwidth-\widestnum\relax}{\strut#1\strut}%
        }%
      }%
    }%
  }
\titleformat{\section}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \fbox{%
        \parbox{\textwidth}{%
          \parbox[t]{\widestnum}{\thesection}%
          \parbox[t]{\dimexpr\textwidth-\widestnum\relax}{\strut#1\strut}%
        }%
      }%
    }%
  }
\titleformat{\subsection}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \fbox{%
        \parbox{\textwidth}{%
          \parbox[t]{\widestnum}{\thesubsection}%
          \parbox[t]{\dimexpr\textwidth-\widestnum\relax}{\strut#1\strut}%
        }%
      }%
    }%
  }
\titleformat{name=\chapter,numberless}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \colorbox{gray!20}{%
        \parbox{\textwidth}{\strut#1\strut}%
      }%
    }%
  }
\titleformat{name=\section,numberless}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \fbox{%
        \parbox{\textwidth}{\strut#1\strut}%
      }%
    }%
  }
\titleformat{name=\subsection,numberless}[hang]
  {\normalfont\bfseries\sffamily}
  {}
  {0em}
  {%
    \makebox[\linewidth]{%
      \fbox{%
        \parbox{\textwidth}{\strut#1\strut}%
      }%
    }%
  }

\titlespacing*{\chapter}
  {0pt}{20pt}{12pt}

\begin{document}

\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
\subsection{Test numbered subsection}
Text

\end{document}

An image showing the settings for unnumbered sectional units:

enter image description here

An image showing the settings for both numbered and unnumbered sectional units:

enter image description here

14
  • Thanks a lot for your answer! That looks pretty well, there's just one thing left I mentionned above ... the chapter/section/subsection-headers should be at the same position ... - I've drawn a line in your picture and will attach it to my first post - I think, then it's easier to understand what I mean.
    – eniem
    May 19, 2014 at 13:45
  • @eniem Please see my updated answer. May 19, 2014 at 13:52
  • ouh. just seen it! Thank's a lot! ... That's how it should be ... How did you find this solution or the 'material' and knowledge to come to this solution? (I also want to improve my Tex, that's why I'm asking ...)
    – eniem
    May 19, 2014 at 13:55
  • @eniem Regarding your question, well you can learn a lot by getting a good introductory LaTeX manual (The LaTeX Companion, for example) and by reading the packages documentation; then you can start reading lots of code (from packages, classes, etc.); you can also hang around here and study some of the great answers provided by some of the members of this community. May 19, 2014 at 14:06
  • Do you also want to indent unnumbered titles or are they OK in my answer? May 19, 2014 at 14:13
1

With newer KOMA-Script you should not and need not use titlesec. You can redefine \chapterformat, \sectionformat etc. to left align the heading numbers in a box of the same width. And you can redefine \chapterlinesformat and \sectionlinesformat to put the heading in colorbox or a frame:

\documentclass[oneside,BCOR=5mm]{scrbook}% all other options are default

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[automark]{scrlayer-scrpage}% obsolete scrpage2 replaced
\usepackage{xcolor}

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

\usepackage[demo]{graphicx}

\newlength{\widestnumberwidth}
\setlength{\widestnumberwidth}{1.5cm}
\renewcommand*{\chapterformat}{\makebox[\widestnumberwidth]{\thechapter\autodot}}
\renewcommand*{\sectionformat}{\makebox[\widestnumberwidth]{\thesection\autodot}}
\renewcommand*{\subsectionformat}{\makebox[\widestnumberwidth]{\thesubsection\autodot}}
\renewcommand*{\subsubsectionformat}{\makebox[\widestnumberwidth]{\thesubsubsection\autodot}}


\makeatletter
\renewcommand*{\chapterlinesformat}[3]{%
  \colorbox{gray}{%
    \parbox{\dimexpr\linewidth-2\fboxsep}{\raggedchapter
      \@hangfrom{\strut#2}{#3}%
    }%
  }%
}
\renewcommand*{\sectionlinesformat}[4]{%
  \fbox{%
    \parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{\raggedsection
      \@hangfrom{\strut\hskip #2#3}{#4}%
    }%
  }%
}

\makeatother

\begin{document}

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

\end{document}

If you want to reduce the distance between the frame line and the heading, you can change \fboxsep.

For more complex framing see, e.g., Emulation von fncychap-Stil Glenn mit KOMA-Script-Mitteln (in German). Usage of pgf/tikz inside \chapterlinesformat or \sectionlinesformat would also be possible.

You must log in to answer this question.

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