# Create fancy section titles by redefining the section command

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:

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}

\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):

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.

-
–  cmhughes Apr 10 '12 at 17:38
Do the sections have a title or just a number? –  Marco Apr 10 '12 at 18:21
Title and number. But in fact I haven't seen that question yet. I'll give that approach a shot :). –  Ingo Apr 10 '12 at 18:44
The problem is that I can not put the section title into the node, but the signal shape is provided by a node. No idea how to tackle this using TikZ :-/. No idea whether the section title is accessible through any command at all, something like \sectiontitle does not exist. –  Ingo Apr 10 '12 at 19:44
@tehingo: you can use the explicit option for titlesec, as I did in my answer. –  Gonzalo Medina Apr 10 '12 at 19:57

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}
\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}

\titleformat{name=\section,page=odd}
{\normalfont\sffamily}{}{0em}

\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}


-
Great work Gonzalo Medina. There is one obvious problem :) The automatic numbering of sections, however, does not get passed to the tikz environment. Do you have any ideas how I might do that? –  macmadness86 Jul 4 '12 at 23:27

Here is a quick ConTeXt solution that can get you started. It still lacks several features, e.g. the section number is not converted to a text string and there's no distinction between even and odd pages. The spacing around the “arrow background” and the background itself could be optimized and the hard coded horizontal space should vanish.

\definecolor [chapcolour] [h=bbc600]

\define[2]\myseccmd{%
\hskip-2.2cm
\framed
[
frame=off,
background=chap,
foregroundcolor=white,
]{Section #1}}

\startuseMPgraphic{chap}
path p; path q;
p := origin -- (0cm,\overlayheight) -- (\overlayheight/2,\overlayheight/2) -- cycle;
q := origin -- (\overlaywidth,0cm) -- (\overlaywidth+\overlayheight/2,\overlayheight/2);
fill unitsquare xyscaled (\overlaywidth,\overlayheight) withcolor \MPcolor{chapcolour};
fill p xshifted \overlaywidth withcolor \MPcolor{chapcolour};
interim linecap := butt;
draw q withpen pencircle scaled 2pt withcolor \MPcolor{darkgray};
setbounds currentpicture to unitsquare xyscaled (\overlaywidth, \overlayheight);
\stopuseMPgraphic

\defineoverlay [chap] [\useMPgraphic{chap}]

\starttext
\startsection [title=Foo]
\input ward
\stopsection
\startsection [title=Bar]
\input knuth
\stopsection
\stoptext


It uses MetaPost, instead of Tikz. First I create a custom section command which uses a \framed-frame to easily include a background. Then I create a MetaPost graphic and use this graphic as an overlay, whichis included as a background behind the section titles.

-