7

Description

This question follows from the question Why is the decoration marker off center? where we found what was causing the problem, but not why it was being caused in the first place.

It seems that when placing the object o2 above the pic o1 the above option is getting passed through to the decorations in the o2 object. This is the hypothesis because when we place the object o2_2 at a node that is above o1_2 then the decorations are drawn as expected.

Example Image

Comparison of the two methods

Semi-minimal code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,decorations.markings,patterns}

\pagenumbering{gobble} % remove page numbering to get good cropping when using pdfcrop

\def\pnode [#1]#2{
    % node for the potential function
    \node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
}
\def\snode [#1]#2#3{
    % node for the state variables
    \node[circle, minimum size=35.0pt , fill=lightgray,line width=0.625pt, draw=black,#1](#3){#2};
}
\def\osnode [#1]#2#3{
    % for for observed state variables
    \node[circle, minimum size=35.0pt , fill={rgb:red,1;green,2;blue,3},line width=0.625pt, draw=black,#1](#3){#2};
}
\tikzset{
    set midblock/.code={\pgfqkeys{/tikz/midblock}{#1}},
    set midblock={name/.initial=b1},
    midblock/.style={
        set midblock={#1},
        postaction={
            decorate,
            decoration={
                markings,
                mark=at position .5 with {\pnode[]{\pgfkeysvalueof{/tikz/midblock/name}}}}
        }
    }
}

\tikzset{
    pics/object1/.style 2 args={
       code={
        \begin{scope}
            \snode[]{obj-1}{-i};
        \end{scope}
        }
    }
}

\tikzset{
    pics/object2/.style 2 args={
       code={
        \begin{scope}[rotate=#2, transform shape]
            \snode[]{obj-2}{-i};
            \snode[right = of -i]{j-txt}{-j};
            \snode[above = of -i]{k-txt}{-k};
            \snode[left = of -i]{l-txt}{-l};
            \draw[midblock={name=foo}] (-i)--(-j);
            \draw[midblock={name=baz}] (-j)--(-k);
            \draw[midblock={name=bar}] (-k)--(-l);
            \draw[midblock={name=bar}] (-l)--(-i);
        \end{scope}
        }
    }
}

\begin{document}

\begin{tikzpicture}
    % Broken Code ...  but why?
    \path pic (o1) {object1};
    \pic [above= of o1-i] (o2) {object2={}{0}};
\end{tikzpicture}
\hspace {2cm}
\begin{tikzpicture}
    % This code works
    \path pic (o1_2) {object1};
    \snode[above=of o1_2-i]{}{n1};
    \pic (o2_2) at (n1) {object2={}{0}};
\end{tikzpicture}

\end{document}

******EDIT******

As requested a more minimal example.

Image

More minimal Example

The code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,decorations.markings,patterns}

\pagenumbering{gobble} % remove page numbering to get good cropping when using pdfcrop

\def\pnode [#1]#2{
    % node for the potential function
    \node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
}
\def\snode [#1]#2#3{
    % node for the state variables
    \node[circle, minimum size=35.0pt , fill=lightgray,line width=0.625pt, draw=black,#1](#3){#2};
}

\tikzset{
    set midblock/.code={\pgfqkeys{/tikz/midblock}{#1}},
    set midblock={name/.initial=b1},
    midblock/.style={
        set midblock={#1},
        postaction={
            decorate,
            decoration={
                markings,
                mark=at position .5 with {\pnode[]{\pgfkeysvalueof{/tikz/midblock/name}}}}
        }
    }
}

\tikzset{
    pics/object1/.style 2 args={
       code={
        \begin{scope}
            \snode[]{obj-1}{-i};
        \end{scope}
        }
    }
}

\tikzset{
    pics/object2/.style 2 args={
       code={
        \begin{scope}[rotate=#2, transform shape]
            \snode[]{obj-2}{-i};
            \snode[right = of -i]{j-txt}{-j};
            \draw[midblock={name=foo}] (-i)--(-j);
        \end{scope}
        }
    }
}

\begin{document}

\begin{tikzpicture}
    % Broken Code ...  but why?
    \path pic (o1) {object1};
    \pic [above= of o1-i] (o2) {object2={}{0}};
\end{tikzpicture}
\hspace {2cm}
\begin{tikzpicture}
    % This code works
    \path pic (o1_2) {object1};
    \snode[above=of o1_2-i]{}{n1};
    \pic (o2_2) at (n1) {object2={}{0}};
\end{tikzpicture}

\end{document}
2
  • 1
    The current implementation of pics is demonstrably buggy. For example, some code from the manual simply doesn't work in the current version of PGF/TikZ. (It used to work.) I don't know if this is a bug or expected behaviour, but don't be surprised if it is the former.
    – cfr
    May 5, 2016 at 20:38
  • Can you really not demonstrate the problem with a more minimal example? If you really want help with diagnosis, it is going to have to be reduced to a more manageable tangle.
    – cfr
    May 5, 2016 at 20:40

1 Answer 1

1

\pic[above= of o1-i]...
\path pic[above= of o1-i]...
node[above= of o1-i]...
\tikzset{above= of o1-i}
\def\tikz@anchor{south}

Keeping this in mind, your problem can be solved by assigning explicit anchor to \pnode. That is, you just need to replace

\pnode[]

by

\pnode[anchor=center]

and everything works as expected.

You must log in to answer this question.

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