3

According to this answer, I created a style for drawing rectangles with double lines. The problem is, if I pass options like fill=color or thick to the node which I create with the style, these options are obviously not passed to the outer node, so the outer node won't be drawn correctly. (See MWE)


Remark: I can't use the double key, as I normally want the gap between the two lines to be transparent. This is why I use the method of the answer I linked.


So the question is, is there a way to pass the options of the style (nonlin) to the \pgfextra or to extract the properties of the \tikzlastnode and reuse it then? Why do I need to use \pgfextra here anyway? Or am I just overseeing a trivial solution? Notice the wrong line width of the outer node and the wrong filling of the gap of the first node:

MWE

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit, positioning}

\tikzset{
    nonlin/.style = {
        rectangle,thick,draw,minimum width=1cm,minimum height=0.6cm,
        append after command={
            \pgfextra{
                \node[fit=(\tikzlastnode), draw, inner sep=1.8pt] (\tikzlastnode-outer) {};
            }
        }
    }
}

\begin{document}
\begin{tikzpicture}
    \node(mynode)[nonlin,fill=blue!20]at(0,0){$a$};
    \node(node2)[nonlin, right=of mynode]{$b$};
    \begin{scope}[on background layer]
    \node[fit=(mynode)(node2), inner sep=5pt,fill=gray!30]{};
    \end{scope}
    \draw[->,thick](mynode-outer)--(node2-outer);
\end{tikzpicture}
\end{document}
2
  • I'm not really sure what you're trying to accomplish here. Do you just want a fill directive to get shard by a family of nodes? Perhaps if you could clarify what kind of behavior your looking for we could help. But right now, I'm not sure how you want things to behave.
    – A.Ellett
    Jan 28, 2016 at 18:42
  • I'm sorry if I'm unclear. I'm creating the outer node by the nonlin style. What I want to achieve is that if I pass further options to the (inner) node like for example thick or fill=color, then I want the outer node to also have these styles. More clear now? (Sorry, I'm not able to insert the ' signs for code on mobile device...)
    – Ktree
    Jan 28, 2016 at 18:54

1 Answer 1

5

Is this what you wanted?

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit, positioning}

\tikzset{
    nonlin/.style = {
        rectangle,thick,draw,minimum width=1cm,minimum height=0.6cm,#1,
        append after command={
            \pgfextra{
                \node[fit=(\tikzlastnode),#1, draw, inner sep=1.8pt] (\tikzlastnode-outer) {};
            }
        }
    }
}

\begin{document}

\begin{tikzpicture}

    \node[nonlin={fill=blue!20}]     (mynode) at (0,0) {$a$};
    \node[nonlin, right=of mynode] (node2)           {$b$};

    \begin{scope}[on background layer]
      \node[fit=(mynode)(node2), inner sep=5pt,fill=gray!30]{};
    \end{scope}

    \draw[->,thick](mynode-outer)--(node2-outer);

\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks, this is a good solution. Still, I was hoping to find a way in which I don't have to pass the arguments by nonlin={...}... Why? Lets say for example I have a scope in which I want to change the background (or a different style) of all nonlin blocks. Then it would be nice to simply write nonlin/.append style={fill=white} or so. How could I achieve this here?
    – Ktree
    Jan 29, 2016 at 6:29

You must log in to answer this question.

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