2

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! :-)

6

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

  • 2
    Curiously (because I thought it was optional), \nodepart{one} is necessary in both examples for the text style parameters (text and text width) to take effect. Odd. – sgmoye Jul 2 at 18:24
  • @sgmoye Yea, I forgot to include it in the MWE. I've also tried every upper node part in the style, but it didn't have any effect either. – Carsten Jul 3 at 8:03
  • @sgmoye nvm, my bad. Within the style definition you should not use the every one node part at all. Leave it out and set the text and text width properties directly (e.g. style = { ..., text=white, text width=2cm, every two node part/.style = { ... }, ... }and everything works as expected. – Carsten Jul 4 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.