TikZ-PGF Styles – How to Pass Parameters to \tikzstyle?

multipart-nodeoptional argumentspgfkeystikz-pgftikz-styles

I am using multipart shapes to create two-sided nodes and want to change the background color of the left node based on a user-provided (color) parameter. Changing the default parameter at \tikzstyle{package}[green] also changes the background color in both outputs (Node 1+2), however I am not able to specify another color for some reason. Instead, the text color changes (Node 2).

The following example demonstrates this behavior:

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

\usetikzlibrary{shapes, shapes.geometric, shapes.multipart}
\tikzstyle{package}[green] = [
    rectangle split,
    rectangle split horizontal,
    rectangle split parts=2,
    rectangle split part fill = { 
        #1, gray
    },
    draw=black, very thick,
    minimum height=2em,
    inner sep=1cm,
    every one node part/.style={
        text=white,
        text width=5cm
    },
    every two node part/.style={
        text=white,
        text width=\linewidth-7cm
    }
]

\begin{document}
\begin{tikzpicture}
    \node[package] {Node 1 \nodepart{two} Lorem Ipsum \\ dolor sit amet};
\end{tikzpicture}

\begin{tikzpicture}
    \node[package][red] {Node 2 \nodepart{two} Lorem Ipsum \\ dolor sit amet};
\end{tikzpicture}
\end{document}

The following image shows the output. I want to fill the left side of the second node with red, instead of green.

enter image description here

I understand, that the red text color comes from the way, the node is drawn (i.e. the red does not actually specify my parameter, but instead the standard \node parameters). I also tried changing how to express this, for example \node[package][fill=red] or \node[package, red], but nothing worked as expected. So my question is: How do I add parameters to a tikzstyle and pass arguments when drawing a node? Also, is it possible to define multiple parameters?

Thanks in advance! 🙂

Best Answer

Everything about keys is detailed in section "87 Key Management" in pgf documentation. But about your code:

once you define a style with parameters, use style=parameter_value to change it. You construction [package][red] leaves package style with its default value and introduces the red as another parameter, not for filling.

If you need more than one parameter, you could use style_name/.style 2 args (see following code) or style_name/.style n args={arguments number}{style definition}. There's another option which allows a particular construction but I don't know how to use it.

By the way, I've changes old tikzstyle to tikzset (see Should \tikzset or \tikzstyle be used to define TikZ styles?)

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

\usetikzlibrary{shapes, shapes.geometric, shapes.multipart}
\tikzset{
    package/.style 2 args = {
        rectangle split, rectangle split horizontal,
        rectangle split parts=2,
        rectangle split part fill = { 
            #1, #2
        },
        draw=black, very thick,
        minimum height=2em,
        inner sep=1cm,
        every one node part/.style={
            text=white,
            text width=5cm
        },
        every two node part/.style={
            text=white,
            text width=\linewidth-7cm
        }
    },
    package/.default={green}{gray}
}

\begin{document}
\begin{tikzpicture}
    \node[package] {Node 1 \nodepart{two} Lorem Ipsum \\ dolor sit amet};
\end{tikzpicture}

\begin{tikzpicture}
    \node[package={red}{blue!20!black}] {Node 2 \nodepart{two} Lorem Ipsum \\ dolor sit amet};
\end{tikzpicture}
\end{document}

enter image description here

Related Question