3

I'd like to align either one node A (see first center-environment in MWE) or multiple nodes A+AA (see second center-environment in MWE) to another set of nodes (B-F) so that they are centered to each other.

How can I do that?

Note:

  • Currently I still use the method via anchoring on the left side and adding a manual shift into x-direction which gets me some satisfactory result, but it does not look great.

  • In the first center environment, one could quickly decide to align the A and D but this is a somewhat bad example because very often the nodes are not of the same width.

Picture

enter image description here

MWE

\documentclass[
12pt,
a4paper
]{scrartcl}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    intersections,
    positioning
}

\tikzset{
    framedAAA/.style={
        rectangle,
        draw=black,
        very thick
    },
}

\listfiles

\begin{document}
    \begin{center}
        \begin{tikzpicture}[font=\sffamily]
        \node[framedAAA] (nodeA) {A};
        \node[framedAAA, below=of nodeA.west, anchor=west, xshift=-2cm] (nodeB) {b};
        \node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
        \node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
        \node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
        \node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
        \end{tikzpicture}
    \end{center}
    \begin{center}
    \begin{tikzpicture}[font=\sffamily]
    \node[framedAAA] (nodeA) {A};
    \node[framedAAA, right=of nodeA] (nodeA) {AA};
    \node[framedAAA, below=of nodeA.west, anchor=west, xshift=-2cm] (nodeB) {b};
    \node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
    \node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
    \node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
    \node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
    \end{tikzpicture}
\end{center}
\end{document}
  • Thought: One could use \draw ($(nodeB.west)!0.5!(nodeF.east)$) coordinate (middlenodeLOWER);\draw[red] (middlenodeLOWER.north) -- ([yshift=20pt]middlenodeLOWER.north);\draw[red] (nodeA.south) -- ([yshift=-4pt]nodeA.south); for the first center-environment. Then aligning them--literally--point by point would work. – henry Jun 30 '14 at 15:07
2

You must use matrix nodes, and try using global declarations as every node/.style=framedAAA to avoid repeat in every node. Add the matrix library with \usetikzlibrary{matrix}. This simplifies the code. Here is my code

\documentclass[12pt,a4paper]{scrartcl}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{calc,intersections,positioning,matrix}

\tikzset{
    framedAAA/.style={
        rectangle,
        draw=black,
        very thick,
    },
}

\listfiles

\begin{document}
\begin{center} 
  \begin{tikzpicture}[font=\sffamily]
    \matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeBF]
    {
    B & C & D & E & F\\
    };   
    \node[framedAAA,above=5mm of nodeBF] (nodeA) {A}; 
  \end{tikzpicture}
\end{center}
% Here my second alignment (both nodes of `matrix` type)   
\begin{center}
  \begin{tikzpicture}[font=\sffamily]
    \matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeBF]
    {
      B & C & D & E & F\\
    };
    \matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeAAA,above=5mm of nodeBF]
    {
      A & AA\\
    };    
  \end{tikzpicture}
\end{center}
\end{document}

and here the result

enter image description here

  • This is easier to understand imho. Damnit, I just changed all the bits to nodes earlier. Not going to put it all into a matrix again, but thank you nonetheless. – henry Jun 30 '14 at 17:15
1

One way to do this is to put the nodes B - F in a scope, and use the local bounding box of this scope.

The first example, with only one node A is really easy to solve this way:

\begin{tikzpicture}[font=\sffamily]
    % We put the nodes in a scope, with bounding box `scope1`
    \begin{scope}[local bounding box=scope1]
        \node[framedAAA] (nodeB) {b};
        \node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
        \node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
        \node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
        \node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
    \end{scope}
    % Simply put the node above `scope1`
    \node[framedAAA, above=of scope1.north] (nodeA) {A};
\end{tikzpicture}

When we want to align two sets of several nodes, we can use a ghost scope that will help compute the size of the second set of nodes A-AA, and then shift the real nodes appropriately:

\begin{tikzpicture}[font=\sffamily]
    \begin{scope}[local bounding box=scope1]
        \node[framedAAA] (nodeB) {b};
        \node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
        \node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
        \node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
        \node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
    \end{scope}

    % Ghost scope above the nodes B-F
    \begin{scope}[local bounding box=ghost, shift={($(scope1.north)+(0,1cm)$)}]
        \node[white] (ghostA) {A};
        \node[white, right=of ghostA] (ghostAA) {AA};
    \end{scope}

    % This scope is shifted to the left, so that it is centered
    \begin{scope}[local bounding box=scope2, shift={($2*(ghostA)-(ghost)$)}]
        \node[framedAAA] (nodeA) {A};
        \node[framedAAA, right=of nodeA] (nodeA) {AA};
    \end{scope}
\end{tikzpicture}

To explain the formula 2*(ghostA)-(ghost), it is equivalent to (ghostA) - ((ghost)-(ghostA)), which means that the node nodeA will be shifted from ghostA by the distance between ghostA and the center of the ghost scope. This will place the center of scope2 at ghostA, which was aligned with the center of scope1.

As you see, my solution when there are several nodes A-AA is quite complex. I would be interested in a simpler solution.

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.