0

I am using code in the following solution How can I write perfectly centered tex inside a cloud figure?:

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\begin{document}
\begin{tikzpicture}
   \node[text width=0.8cm,inner sep=0pt]
   [execute at begin node=\setlength{\baselineskip}{8pt},
       cloud,
       draw] (c) at (0,0)
   {\scriptsize{Lost\\Broker}};
\end{tikzpicture}
\end{document}

I want to draw additional aligned rectange inside the cloud. Is it possible to wrap text inside the cloud with a rectangle as below, filling inside with gray if possible?

enter image description here

alper
  • 1,389

3 Answers3

3

You can use a label like this

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{shapes.symbols}
\begin{document}
\begin{tikzpicture}
\node[cloud, draw, text width=0.6cm, label={[draw, text width=0.8cm, inner sep=2pt, font=\scriptsize]center:{Lost\\Broker}}] {};
\end{tikzpicture}
\end{document}

Text within rectangle within cloud shape

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{shapes.symbols}
\begin{document}
\begin{tikzpicture}
\node[cloud, fill=cyan, draw, text width=0.6cm, label={[draw, fill=pink, text width=0.8cm, inner sep=2pt, font=\scriptsize]center:{Lost\\Broker}}] {};
\end{tikzpicture}
\end{document}

Cyan and pink cloud

  • Is it possible to fill color inside of the rectangle? – alper Jul 07 '22 at 11:38
  • Yes - just add fill=... – hpekristiansen Jul 07 '22 at 12:19
  • I'd do \node[cloud, fill=cyan, draw, node font=\scriptsize, align=center, label={[draw, fill=pink, node font=\scriptsize, align=center]center:{Lost\\Broker}}] {\phantom{Lost}\\\phantom{Broker}}; so that there's no fiddling needed with text width because the cloud will be automatically big enough for the label to fit. – Qrrbrbirlbel Oct 16 '22 at 21:26
3

An other approach using tikz library fit: a cloud node fit to the size of a rectangle node

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols, fit}
\begin{document}
\begin{tikzpicture}
   \node[text width=0.8cm, inner sep=1pt, fill=gray!50]
        [node font=\setlength{\baselineskip}{8pt}\scriptsize, draw] 
      (c) at (0,0)
      {Lost\\ Broker};
    \node[draw, cloud, inner sep=-1pt, fit=(c)] {};
\end{tikzpicture}
\end{document}

enter image description here

Update: To color (fill) the cloud:

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds, shapes.symbols, fit}

\begin{document} \begin{tikzpicture} \node[text width=0.8cm, inner sep=1pt, fill=gray!50] [node font=\setlength{\baselineskip}{8pt}\scriptsize, draw] (c) at (0,0) {Lost\ Broker}; \scoped[on background layer] \node[draw, cloud, inner sep=-1pt, fit=(c), fill=cyan!30] {}; \end{tikzpicture} \end{document}

cloud with light blue background color

muzimuzhi Z
  • 26,474
1

You can use an \fcolorbox. This does not allow line breaks though, for that you can put the text inside a \parbox. You may need to play with the box width (9mm in the MWE) to get the correct line break.

Note also that \scriptsize is not a command that takes an argument, it is a switch that changes the font size until a new font size command is found or until the group ends. So the correct syntax is \scriptsize Some text and not \scriptsize{Some text}.

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\begin{document}
\begin{tikzpicture}
   \node[inner sep=0pt,cloud,draw] (c) at (0,0)
   {\fcolorbox{black}{gray}{\parbox{9mm}{\scriptsize Lost Broker}}};
\end{tikzpicture}
\end{document}

enter image description here

Marijn
  • 37,699