2

Assume four nodes, arranged in two rows, with text inside each of them. Further assume that I would like to arrange these nodes in the following pattern:

|----Node A----|  |Node B|
|Node C||Node D|

I aligned node C via anchor=north east (kind of counter-intuitive, but well ...), yet the combination of nodes C and D is slightly longer than node A on each side.

How can I correct for this?

\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta}
\usetikzlibrary{calc, arrows, fit, positioning, shapes.symbols, chains}
\usetikzlibrary{decorations.markings, decorations.pathmorphing, decorations.pathreplacing}
\pagenumbering{gobble} % Remove page number

\begin{document}
% Define block styles
\tikzstyle{block_large} = [rectangle, draw, text width=8cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\tikzstyle{block_medium} = [rectangle, draw, text width=4cm, rounded corners, text height=0.4cm, text depth=1.25cm]

    \begin{tikzpicture}[node distance=1.5cm, auto, comment/.style={rectangle, inner sep=5pt}]
        % Draw nodes - first row
        \node [block_large] (col1_row1) {
            \textbf{Node A}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };

        \node [block_medium, right=1cm of col1_row1] (col3_row1) {
            \textbf{Node B}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };

        % Draw nodes - second row
        \node [block_medium, below=0.1cm of col1_row1, anchor=north east] (col1_row2) {
            \textbf{Node C}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };
        \node [block_medium, right=0cm of col1_row2] (col2_row2) {
            \textbf{Node D}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };
\end{tikzpicture}

\end{document}

Output:

enter image description here

3

Actually since you're defining the text width of your box, you need to take care, that the inner sep adds to the size of the box. Therefore by defining the inner xsep and tweaking the size of your boxes results in:

enter image description here

code:

\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta}
\usetikzlibrary{calc, arrows, fit, positioning, shapes.symbols, chains}
\usetikzlibrary{decorations.markings, decorations.pathmorphing, decorations.pathreplacing}
\pagenumbering{gobble} % Remove page number

\begin{document}
% Define block styles
\tikzstyle{block_large} = [rectangle, draw, text width=8cm, inner xsep=0.25cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\tikzstyle{block_medium} = [rectangle, draw, text width=3.75cm, inner xsep=0.25cm, rounded corners, text height=0.4cm, text depth=1.25cm]

    \begin{tikzpicture}[node distance=1.5cm, auto, comment/.style={rectangle, inner sep=5pt}]
        % Draw nodes - first row
        \node [block_large] (col1_row1) {
            \textbf{Node A}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };

        \node [block_medium, right=1cm of col1_row1] (col3_row1) {
            \textbf{Node B}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };

        % Draw nodes - second row
        \node [block_medium, below=0.1cm of col1_row1, anchor=north east] (col1_row2) {
            \textbf{Node C}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };
        \node [block_medium, right=0cm of col1_row2] (col2_row2) {
            \textbf{Node D}\\
            \textit{Foo:} Bar\\
            \textit{Baz:} Qux
        };
\end{tikzpicture}

\end{document}
0
1

For this kind of tikzpicture may be you could consider tcolorbox and its raster library which allows to more or less easily organize text boxes.

In this particular case, a result similar to what you want could be done with

\documentclass{article}
\usepackage[most]{tcolorbox}

\newcommand{\mytext}[1]{\textbf{Node #1}\\\textit{Foo:} Bar\\\textit{Baz:} Qux}

\begin{document}
\begin{tcbitemize}[raster equal height=rows, raster columns=3, notitle, colback=white]
\tcbitem[raster multicolumn=2] \mytext{A}
\tcbitem \mytext{B}
\tcbitem \mytext{C}
\tcbitem \mytext{D}
\end{tcbitemize}
\end{document}

enter image description here

0

Just add inner sep=0pt (using new style):

\tikzset{
    block/.style = {
        rectangle, draw, rounded corners, text depth=1.25cm, 
       text height=0.4cm, inner sep=0pt,
   },
    block_large/.style = {block, 
       text width=8cm,
   },
    block_medium/.style = {block, 
       text width=4cm
   },
}

to the node specifications --- inner sep is added to other sizes, so you get one in the big box, and two in the smaller ones.

Output:

enter image description here

3
  • 1
    \tikzstyle is deprecated, use \tikzset instead.
    – Alenanno
    May 6 '16 at 11:20
  • @Rmano Thank you! For a few minutes, you had a version posted where the option inner sep was set such that the boxes were nicely aligned. That would be my preferred answer.
    – Michael G
    May 6 '16 at 11:25
  • @MichaelGruenstaeudl I deleted it because it didn't work, inner sep was destroying vertical alignement...
    – Rmano
    May 6 '16 at 11:26

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.