It seems like I'm doing something wrong when using foreach
variables to indicate node numbers. The two foreach loops are anchoring the paths to the east of each node. Why does this happen?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit,patterns,decorations.pathreplacing,calc}
\makeatletter
\tikzset{
labelnode/.style={draw=none, fill=none, black},
latentnode/.style={draw, minimum width=5mm, shape=circle, ultra thick, black},
dagconn/.style={arrows=->, black, thick},
}
\makeatother
\begin{document}
\begin{tikzpicture}[scale=1]
\pgfmathsetmacro{\colgap}{1.6}
\pgfmathsetmacro{\rowgap}{1.3}
\node[labelnode] (x1) at (\colgap*0,\rowgap*1) {$X_1$};
\node[labelnode] (x2) at (\colgap*1,\rowgap*1) {$X_2$};
\node[labelnode] (x3) at (\colgap*2,\rowgap*1) {$X_3$};
\node[latentnode,blue] (x4) at (\colgap*0,\rowgap*0) {$X_4$};
\node[latentnode,red] (x5) at (\colgap*1,\rowgap*0) {$X_5$};
\node[labelnode] (x6) at (\colgap*2,\rowgap*0) {$X_6$};
\foreach \c in {1,...,2} {
\foreach \r in {1,...,2} {
\pgfmathsetmacro{\st}{(((\r-1)*3)+\c)}
\pgfmathsetmacro{\en}{\st+1}
\draw[dagconn] ([xshift=0.2cm]x\st) to ([xshift=-0.9cm]x\en);
}
}
\foreach \c in {1,...,3} {
\foreach \r in {1,...,1} {
\pgfmathsetmacro{\st}{((\r-1)*3)+\c}
\pgfmathsetmacro{\en}{\st+3}
\draw[dagconn] (x\st) to (x\en);
}
}
\end{tikzpicture}
\end{document}
\pgfmathtruncatemacro
\pgfmathsetmacro
to\pgfmathtruncatemacro
fixes the problem. Thanks @percusse.