4

How can the balls b1 and b2 be aligned exactly above each other? Also, above=1cm of b1.center, anchor=center and all kinds of combinations with north - south, north east - south west does not yield the desired result.

\documentclass[tikz, margin=1pt]{standalone}
\usetikzlibrary{positioning}
\tikzset{ball/.style={draw, circle, minimum size=.1cm}}

\begin{document}
\begin{tikzpicture}

\begin{scope}[]
    \matrix[nodes={draw, ball}]{
        \node (b1) {b1}; &
        \node (b2) [above=.1cm of b1] {b2}; \\
    };
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • (1) welcome, (2) you might want to explain in more detail what your end goals is here. To me it does not seem the \matrix is the right tool to use here. – daleif Apr 25 at 10:26
5

Your code with the matrix is not correct because your nodes are on same row, they are separated by & and there is only one \\ in your matrix.

If you want to use a matrix, I propose following code which loads matrix library and uses a matrix of nodes to simplify syntax.

But I think second solution is better, just use above=0pt of ... and you'll get two nodes one above the other.

\documentclass[tikz, margin=1pt]{standalone}
\usetikzlibrary{positioning, matrix}
\tikzset{ball/.style={draw, circle, minimum size=.1cm}}

\begin{document}
\begin{tikzpicture}

    \matrix[matrix of nodes, nodes={draw, ball}, row sep=0pt]{
        b2\\
        b1\\
    };

\end{tikzpicture}

\begin{tikzpicture}
\node[ball] (b1) {b1};
\node[ball, above=0pt of b1] (b2) {b2};
\end{tikzpicture}
\end{document}

enter image description here

  • I want to finally make a grid of balls that is why I used matrix in the first place. I just reduced it to the essential part for the MWE. – AKG Apr 25 at 11:30

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.