2

I have a question regarding using "tikzpicture". I intend to draw a figure, which includes a top rectangular, middle circle and bottom rec. I tried and drew what I wanted with considering coordination for each shape. But I would like to consider the position of each shape depending on the position of the upper shape. I coded what I want but there is an intersection between my shaps.

Here is my code:

\documentclass{article}
 \usepackage{tikz}
 \usepackage{pgfplots}
\begin{document}
\tikzstyle{cir}=[circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=3cm]
\tikzstyle{rec}=[rectangle,draw=black!50,fill=black!20,thick,
    inner sep=0pt,minimum size=3cm]
\begin{figure}
   \centering
   \begin{tikzpicture}
     \node[rec] (top_rec)                            {};
     \node[cir] (middle_cir) [below of=top_rec]      {};
     \node[rec] (down_rec)   [below of=middle_cir]   {};
     \end{tikzpicture}   
  \caption{Caption}
  \label{fig:my_label}
\end{figure}
\end{document}

Here also is what I got enter image description here

Torbjørn T.
  • 206,688

2 Answers2

5

Is this what you want?

enter image description here

Observe that I changed the anchor in the node style and changed their positions making use of at and passing the anchor explicitly.

\documentclass{article}
 \usepackage{tikz}
 \usepackage{pgfplots}
\begin{document}
\tikzstyle{cir}=[circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=3cm,anchor=north] % added anchor
\tikzstyle{rec}=[rectangle,draw=black!50,fill=black!20,thick,
    inner sep=0pt,minimum size=3cm,anchor=north] % added anchor
\begin{figure}
   \centering
   \begin{tikzpicture}[]
     \node[rec] (top_rec)                            {A};
     \node[cir] (middle_cir) at (top_rec.south)      {B}; % changed
     \node[rec] (down_rec)   at (middle_cir.south)   {C}; % changed
   \end{tikzpicture}
   \caption{Caption}
   \label{fig:my_label}
\end{figure}
\end{document}
Sigur
  • 37,330
5

With use of the chains and positioning library, and defining nodes style as options of `tikzpicture:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\begin{document} \begin{figure} \centering \begin{tikzpicture}[ node distance = 0pt, start chain = going below, C/.style = {circle, draw=blue!50,thick, fill=blue!20, inner sep=0pt, minimum size=3cm, on chain}, R/.style = {draw=black!50, thick, fill=black!20, inner sep=0pt, minimum size=3cm, on chain} ] \node[R] (n1) {}; \node[C] (n2) {}; \node[R] (n3) {}; \end{tikzpicture} \caption{Caption} \label{fig:my_label} \end{figure} \end{document}

Result of compilation is:

enter image description here

Addendum: Regarding your comment with your new request:

  • Please don't add some code to my answer which change it. If you have new problem, please ask new question, if you like to clarify your problem, edit your question and describe it there, that it can be seen to other members of site.
  • Arrows with labels between rectangles in your images you can draw as edges with quotes. See MWE below.
  • In MWE below is changed nodes style declaration too. I do this to show one more possibilities how to define it and that they support your new request in comment.
  • Style and text of quotes you can change according to your needs.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                quotes}

\begin{document} \begin{figure} \centering \begin{tikzpicture}[ node distance = 0pt, start chain = going below, base/.style = {draw=#1!50, thick, fill=#1!20, inner sep=0pt, outer sep=0pt, minimum size=3cm, on chain}, % <--- new C/.style = {circle, base=blue}, % <--- changed R/.style = {base=black}, % <--- changed every edge/.style = {draw, -Straight Barb, semithick, bend angle=45}, % <--- for edges every edge quotes/.style = {auto=right, font=\small\bfseries} % <--- for quotes on edges ] \node[R] (n1) {}; \node[C] (n2) {}; \node[R] (n3) {}; % edges \draw (n1.west) edge [bend right, "A"] (n3.west) (n3.east) edge [bend right, "B"] (n1.east); \end{tikzpicture} \caption{Caption} \label{fig:my_label} \end{figure} \end{document}

enter image description here

Zarko
  • 296,517
  • Thanks for it. Just wondering how I can label the edges? If there are two edges one from top rectangular to bottom rectangular and one from bottom rectangular to top rectangular? – Ali Sohrabi Feb 01 '22 at 02:02
  • @AliAkbarSohrabiYousefkhan, I do not understand your commend. If you have new request, please edit your question or better ask new question. Both answers solve what you ask. – Zarko Feb 01 '22 at 07:28
  • Thanks now it solves my problem. Sure I just wanted to add new comments but I had a limited number of characters, then have to edit your code, sorry about it. – Ali Sohrabi Feb 01 '22 at 18:38
  • @AliAkbarSohrabiYousefkhan, since your problem is solved, seems that now is time to upvote and accept my answer :-). – Zarko Feb 01 '22 at 19:11
  • sure. Thanks again :) – Ali Sohrabi Feb 02 '22 at 00:09