5

Please consider the following dummy MWE:

\documentclass[border=3mm,tikz,preview]{standalone}
    \usetikzlibrary{arrows,positioning,shadows,shapes}

   \tikzset{test/.style = {%
             > = angle 90,
YY/.style args = {##1/##2/##3}{
    % changed in each use of shape
        name=n##1,
    % different in each picture
        fill=##2,% color
        text width=##3,
    % common in each picture
    shape=rectangle, draw, inner sep=1mm, minimum height=9mm,
    align=flush center, drop shadow},       }
            }
\begin{document}
     \begin{tikzpicture}[test,
     node distance = 12mm,
                        ]
\node[YY=1/white/12mm]                {node A};
\node[YY=2/white/12mm,right=of n1]    {node B};
    \draw[->] (n1) -- (n2);
    \end{tikzpicture}
\end{document}

Is it possible to set optional parameters #2 and #3 as default in some picture and then change only parameter #1 on the following way:

\node[YY=1]                {node A};
\node[YY=2,right=of n1]    {node B};
    \draw[->] (n1) -- (n2);

I'm aware of possibilities of `YY/.default = 1/white/12mm, but it has sense only if the all nodes have the same name (in considered dummy case). Then ones can write:

\node[YY]                {node A};
\node[YY,right=of n1]    {node B};

In this case the names can be put in (...) after node declaration, but this in the case, that node has defined node contents is not possible ...

Edit: My main intention is to make node description (determining parameters) as well presets in each TikZ picture as short as possible and for one, who aware of defined presets, make it intuitive. In this I wonder, if it is possible to define optional parameters on similar way as can be defined for example \newcommand in LaTeX:

\newcommand{maycomand}[2][optional parameter] {command definition};

In sense of my (dummy) example in the case, that node use preset parameters, the node declaration is

\node[YY=1] {node content};

and in the case, when I like to change default optional values, I can simple write:

\node[YY=1/red!20/22mm] {node content};

So far (for simplified case of given MWE) I can do the following:

  • as say @cfr in his comment (and I was not aware before) in node preset determine only option #2 and #3 and name of node write in braces like: \node (name) [YY] {node contents}; when I use default values for optional parameters, and \node (name) [YY=red!20/22mm] {node contents}; when I change default values.

  • not use optional arguments in node preset and in each picture add in its preamble YY/.append style = {fill=white, text width=12mm} and in case, that I like to have different color and width, this locally overwrite as \node[YY=name,fill=red!20,text width=22mm] {node content};

  • To be honest, I've now read this at least twice and I still don't fully understand it. I find the sentence 'In this case the names can be put... is not possible...' especially difficult. – cfr Jul 12 '15 at 1:48
  • What is the difference between center and flush center, by the way? I've never seen this before... – cfr Jul 12 '15 at 2:02
  • 1
    For flush center see manual, section 17.4.3 on pp. 224 (bottom, in CVS version of TikZ). flush center suppress hyphenation, only center not. – Zarko Jul 12 '15 at 2:38
  • 1
    Sorry. But why can't you put the name before? \node (name) [YY]? But, in any case, I still don't really see what the problem is. Why are you set on YY/.style args ={thing/thing/thing}? Why that syntax given that it then means you need to specify 3 args? – cfr Jul 12 '15 at 2:49
  • 1
    @cfr, I was not aware for possibility to put name before option of node. This change some disposition in my question. I need to think about this (now is to early in the morning here for this :-) ) . – Zarko Jul 12 '15 at 3:00
3

As I say, I'm not entirely sure I understand what you are trying to do. In particular, I'm not sure why you are nesting a style definition within another in this particular case.

But perhaps this will help:

\documentclass[border=3mm,tikz,multi]{standalone}
\usetikzlibrary{arrows,positioning,shadows,shapes}

\tikzset{
  YY fill/.store in=\yyfill,
  YY width/.store in=\yywidth,
  YY fill=blue!10,
  YY width=10mm,
  test/.style = {> = angle 90},
  YY/.style = {
    % changed in each use of shape
    name=n#1,
    % different in each picture
    fill=\yyfill,% color
    text width=\yywidth,
    % common in each picture
    shape=rectangle,
    draw,
    inner sep=1mm,
    minimum height=9mm,
    align=flush center,
    drop shadow
  },
  test 2/.style 2 args= {
    > = angle 90,
    XX/.style = {
      % changed in each use of shape
      name=n##1,
      % different in each picture
      fill=#1,% color
      text width=#2,
      % common in each picture
      shape=rectangle,
      draw,
      inner sep=1mm,
      minimum height=9mm,
      align=flush center,
      drop shadow
    },
  }
}
\begin{document}
  \begin{tikzpicture}
    [
      test,
      node distance = 12mm,
    ]
    \node[YY=1]                 {node A};
    \node[YY=2, right=of n1]    {node B};
    \draw[->] (n1) -- (n2);
  \end{tikzpicture}
  \begin{tikzpicture}
    [
      test,
      node distance = 12mm,
      YY fill=white,
      YY width=12mm,
    ]
    \node[YY=1]                 {node A};
    \node[YY=2, right=of n1]    {node B};
    \node (C) [right=of n2, node contents={node C}];
    \draw[->] (n1) -- (n2) -- (C);
  \end{tikzpicture}
  \begin{tikzpicture}
    [
      test,
      node distance = 12mm,
      YY fill=yellow!20,
      YY width=20mm,
    ]
    \node[YY=1]                 {node A};
    \node[YY=2, YY fill=red, right=of n1]    {node B};
    \draw[->] (n1) -- (n2);
  \end{tikzpicture}
  \begin{tikzpicture}
    [
      test 2={green}{25mm},
      node distance = 12mm,
    ]
    \node[XX=1]                 {node A};
    \node[XX=2, right=of n1]    {node B};
    \draw[->] (n1) -- (n2);
  \end{tikzpicture}
\end{document}

variations on a theme?

  • thank you very much for your effort. The third case is very close to what I like to achieve. I need to (re)think the problem and clarify my questions. – Zarko Jul 12 '15 at 2:54
  • @Zarko I've added another possibility which does create a variable style directly using nesting. But I'm not sure it is what you want.... Also an example of putting the name of the node first with node contents. – cfr Jul 12 '15 at 3:00
  • I see now, that solution as I imagine, is not possible. Your answer gives me a lot ideas, how to change a way of drawing my tikzpictures. I need a while to checking all consequences in my existing pictures. First I Itend to collect my solution as an answer, but then I recognize, that Your answer is enough. So, I accept it. – Zarko Jul 27 '15 at 21:13
  • @Zarko I'm glad it helped even if it was not exactly what you'd hoped for ;). – cfr Jul 27 '15 at 21:19

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.