12

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@[email protected]}\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.

6
  • 1
    have you seen how-to-place-a-shaded-box-around-a-section-label-and-name?
    – cmhughes
    Apr 10, 2012 at 17:38
  • Do the sections have a title or just a number?
    – Marco
    Apr 10, 2012 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, 2012 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, 2012 at 19:44
  • @tehingo: you can use the explicit option for titlesec, as I did in my answer. Apr 10, 2012 at 19:57

2 Answers 2

10

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

1
  • 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? Jul 4, 2012 at 23:27
5

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]
\setuphead   [section]    [command=\myseccmd]

\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

result

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.

You must log in to answer this question.

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