[Tex/LaTex] Create fancy section titles by redefining the section command

sectioning

I am preparing a non-technical document and would like to add some spice to it. Doing so, I'm trying to redefine the section command that is provided by the KOMA Script classes.

My idea is that it should look something like this:

Mock-up

Since this is the first time that I am doing something like this, I am struggling. My philosophy is that the code does not need to be perfect, as long as it just works. I've been looking at some other code to get some inspiration but some things I can not figure out.

Here is how I am trying to tackle it:

  • Use TikZ to draw the shape
  • Copy paste the section command provided by the KOMA classes into \renewcommand and alter it there
  • Use \ifodd\value{page} ..then.. \else \fi to check for even/odd pages and adjust parameters accordingly
  • Use \hspace to move the arrow into the margin

Here is what I have come up with:

\documentclass{scrbook}

\usepackage{tikz,ifthen,blindtext}
    \usetikzlibrary{shapes.symbols,shadows}

\definecolor{visgreen}{rgb}{0.733, 0.776, 0}

\makeatletter
\renewcommand\section{\@startsection{section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
    \setlength{\parfillskip}{\z@ plus 1fil}\fi
    \ifodd\value{page}
        \hspace*{-\dimexpr\oddsidemargin+1in\relax}
        \begin{tikzpicture}[auto, font=\Huge, every node/.style={signal, draw, text=white, signal to=nowhere}]
            \node[visgreen, fill, signal to=east, inner sep=1em, drop shadow, text=white] at (0,0) {\raggedsection\normalfont\sectfont\nobreak\size@section};
        \end{tikzpicture}
    \else
    \hspace*{\dimexpr\evensidemargin+1in\relax}
    \begin{flushright}
        \begin{tikzpicture}[auto, font=\Huge, every node/.style={signal, draw, text=white, signal to=nowhere}]
        \node[visgreen, fill, signal to=west, inner sep=1em, drop shadow, text=white] at (0,0) {\raggedsection\normalfont\sectfont\nobreak\size@section};
    \end{tikzpicture}
    \end{flushright}
    \fi}%
}
\makeatother


\begin{document}

\section{Test}
\Blindtext
\section{Second Test}
\blindtext

\end{document}

Unfortunately, there are a few problems that I can not solve:

  • \oddsidemargin+1in is apprently not enough to move the arrow up to the page border
  • \hspace does not work at all on even pages
  • For some reason, the section name and number do not show up in the arrow, but next to it

Currently it looks like this (on an odd page):

enter image description here

It would be great if you could give me some hints how I can fix these issues. If I manage to solve the problem I might delve deeper into the matter and write some more fancy section titles and pacakge them.

Best Answer

Here's a quick solution, to be used as a starting point (it admits improvements), using the titlesec package:

\documentclass[twoside]{scrartcl}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}

\definecolor{visgreen}{rgb}{0.733, 0.776, 0}

% the tikz picture that will be used for the title formatting
% \SecTitle{<signal direction>}{<node anchor>}{<node horiz, shift>}{<node x position>}{#5}
% the fifth argument will be used by \titleformat to write the section title using #1
\newcommand\SecTitle[5]{%
\begin{tikzpicture}[overlay,every node/.style={signal, draw, text=white, signal to=nowhere}]
  \node[visgreen,fill, signal to=#1, inner sep=1em, drop shadow,
    text=white,font=\Huge\sffamily,anchor=#2,
    xshift=\the\dimexpr-\marginparwidth-\marginparsep-#3\relax] 
    at (#4,0) {#5};
\end{tikzpicture}%
}

\titleformat{name=\section,page=even}
{\normalfont}{}{0em}
{\SecTitle{east}{west}{16pt}{0}{#1}}[\addvspace{4ex}]

\titleformat{name=\section,page=odd}
{\normalfont\sffamily}{}{0em}
{\SecTitle{west}{east}{14pt}{\paperwidth}{#1}}[\addvspace{4ex}]

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two}
\lipsum[2]
\clearpage
\section{Test Section Three}
\lipsum[2]
\section{Test Section Four}
\lipsum[2]

\end{document}

enter image description here