1

I was trying to extend the answer here (code below) to subsection.

enter image description here

\documentclass{scrbook}

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \ifstr{#1}{section}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
      {\@hangfrom{\hspace*{#2}#3}{#4}}%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\Blindtext
\end{document}

I guess it's fairly straightforward to apply this to chapters with \chapterlinesformat, how do I reproduce this for a subsection?

1 Answer 1

1

If you want to use this layout for all section like titles (section, subsection and subsubsection) you can use

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

Example

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

enter image description here

If subsubsection should get the default layout use

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \Ifstr{#1}{subsubsection}
      {\@hangfrom{\hspace*{#2}#3}{#4}}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}%
}
\makeatother

Example:

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \Ifstr{#1}{subsubsection}
      {\@hangfrom{\hspace*{#2}#3}{#4}}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

enter image description here


If you want to use different colors depending on the section level you could use nested \Ifstring commands. But I would define a color name for each level (the color name must depend on the section level) and use these colors as left color.

Example:

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\colorlet{sectionbg}{blue!20}
\colorlet{subsectionbg}{orange!20}
\colorlet{subsubsectionbg}{green!20}

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=#1bg,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

enter image description here

2
  • Great! Thank you! I thought I could use another string as in \Ifstr{#1}{subsubsection} to change colors according to level, but it doesn't seem to work. Is there a way to do it?
    – user9424
    Commented Apr 10, 2020 at 20:06
  • See my updated answer.
    – esdd
    Commented Apr 11, 2020 at 10:04

You must log in to answer this question.