1

I would like to position nodes in groups a and b in the following way: enter image description here

Group b of tikz nodes is positioned in some symmetric way relative to group a.

The following script creates nodes corresponding to group a:

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \node(a1)[text width=0.75\linewidth, text height=2cm, draw] {
  };
  \node(a2)[text width=0.25\textwidth, text height=3cm, draw] at (a1.east)[anchor=west]{
    };
\end{tikzpicture}
\end{document}

How to group them and add group b?

Update

I am checking the answer from @Torbjorn

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{matrix, positioning}
\tikzset{
  mymat/.style={
    matrix of nodes,
    % style of matrix border                                                                                                                  
    draw=black!20,
    dashed,
    % distance between columns in matrix                                                                                                      
    column sep=-\pgflinewidth,
    nodes={ % set style for nodes inside matrix                                                                                               
      draw=red,
      solid
    }
  }
}
\begin{document}
\begin{tikzpicture}
  \matrix [mymat, name=m1] {
    % the |[..]| at the start of a cell lets you set the style for an individual cell                                                         
    |[minimum width=0.75\textwidth]| node a1
    & \includegraphics[width=0.25\textwidth]{example-image-a} \\ };
  \matrix [mymat, below=2mm of m1, name=m2]  {   |[minimum width=0.33\textwidth]| node b1 &    |[minimum width=0.33\textwidth]| node b2 &    \
|[minimum width=0.33\textwidth]| node b3 \\ };
\end{tikzpicture}
\end{document}

and get

enter image description here

But I would like node a1 centred with regard to node a2.

1

For groups of nodes like that, might make sense to use a \matrix.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\tikzset{
  mymat/.style={
    matrix of nodes,
    % style of matrix itself (outer  border, background)
    draw=black!50,
    fill=blue!10,
    dashed,
    % distance between columns in matrix
    column sep=-\pgflinewidth,
    nodes={ % set style for nodes inside matrix
       draw=red,
       solid,
       anchor=center 
   }
  } 
}
\begin{document}
\begin{tikzpicture}
  \matrix [mymat, name=m1] {
    |[minimum width=0.75\textwidth]| node a1
    & \includegraphics[width=0.25\textwidth]{example-image-a} \\ };

  \matrix [mymat,
           below=2mm of m1,
           nodes={minimum width=0.3\textwidth}, % set common width
           name=m2]  { 
   node b1 &    
  |[minimum height=3cm]| node b2 &   
  node b3 \\ };
\end{tikzpicture}

\end{document}
  • I think you did not understand the question. "node a1" and "node a2" are merely labels for nodes which will be filled with text and pictures. – Viesturs May 2 '18 at 10:59
  • @Viesturs I don't get it, can't you replace node a1 etc. with the text/images you want? – Torbjørn T. May 2 '18 at 11:00
  • @Viesturs The way I read your question my code does exactly what you ask for, so you need to help me understand what you want. – Torbjørn T. May 2 '18 at 11:11
  • In your example, how can I make a1.east correspond to a2.west, where a1 and a2 are cells in the matrix? – Viesturs May 2 '18 at 11:15
  • @Viesturs column sep=-\pgflinewidth. – Torbjørn T. May 2 '18 at 11:21
1
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\begin{document}
\begin{tikzpicture}
  \node(a1)[text width=0.6\linewidth, text height=2cm, draw] {
  };
  \node(a2)[text width=0.25\textwidth, text height=3cm, draw,
  right=of a1] {};
  \node(b3)[text width=0.25\textwidth, text height=3cm, draw,below=1.5cm of a2] {
  };
  \node(b2)[text width=0.25\textwidth, text height=3cm, draw,left=of b3] {
  };
  \node(b1)[text width=0.25\textwidth, text height=3cm, draw,left=of b2] {
  };
\begin{scope}[on background layer]
 \node[fit=(a1) (a2),inner sep=3mm,fill=gray!20] (f1){};
 \node[fit=(b1) (b2) (b3),inner sep=3mm,fill=gray!20] (f2){};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • It is not necessary to centre node b3 below a2. – Viesturs May 2 '18 at 11:06
  • @Viesturs Of course, you are the boss ;-) Please feel free to move it wherever you want it to be. – marmot May 2 '18 at 11:08
  • How can I do it? I modify your code and b3 is always centred below a2. – Viesturs May 2 '18 at 11:17
  • @Viesturs Just say ` \node(b3)[text width=0.25\textwidth, text height=3cm, draw] at (4,-3) { };` and then adjust the coordinates (4,-3). – marmot May 2 '18 at 11:19
  • I would replace (4, -3) with at (a2.south east)[anchor=north east] – Viesturs May 2 '18 at 11:24
1

with use of tikz libraries backgrounds, \fit and positioning is simple:

\documentclass[tikz, margin=1cm]{standalone}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance=0.05\linewidth, 
    box/.style args = {#1/#2}{
            draw=red, draw=red, minimum width=#1, minimum height=#2},
    box/.default = 0.3\linewidth/0.3\linewidth
                        ]
\node (a1) [box=0.75\linewidth/0.2\linewidth] {node a1};
\node (a2) [box=0.2\linewidth/0.3\linewidth,
            right=of a1] {node a2};
\scoped[on background layer] 
    \node[draw=gray!25, very thick, dotted, fill=gray!50, 
          fit=(a1) (a2)] {};
%
\node (b1) [box,below right=12mm and 0mm of a1.south west] {node a1};
\node (b2) [box,right=of b1] {node b2};
\node (b3) [box,right=of b2] {node b3};
\scoped[on background layer]
    \node[draw=gray!25, very thick, dotted, fill=gray!50,
          fit=(b1) (b3)] {};
   \end{tikzpicture}
\end{document}

enter image description here

  • Where is group b? – Viesturs May 2 '18 at 10:58
  • @Viesturs, added now ... – Zarko May 2 '18 at 11:23

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.