# Line shown behind filled rectangle made with TikZ

I need to create a TikZ style that is a filled rectangle with a line crossing it.

The problem I'm facing now is that when the rectangle is filled with a color, then the line is drawn behind the rectangle, and I need it above.

The MWE:

\documentclass[12pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc, shapes.geometric}

\tikzstyle{mainfig} = [
minimum width=16mm,
minimum height=5mm,
text centered,
draw=black,
fill=orange!80,
line width=1pt,
rectangle,
append after command={
\pgfextra{
\draw [line width=2pt]
($(\tikzlastnode.north west)+(3mm,5mm)$)--
($(\tikzlastnode.south west)+(3mm,-5mm)$);
}
}
]

\begin{document}

\begin{tikzpicture}[font=\footnotesize]
\node (test) [mainfig] {\ldots};
\end{tikzpicture}

\end{document}


This is what I get:

How can I force the line drawn after append after command to be drawn above the filled figure?

Update:

I've confirmed that this code doesn't work with pgf 2.10, nor with 3.0.0.

Also, I've seen that this problem is perfectly solvable using pgf 3.0.0, thanks to the answers of @PaulGessler and @PaulGaborit, but for now I should like a solution working for both versions of pgf. I hope that is not too much to ask for.

In particular, I need a working solution with pgf 2.10 support, because I will use this code in a project to be worked collaboratively, and as of now, both ShareLaTeX and writeLaTeX use that version of pgf.

If nothing new appears, I will gladly accept the most voted of those answers.

-
Did you update to v3.00 ? –  percusse Mar 14 at 21:42
No, I have pgf version 2.10 (TexLive 2012). –  Nicolás Mar 15 at 4:01

One method is to use layers. Declare the foreground (fg) layer with \pgfdeclarelayer{fg} and set the order of layers (bottom to top) with \pgfsetlayers{main,fg}.

Then, anywhere in a pgf-based environment, you can use

\begin{pgfonlayer}{fg}
<drawing commands>
\end{pgfonlayer}


to draw in the foreground.

A note: the shapes.geometric library is not required; rectangle is included in the base package.

The Code

\documentclass[12pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\pgfdeclarelayer{fg} % declare foreground layer
\pgfsetlayers{main,fg} % set layer order

\tikzstyle{mainfig} = [
minimum width=16mm,
minimum height=5mm,
text centered,
draw=black,
fill=orange!80,
line width=1pt,
rectangle,
append after command={
\pgfextra{
\begin{pgfonlayer}{fg}
\draw [line width=2pt]
($(\tikzlastnode.north west)+(3mm,5mm)$)--
($(\tikzlastnode.south west)+(3mm,-5mm)$);
\end{pgfonlayer}
}
}
]

\begin{document}

\begin{tikzpicture}[font=\footnotesize]
\node (test) [mainfig] {\ldots};
\end{tikzpicture}

\end{document}


The Output

-
This example doesn't work for me. BTW, I'm using TexLive 2012. I think I should not need a so up-to-date version to get working something as simple as this, but I may be wrong. –  Nicolás Mar 15 at 3:51
Using sharelatex doesn't work either: sharelatex.com/project/… –  Nicolás Mar 15 at 4:21
@Nicolás, you might try adding \usetikzlibrary{backgrounds} based on the second paragraph of section 3.13 of the pgf 2.10 manual. Failing that, I'm not sure what's going on. –  Paul Gessler Mar 17 at 4:50
I've just tested your answer with TexLive2013 (pgf 3.0.0) and works perfect. But I should want a solution working with the not so up-to-date pgf as well. –  Nicolás Mar 21 at 19:11

The append after command key appends nothing after drawing the node. It appends material to the current path where the node entry appears. Here is an example to illustrate its usage.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0)  -- (1,0) node{A} -- (2,0);
\draw[red] (0,1)  -- (1,1)  node[append after command={-- +(0,1)}] {A}  -- (2,1);
\end{tikzpicture}
\end{document}


When TikZ draws a path with some nodes, first it draws the path (and collects all the nodes) then it draws the nodes. With TikZ 3.0, you may apply the behind path option to a node to draw it before the current path. Here is in example of its usage (combined with append after command):

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
mainfig/.style={
minimum width=16mm,minimum height=5mm,text centered,
draw=black,fill=orange!80,
line width=1pt,rectangle,
append after command={
($(\tikzlastnode.north west)+(3mm,5mm)$)--
($(\tikzlastnode.south west)+(3mm,-5mm)$)
},
},
}
\begin{document}
\begin{tikzpicture}[font=\footnotesize]
\draw[line width=2pt,draw=blue] node [mainfig] {\ldots};
\draw[line width=2pt,draw=red] (0,2) node [behind path,mainfig] {\ldots};
\end{tikzpicture}
\end{document}


-