4

I have a very similar question to this old (and already answered) one:

Giving headlines a background color spanning across the entire typearea

However, the solutions provided there only work for sections and below. As I want to use the class "scrreprt" from komascripts, I want to be able to give a background color to chapter headings as well. It should work for levels below, too.

Therefor I tried adapting the given solution in the following way:

\documentclass{scrreprt}

\usepackage{xcolor}
\usepackage{lipsum}

\addtokomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange}{%
    \begin{minipage}{\linewidth}%
        \vspace*{2pt}%Space before
        #1
        \vspace*{2pt}%Space after
    \end{minipage}%
}}

\addtokomafont{chapter}{\mychapter}
\newcommand{\mychapter}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{red}{%
\begin{minipage}{\linewidth}%
    \vspace*{2pt}%Space before
    #1
    \vspace*{2pt}%Space after
\end{minipage}%
}}

\begin{document}
\tableofcontents
\chapter{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long chapter}
\lipsum[1]
\section{Example section}
\lipsum[1]
\section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[1]
\end{document}

This gives me the following result:

Working background color for sections, not working background color for chapters Working background color for the toc

The solution works fine for \section but not for \chapter. For \chapter the title gets flushed to the outside margin. Interestingly enough, it seems to work for "chapters" such as the toc, that were created automatically.

I also tried this solution from the editor of the koma scripts and edited it a little:

\documentclass{scrreprt}
\usepackage{xcolor}

\addtokomafont{section}{%
    \makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
    }%
}

\addtokomafont{chapter}{%
    \makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
    }%
}
\begin{document}
\chapter{Chapter - not ok}
\section{Section - ok}
\end{document}

This also works well for sections. For chapters however, the background color spans wider to the margin that the text's width as seen here:

enter image description here

The provided code from Ulrike's link works for chapters, but only chapters:

\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}
\addtokomafont{chapter}
\makeatletter
\renewcommand*{\@@makechapterhead}[1]{%
\chapterheadstartvskip
\begin{mdframed}[style=chapter]
\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@chapterprefix
        \expandafter\size@chapterprefix
      \else
        \expandafter\size@chapter
      \fi
      {\chapterformat}%
      \if@chapterprefix
        \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
      \fi
  \fi
}%
{\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}
\nobreak\chapterheadendvskip
}
\renewcommand*{\@@makeschapterhead}[1]{%
\chapterheadstartvskip%
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
\nobreak\chapterheadendvskip%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{Introduction}
\lipsum[1]
\chapter{Main Part}
\lipsum[2]
\chapter{Conclusion}
\lipsum[3]
\section{Summary}
\section{Future developments}
\end{document}

The result is almost exactly what I want:

Background for chapter working

The background is only limited to chapters now and I don't know any TeX in order to know how to adept this to sections (and possibly subsections etc.). I tried it by replacing chapter with section but this (obviously?) didn't work.

Is there a possibility to fix the heading's alignment in the 1st way or make the background color smaller in the 2nd way or adept the 3rd way to sections as well?

Thanks in advance and best regards :)

8
  • Hi and welcome, off-topic question: You want to much lighter colors in your real document, right? Right now, this hurts my eyes, and i guess i am not the only one.
    – Johannes_B
    Mar 26, 2015 at 15:12
  • Adding material to a font command it brave. Both your examples don't work as the chapter font is used in more than one place, more precisly at least once for the number and once before the text. It would be better to redefine chapter completly than to try such hacks. See e.g. tex.stackexchange.com/a/146510/2388 Mar 26, 2015 at 15:14
  • Johannes_B, of course. This is just for better visualisation. Sorry for the eye-cancer :) Mar 26, 2015 at 15:27
  • @UlrikeFischer I added the content of your link to the question. It almost solves the question, almost. Mar 26, 2015 at 17:50
  • 1
    And why don't you continue to use the working solution for sections? Mar 26, 2015 at 17:54

1 Answer 1

2

So, I found a solution that does everything I want, although it might be a nasty hack:

\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{lipsum}

\usepackage{xcolor}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}

\addtokomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange}{%
    \begin{minipage}{\linewidth}%
        \vspace*{2pt}%Space before
        #1
        \vspace*{2pt}%Space after
    \end{minipage}%
}}

\addtokomafont{chapter}
\makeatletter
\renewcommand*{\@@makechapterhead}[1]{%
\chapterheadstartvskip
\begin{mdframed}[style=chapter]
\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@chapterprefix
        \expandafter\size@chapterprefix
      \else
        \expandafter\size@chapter
      \fi
      {\chapterformat}%
      \if@chapterprefix
        \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
      \fi
  \fi
}%
{\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}
\nobreak\chapterheadendvskip
}
\renewcommand*{\@@makeschapterhead}[1]{%
\chapterheadstartvskip%
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
\nobreak\chapterheadendvskip%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{A chapter}
\lipsum[1]
\section{A section}
\lipsum[2]
\chapter{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long chapter}
\lipsum[3]
\section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[4]
\end{document}

This gives you colored background for headings of automatically created chapters like toc and so on:

Colored background (toc)

This also gives you colored background of short chapter and section headings:

Colored background (single lines)

Last but not least this also gives you colored background of very long sections and chapter headings:

Colored background (multiple lines)

I hope this will be of some help to someone in the future :) Thanks for the help.

You must log in to answer this question.

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