7

Description

I am making a graph and am using a decoration marker to make square nodes between the circular ones. That had been working fine until I wanted to add a decoration between T_t and I_t (near the top). The square is off-center and I am not sure why, it uses the same decoration code as all of the others.

So the question is why? And how do I fix it? The answer to the first will be a large step to resolving the second I'm sure.

Example Image

enter image description here

Code to Produce the Image

\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}[rotate=#2, transform shape]
        \osnode[]{$Y_{r_{#1}}$}{-Yr}
        \pnode[above right= of -Yr]{-f1}
        \snode[below right = of -f1]{$X_{r_{#1}}$}{-Xr}
        \snode[above = of -f1]{$X_{c_{#1}}$}{-Xc}
        \snode[above = 20mm of -Xc]{$C_{{#1}}$}{-C}
        \draw[-] (-Yr)--(-f1);
        \draw[-] (-Xr)--(-f1);
        \draw[-] (-Xc)--(-f1);
        \draw[midblock={name=-XctoC}] (-Xc)--(-C);
    \end{scope}
    }
}
}

\tikzset{
pics/object2/.style 2 args={
   code={
    \begin{scope}[rotate=#2, transform shape]
        \pnode[]{-f2}
        \snode[above left=of -f2]{$T_{#1}$}{-Tt}

        \snode[above right = of -f2]{$I_{#1}$}{-It}

        \draw[-] (-Tt)--(-f2);
        \draw[-] (-It)--(-f2);
        \draw[midblock={name=testing}] (-Tt)--(-It);
    \end{scope}
    }
}
}

\begin{document}

\begin{tikzpicture}[remember picture]
\path pic (t1) {object1={t}{0}};
\pic [above= of t1-C] (c1) {object2={t}{0}};
\draw[-] (t1-C)--(c1-f2);
\end{tikzpicture}
\hspace{2 cm}
\begin{tikzpicture}[remember picture]
\path pic (t2) {object1={t+1}{0}};
\pic [above= of t2-C] (c2) {object2={t+1}{0}};
\draw[-] (t2-C)--(c2-f2);
\end{tikzpicture}

\begin{tikzpicture}[remember picture,overlay,every text node part/.style={align=center}] % text node stuff for multiline text
%%% Draw some edges
\draw[midblock={name=XtoX}] (t1-Xr) to [out=310,in=230] (t2-Xr);
\draw[-] (c1-It) to [out=345,in=135] (XtoX);
\draw[midblock={name=TtoT}] (c1-Tt) to [out=50,in=130] (c2-Tt);
\draw[midblock={name=ItoI}] (c1-It) to [out=50,in=130] (c2-It);

\end{tikzpicture}

\end{document}
3
  • Sorry, but what is off-centre? The square between T_t and I_t seems to be centred between them (as far as I can tell).
    – cfr
    May 4, 2016 at 22:32
  • 1
    @cfr vertically centred on the line
    – Thruston
    May 4, 2016 at 22:32
  • Oh, vertically. OK.
    – cfr
    May 4, 2016 at 22:33

1 Answer 1

5

Edited answer:

Upon closer inspection I found this only affected object2, which is nearly identical in definition to object1. The only difference is that when object1 is used in the actual tikzpicture, no options are passed to the \pic command. The \pic command for object2, on the other hand, has options [above= of t1-C]. It seems this above is being passed through to object2, making all the midblocks appear above the paths upon which they are drawn (paths shown in red).

In the graph below, I have corrected the left side by first naming a node, and then placing the pic containing object2 at that node. The right side uses the original \pic [above= of ...] to show the difference. I have also added more midblocks to illustrate their positioning.

enter image description here

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

\def\pnode [#1]#2{
% node for the potential function
\node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
%\node[circle, minimum size=5.0pt,fill=red,#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}[rotate=#2, transform shape]
        \osnode[]{$Y_{r_{#1}}$}{-Yr}
        \pnode[above right= of -Yr]{-f1}
        \snode[below right = of -f1]{$X_{r_{#1}}$}{-Xr}
        \snode[above = of -f1]{$X_{c_{#1}}$}{-Xc}
        \snode[above = 20mm of -Xc]{$C_{{#1}}$}{-C}
        \draw[-] (-Yr)--(-f1);
        \draw[-] (-Xr)--(-f1);
        \draw[-] (-Xc)--(-f1);
        \draw[midblock={name=-XctoC}] (-Xc)--(-C);
    \end{scope}
    }
}
}

\tikzset{
pics/object2/.style 2 args={
   code={
    \begin{scope}[rotate=#2, transform shape]
        \pnode[]{-f2}
        \snode[above left=of -f2]{$T_{#1}$}{-Tt}
        \snode[above right=of -f2]{$I_{#1}$}{-It}
        \draw[-,red,midblock={name=foo}] (-Tt)--(-f2);
        \draw[-,red,midblock={name=bar}] (-It)--(-f2);
        \draw[-,red,midblock={name=testing}] (-Tt)--(-It);
    \end{scope}
    }
}
}

\begin{document}

\begin{tikzpicture}[remember picture]
\path pic (t1) {object1={t}{0}};
%\pic [above= of t1-C] (c1) {object2={t}{0}};
\node[above=of t1-C] (n1){};
\pic(c1) at (n1) {object2={t}{0}};
\draw[-] (t1-C)--(c1-f2);
\end{tikzpicture}
\hspace{2 cm}
\begin{tikzpicture}[remember picture]
\path pic (t2) {object1={t+1}{0}};
\pic [above= of t2-C] (c2) {object2={t+1}{0}};
\draw[-] (t2-C)--(c2-f2);
\end{tikzpicture}

\begin{tikzpicture}[remember picture,overlay,every text node part/.style={align=center}] % text node stuff for multiline text
%%% Draw some edges
\draw[midblock={name=XtoX}] (t1-Xr) to [out=310,in=230] (t2-Xr);
\draw[-] (c1-It) to [out=345,in=135] (XtoX);
\draw[midblock={name=TtoT}] (c1-Tt) to [out=50,in=130] (c2-Tt);
\draw[midblock={name=ItoI}] (c1-It) to [out=50,in=130] (c2-It);

\end{tikzpicture}

\end{document}
7
  • 1
    Sorry about the non-minimal example...Could you explain why this only failed in a specific case, and not for all markers?
    – Brett
    May 5, 2016 at 2:16
  • 1
    @bizzy At first I didn't realize all the markers all had the same definition, so your example code turned out to be perfect. Sorry about that. Please see my updated answer. It seems to answer your "why" question, but raises yet another.
    – erik
    May 5, 2016 at 16:20
  • You mention that it raises another question, but that other question isn't quite clear. Is the question: Why does passing above to object2 pass through to the midblock object?
    – Brett
    May 5, 2016 at 16:54
  • 1
    @bizzy Yes, or, "Why does the pic option get passed through to object2 in the first place?" That's probably a good candidate for a separate question though.
    – erik
    May 5, 2016 at 17:09
  • Yeah, I guess I'll put that question in for the advancement of knowledge. I am new to this, so I should probably reference this question and provide a more minimal example right?
    – Brett
    May 5, 2016 at 17:24

You must log in to answer this question.

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