6

I am trying to fill a custom shaped node but as can be observed in the example below the fill does not occur naturally rather it uses its default shape to fill. Any insights?

\documentclass[letterpaper]{article}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric,calc}

\setlength{\parindent}{0pt}

\begin{document}

\begin{tikzpicture}
\draw (0,0) -- (\textwidth-0.4pt, 0) node[
    fill=blue!20,
    append after command={[rounded corners=0pt](b.west)|-(b.north)},
    append after command={[rounded corners=0pt](b.north)-|(b.east)},
    append after command={[rounded corners=1pt](b.east)|-(b.south)},
    append after command={[rounded corners=5pt](b.south)-|(b.west)},
    anchor=north west,
    at start,
    text=white] 
(b) {Example} ;
\end{tikzpicture}
\end{document}

enter image description here

2

1 Answer 1

9

Insight: The fill is used for the actual rectangle shape. The append after command is just appended to the path and uses the style of the \draw (no filling).

Although Roelof Spijker has given an excellent solution, here’s a fix by using one full path.

Code

\documentclass[letterpaper]{article}
\usepackage{tikz}

\setlength{\parindent}{0pt}

\tikzset{weird fill/.style={append after command={
    \pgfextra
        \draw[sharp corners, fill=#1] (\tikzlastnode.west) |- (\tikzlastnode.north east) [rounded corners=1pt] |- (\tikzlastnode.south) [rounded corners=5pt] -|  (\tikzlastnode.north west) -- cycle;
    \endpgfextra}}}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- (\textwidth-0.4pt, 0) node [
    weird fill=blue!20,
    anchor=north west,
    at start,
    text=white] {Example} ;
\end{tikzpicture}
\end{document}

Output

enter image description here

2
  • What does \pgfextra do?
    – azetina
    Mar 6, 2013 at 1:28
  • @azetina The PGF manual tells us (note that \pgfextra{<code>} and \pgfextra <code> \endpgfextra is the same): “This command may only be used inside a TikZ path. There it is used like a normal path operation. The construction of the path is temporarily suspended and the <code> is executed. Then, the path construction is resumed.” The append after command entry will be used in the middle of a path and this would result in an error as we would have had \draw <stuff> \draw <inner stuff>; <other stuff>;. Mar 6, 2013 at 1:35

You must log in to answer this question.

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