5

I want to define my own style for section, subsection and subsubsection, with koma script (I know how to do with titlesec, but I need the "koma's version").

I have success with the section and the subsubsection, but not with the subsubsubsection. I have this :

enter image description here

I would like to have for example :

  1. section

1.1. subsection

(a) subsubsubsection (colored in green for example)

Here's my code

\documentclass[10pt]{scrreprt}%scrartcl,scrreprt,scrbook

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage[]{libertine}
\usepackage[libertine]{newtxmath}
\usepackage{microtype}
\usepackage{setspace}
    %\onehalfspacing

\renewcommand*\thesection{\arabic{section}.}
\renewcommand*\thesubsection{\arabic{section}.\arabic{subsection}}
\renewcommand*\thesubsubsection{\alph{subsubsection}}


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

\begin{document}

\section{section}

\subsection{subsection}

\subsubsection{subsubsection}

\end{document}
2
  • Why are you using \arabic{subsubsection} if you want it to be (\alph{...})?
    – TeXnician
    Feb 9, 2017 at 21:29
  • just a mistake ... it was for a first try. I'll edit it... but the problem still remain even with \arabic{subsubsection}, I don't have what I expect.
    – Stan
    Feb 9, 2017 at 21:31

1 Answer 1

6

Do you really want to use section numbers without chapter number in a scrreprt document?

Here is a suggestion assuming all section levels using sectionlinesformat should be colored:

\documentclass[10pt,
  numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}

\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}

\setcounter{secnumdepth}{\subsubsectionnumdepth}

\colorlet{sectioncolor}{red}
\colorlet{subsectioncolor}{blue}
\colorlet{subsubsectioncolor}{green!80!black}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \tikz\node[
      inner xsep=1em,inner ysep=1ex,
      left color=#1color!20,right color=white,
      rounded corners,text=#1color
    ]{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};
}
\makeatother

\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}

enter image description here


Or assuming all section levels formatted by sectionlevelsformat should use nodes:

\documentclass[10pt,
  numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}

\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}

\setcounter{secnumdepth}{\subsubsectionnumdepth}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}

\tikzset{
  section/.style={text=red,left color=red!20,right color=white},
  subsection/.style={text=blue,left color=blue!20, right color=white},
  subsubsection/.style={}
}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \tikz\node[
      inner xsep=1em,inner ysep=1ex,
      rounded corners,
      #1
    ]{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};
}
\makeatother

\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}

Now the subsubsections are not colored (as asked in a comment):

enter image description here


If each level should get a different layout, then you have to use nested \ifstr{#1}{<section level name>} commands:

\ifstr{#1}{section}
  {<code for sections>}
  {\ifstr{#1}{subsections}
    {<code for subsections>}
    {<code for all other levels>}
  }%

Example:

\documentclass[10pt,
  numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}

\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}

\setcounter{secnumdepth}{\subsubsectionnumdepth}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}

\tikzset{
  sectionlinesformat/.style={
    inner xsep=1em,inner ysep=1ex,
    rounded corners
  }
}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#1}{section}
    {\tikz\node[sectionlinesformat,left color=red!20,right color=white,text=red]
      {\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
    {\ifstr{#1}{subsection}
      {\tikz\node[sectionlinesformat,left color=blue!20,right color= white,text=blue]
        {\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
      {\@hangfrom{\hspace*{#2}#3}{#4}}%
    }%
}
\makeatother

\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}
4
  • thank you for this code, I'll have to work a little to understand :) I don't have chapter at this time, but in the future I'll have them. You assumed that all the section was colored ... how to remove this for the subsubsection ?
    – Stan
    Feb 9, 2017 at 21:51
  • I have added two examples with none colored subsubsections. But I do not really understand what the desired result is. May be you only want to use nested \ifstr commands.
    – esdd
    Feb 9, 2017 at 23:04
  • Thank you so much. These examples will greatly assist me. I didn't understand these things with the Koma script manual (in particular this \ifstr and what means #1 #2 #3 #4 ...). Have to learn more :)
    – Stan
    Feb 9, 2017 at 23:09
  • In fact ... I'm learning about this package ... many things I don't understand, and the manual doesn't help me so much (may be because of my ... old english). Then I don't really know what result I want, but this code is a beginning ... to learn.
    – Stan
    Feb 9, 2017 at 23:14

You must log in to answer this question.

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