[Tex/LaTex] Line shown behind filled rectangle made with TikZ

diagramstikz-pgf

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:

enter image description here

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.

Best Answer

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

enter image description here