I'm trying to play with tikz and chains, but I have some troubles when I deal with several chains. How could I position the chains each others ?
For example in the following example, I position the second chain by using yshift
on the first node, but it's not the best way to proceed I think…
And also I would like to know if it's possible to first chain without manually doing a dirty xshift
?
Thank you in advance.
\documentclass[a4paper, 11pt]{article}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}[
circ/.style={
draw,
circle,
fill=blue!30,
inner sep=5pt
},
squa/.style={
draw,
inner sep=5pt,
fill=blue!20,
},
start chain=1,
start chain=2,node distance=15mm
]
% Variables
\node[on chain=1,circ] (x1) {$x_1$};
\node[on chain=1,circ] (x2) {$x_2$};
\node[on chain=1,circ] (x3) {$x_3$};
% Clauses
\node[on chain=2,yshift=-2cm,squa] (c1) {$\lnot x_1 \lor \lnot x_2$};
\node[on chain=2,squa] (c2) {$x_2 \lor \lnot x_3$};
\node[on chain=2,squa] (c3) {$x_1 \lor x_2$};
\node[on chain=2,squa] (c4) {$\lnot x_2 \lor x_3$};
\node[on chain=2,squa] (c5) {$\lnot x_1 \lor x_3$};
% Draw
\draw[dashed] (x1.south) -- (c1.north);
\draw[dashed] (x1.south) -- (c5.north);
\draw[dashed] (x2.south) -- (c1.north);
\draw[dashed] (x2.south) -- (c4.north);
\draw[dashed] (x3.south) -- (c2.north);
\draw[] (x1.south) -- (c3.north);
\draw[] (x2.south) -- (c2.north);
\draw[] (x2.south) -- (c3.north);
\draw[] (x3.south) -- (c4.north);
\draw[] (x3.south) -- (c5.north);
\end{tikzpicture}
\caption{Une représentation d'une CNF sous forme de graph}
\label{fig:sat_graph}
\end{figure}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
Best Answer
Chains enable simple coding of chain of nodes. With help of package `positioning it is easy to control horizontal and vertical distance between node, For example:
in the case that you like to have the same start and end point of lines at nodes, than you need in above code explicitly defined this points with node anchor as you have in your question:
Note: this answer actually summarize Ignasi comments regard chain. I also take liberty and slightly change position of the second nodes chain.
Adendum: From your and Ingnasi answers I understood, that you like to have horizontally centered bot chains. This is easy to achieve with change the order of drawing: first had to be drawn chain below, and than positioning nodes containing Variables:
This change of drawing order and use chains facility only in the bottom row gives (perfectly) centered both rows of nodes: