You can use nodes with the fit
library by adding coordinates to each of the fit
lists that lie on the left and right borders of the bounding box, but at the same vertical position as the nodes you want to highlight. You can do this with the calc
library and its syntax ($(A)!(C)!(B)$)
, which projects the point (C)
on the line from (A)
to (B)
.
In the example below, I've used a script that takes a node name as an argument and returns three coordinates: The node itself, and its projection on the left and right borders of the bounding box. In order to make the new boxes all the same width, the bounding box needs to be "frozen", so one box doesn't influence the size of the next. You can do this by issuing \path [use as bounding box] (current bounding box.north west) (current bounding box.south east)
before the first background box, which stops the updating of the bounding box.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit,calc}
\begin{document}
\tikzset{
schritt/.style={
draw,
rounded corners,
fill=blue!20,
inner xsep=2em,
},
background/.style={
draw,
fill=yellow!30,
align=right
}
}
% Returns three nodes: The argument, and the projections of the argument on the left and right borders of the bounding box
\newcommand{\extendnode}[1]{
(#1)
($(current bounding box.north east)!(#1)!(current bounding box.south east)$)
($(current bounding box.north west)!(#1)!(current bounding box.south west)$)
}
\begin{tikzpicture}
\matrix (matrix) [row sep=0.5cm,column sep=0.5cm] {
\node (elicit) [schritt] {Wissenserhebung}; & \\
\node (interpret) [schritt] {Interpretation}; & \\
& \node (model) [schritt] {Modellierung}; \\
\node (representation) [schritt] {Repräsentation}; & \\
\node (integration) [schritt] {Integration}; & \\
\node (maintenance) [schritt] {Wartung}; & \\
};
\path[->]
(elicit) edge (interpret)
(interpret) edge node[right] {\hspace{.35cm}\tiny Modellbasierter Ansatz} (model)
(interpret) edge node[right] {\tiny Rapid Prototyping} (representation)
(model) edge (representation)
(representation) edge (integration)
(integration) edge (maintenance);
\begin{pgfonlayer}{background}
\path [use as bounding box] (current bounding box.north west) (current bounding box.south east); % Freeze current bounding box
\node [fit={\extendnode{elicit}}, background] {First};
\node [fit={\extendnode{interpret} (model)}, background] {Second};
\node [fit=\extendnode{representation}, background] {Third};
\node [fit=\extendnode{integration}, background] {Fourth};
\node [fit=\extendnode{maintenance}, background] {Fifth};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
The following assumes you want to draw a rectangle, give two numbers (n
and k
) for the number of divisions in x
and y
direction and a list of 'coordinates' defining which subdivisions should be filled. The following code achieves this:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\rectDiv#1#2#3#4#5{%#columns, #rows, rectangle start, rectangle end, list of elements to fill
\begin{tikzpicture}
\draw #3 rectangle #4;
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path #4;
\pgfgetlastxy{\secondx}{\secondy}
\pgfmathsetlengthmacro{\xdiff}{\secondx-\firstx}
\pgfmathsetlengthmacro{\ydiff}{\secondy-\firsty}
\pgfmathsetlengthmacro{\myxstep}{\xdiff/#1}
\pgfmathsetlengthmacro{\myystep}{\ydiff/#2}
\foreach \x in {1,...,#1}{
\draw ($#3 +\x*(\myxstep,0)$) -- ($#3 +(0,\ydiff) +\x*(\myxstep,0)$);
}
\foreach \y in {1,...,#2}{
\draw ($#3 +\y*(0,\myystep)$) -- ($#3 +(\xdiff,0) +\y*(0,\myystep)$);
}
\foreach \i/\j in {#5}{
\path[fill=blue!20,draw] ($#3 + (\i*\myxstep,\j*\myystep)$) rectangle ($#3 + (\i*\myxstep,\j*\myystep) + (\myxstep,\myystep)$);
}
\end{tikzpicture}
}
\begin{document}
\rectDiv{7}{5}{(1,1)}{(4,3)}{0/0,1/1,2/0,5/3}
\end{document}
The parameters are as follows:
- Number of columns
- Number of rows
- Rectangle start coordinate
- Rectangle end coordinate
- List of index pairs to be filled
The list of index pairs is to be given in an i/j
fashion. Where the box denoted (n_i,k_j)
is then filled. Using your notation.
You could potentially change it such that you only specify the endpoints of the rectangle, assuming it starts at (0,0)
. The indexing of the subdivisions starts at 0. The result is the following:
Update: After comment. It's quite easy to modify #4
to be (width, height)
instead of the end coordinate. Since the end coordinate is simply (start) + (width,height)
. This can cause some problems in the path and using \pgfgetlastxy
though and therefore we also define an extra coordinate. The code can be modified by replacing
\draw #3 rectangle #4;
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path #4;
with
\draw #3 rectangle ($#3 + #4$) coordinate (end);
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path (end);
Changing the example to
\rectDiv{7}{5}{(1,1)}{(3,2)}{0/0,1/1,2/0,5/3}
yields the exact same result. Note that you have to specify (width,height)
and not (height,width)
. This is far easier, because it allows for the simple addition.
Best Answer
Here an attempt using default
fancyhdr
andtikzpagenodes
. I didn't check if it's working with KOMA too. If you need additional help just add a comment.Till now i didn't got it working for forced line-breaks (
\\
) in the header. Perhaps you find a solution of adding support for this on your own. I also did not have a closer look at thetikzpagenodes
documentation. Should be worth a try.Renderd document: