[Tex/LaTex] Adding text to a rectangle

tikz-pgf

Wants to add text to a following rectangle. Also wants draw an another rectangle on the right side of the following rectangle sharing the boundary.

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{tcolorbox}


\usetikzlibrary{positioning,shapes,arrows,shadows,patterns,calc}


\hyphenation{op-tical net-works semi-conduc-tor}

\begin{document}
\begin{figure}
    %\centering
%   \captionsetup{justification = centering}
    \begin{tikzpicture}[x=0.5mm,y=0.5mm]
        \coordinate (a1);
        \coordinate[right=150 of a1](a2);
        \coordinate[below=90 of a1](a3);
        \coordinate[right=150 of a3](a4);   
        \draw[ultra thick,rounded corners=10,green] ($(a4)-(80,20)$) 
rectangle +(80*2,20*2);%Rectangle rounded       
    \end{tikzpicture}
\end{figure}

Best Answer

Adaptations:

  • Remove unused packages, unused tikzlibraries and add needed package (tikz).
  • (Old: Add node{\Large text} right after rectangle to add text centered or just use nodes as follows ...)
  • Use node to draw the rectangular nodes with text, so you don't need to calculate any coordinates.
  • You can set minimum width=160, minimum height=40 to the desired size.
  • To let node B share the right boundary of A you can set option right=0 of a.

Code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{figure}
        \begin{tikzpicture}[
            myrectangle/.style={rectangle, draw, minimum width=160, minimum height=40, ultra thick, rounded corners=10, green}
        ]

            \node[myrectangle] (a) at (0,0) {\Large text};
            \node[myrectangle, right=0 of a] (b) {\Large B};
        \end{tikzpicture}
    \end{figure}
\end{document}

Result:

enter image description here