Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Background

Writing a manual using KOMA Script. The default fonts are perfect and should not be changed.

Problem

KOMA Script produces the following section outline:

enter image description here

The desired outline looks similar to the following (gradient extended to left and right margins):

enter image description here

The troubles I have encountered:

  • The fount for numerals and text changes.
  • Retaining the text spacing.
  • Expanding or extending the gradient box to:
    • the left and right margins (\textwidth?); and
    • a bit larger than the section (or subsection) text's top and bottom margins.

Source Code

My first attempt resembles:

\let\oldsection\section
\renewcommand{\section}[1]{%
  \oldsection[#1]{
    \setlength\fboxrule{0pt}%
    \setlength\fboxsep{0pt}%
    \tikz[background rectangle/.style={left color=blue!20,right color=white},
    show background rectangle] 
    \node [inner sep=0pt] (0,0) {#1};%
  }
}

This produces:

enter image description here

Update

Changing the text width for the node helps:

\node [text width=\textwidth, inner sep=0pt] (0,0) {#1};%

This produces:

enter image description here

Related

Question

How would you implement a margin-to-margin gradient (preferably using TikZ) that is slightly larger than the text?

share|improve this question

1 Answer 1

up vote 21 down vote accepted

This is relatively easy to achieve using the titlesec package:

\documentclass{scrbook}
\usepackage{tikz,titlesec}

\usepackage[english]{babel}
\usepackage{blindtext}      % for a test document

\titleformat{\section}[block]%              
    {\usekomafont{sectioning}\usekomafont{section}%
     \tikz[overlay] \shade[left color=blue!20,right color=white] (0,-1ex) rectangle (\textwidth,1em);}%    
    {\thesection}%                   
    {1em}%
    {}

\begin{document}
\blinddocument 
\end{document}

example

Note the following:

  • \usekomafont{sectioning}\usekomafont{section} sets the font to whatever is chosen using the KOMA Script styling mechanism.

  • The [overlay] option of \tikz means that TikZ won't take any space.

  • This breaks if the title is longer than a line.


Edit: Here is second version, that works with long lines and does not ignore ascenders and descenders. The code is more complicated, though. It draws the whole section heading with TikZ.

\documentclass{scrbook}
\usepackage{tikz,titlesec}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand\boxedsection[1]{{%
    \usekomafont{sectioning}\usekomafont{section}%
    \begin{tikzpicture}[inner sep=0pt, inner ysep=0.3ex]
        \node[anchor=base west] at (0,0) (counter) {\thesection};
        \path let \p1 = (counter.base east) in node[anchor=base west, text width={\textwidth-\x1-0.33em}] (content) at ($(counter.base east)+(0.33em,0)$) {#1};
        \begin{pgfonlayer}{background}
            \shade[left color=blue!20,right color=white] let \p1=(counter.north), \p2=(content.north) in
            (0,{max(\y1,\y2)}) rectangle (content.south east);
        \end{pgfonlayer}
    \end{tikzpicture}%
}}

\titleformat{\section}%              
    {}%
    {}%
    {0pt}%
    {\boxedsection}%

\begin{document}
\chapter{test}
\section{Overview}
\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}
\end{document}

example

share|improve this answer
    
@Dave: That would require a different approach: the way I implemented this, the drawing command does not know what the actual content is. I might try something different (that also solves the multiline problem) later on, but right now I have other work to do. –  Caramdir Feb 28 '11 at 1:32
    
@Dave: I updated the answer with a second solution –  Caramdir Feb 28 '11 at 3:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.