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@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

eniem
  • 189

3 Answers3

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}}}} 
  • 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 '14 at 13:58
  • see edited answer. If you do not want the space for teh unnumbered sections delete the first \makebox[40pt]{} –  May 19 '14 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? – Gonzalo Medina May 19 '14 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. –  May 19 '14 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 '14 at 08:39
  • Without an example difficult to say. But way do you textpos? Put the image simply into the footer with \lefoot or \lofoot –  May 20 '14 at 09:09
  • @Herbert I use textpos 'cause my picture has to remain at the right side of the paper. I've added a code-example to the end of my 'main'-question – eniem May 20 '14 at 13:47
  • I edited my answer. Look at the end. However for a oneside dicument it makes no real sense to use \ifoot which is for inner in twoside mode. \makebox(0,0){...} creates a box of width/height (0,0) so you can place contents with \put anywhere. –  May 20 '14 at 14:12
  • @Herbert thank you! Unfortunately I just found something other: when I use your example above and I add a \newpageright before \chapter{Test numbered section} then the header-entry on the second page of the document still remains 'Inhaltsverzeichnis' (that's correct for the first page) but it should be 'Test numbered section', 'cause starting with page 2 that's the 'acutal' chapter ... I don't know, why latex doesn't find the headmark, when I write \pagestyle{scrheadings} \clearscrheadfoot\ohead[\headmark]{\headmark} right after \begin{document} ... – eniem May 20 '14 at 15:28
  • in my definitions the \markboth{#1}{#1} is missing. I'll have a closer look tomorrow. –  May 20 '14 at 15:29
  • @Herbert Ok. Thanks a lot in advance! One last thing I just found out is that there's maybe a kind of incompatibility with the package titletoc? - I use it to format the table of contents ... I'll edit my first question and post a code-snippet and the results I get. ... maybe there's a better way for formatting the table of contents without the package titletoc ... but I don't know :/ – eniem May 21 '14 at 09:46
  • I tried it in several ways, but there are too many problems with titletoc etal, which refers to the section numbering and style. –  May 21 '14 at 20:27
  • hmh. I see, maybe I'll find some ways for formatting by using koma-script ('cause I use scrbook) ... anyway ... but - did you find a solution for getting the right headmark? – eniem May 21 '14 at 21:27
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

Gonzalo Medina
  • 505,128
  • 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 '14 at 13:45
  • @eniem Please see my updated answer. – Gonzalo Medina May 19 '14 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 '14 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. – Gonzalo Medina May 19 '14 at 14:06
  • Do you also want to indent unnumbered titles or are they OK in my answer? – Gonzalo Medina May 19 '14 at 14:13
  • No, they're ok! – eniem May 19 '14 at 14:54
  • @Gonzo Medina: It looks like that the frame/colorboxes have a kind of padding - how is it possible to set it's value to zero? (Or how to change the boxes heights?) – eniem May 20 '14 at 08:37
  • @eniem do you mean vertically or horizontally? If the former, then suppress the \strut commands from the code (but then, letters with descendants will produce inconsistent heights for the boxes). If the later, let me know and I'll provide the code. – Gonzalo Medina May 20 '14 at 12:18
  • thanks for your help; at the moment I meant vertically ... but I'll check it out this evening and 'll then write if there remain some questions =) But thanks in advance! – eniem May 20 '14 at 13:44
  • @eniem OK. But aren't you using the other answer? – Gonzalo Medina May 20 '14 at 13:46
  • no I use your answer; but I also want to improve my latex and so I tried to find out why there are those described problems with \textpos. The other thing is that I'm pretty new in the topic of redefining commands and the 1st answer is a little bit difficult to understand. Actually I try to set up two documents leading to the same results but using different solutions ... just for learning. - Unfortunately I have to ask so much 'cause noone of my collegues uses latex ... :) – eniem May 20 '14 at 13:58
  • @eniem Ah, I see. No problem. Ask what you want. We are here to help and we gladly answer questions from people eager to learn :) – Gonzalo Medina May 20 '14 at 14:08
  • @Gonzo Medina ... yes, but - unfortunately - it's not easy ;) ... but it's cool that there's a platform like the one here =)

    Ah btw, I just tried out removing the 'struts' ... - there still remains a little 'padding'. It's not much but it would be cool if there's a possibility to reduce it to get a result like the one I posted in the first attached picture in the 'main' question...

    – eniem May 21 '14 at 22:46
  • @Gonzo Medina: ah, i finally found a solution: If I define \setlength{\fboxsep}{2pt} then the boxes look like I want them! Thanks again for helping me! – eniem May 28 '14 at 13:25
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.

Schweinebacke
  • 26,336