I need to draw a nonuniform text bubble and fitting the text inside of it.
Asked
Active
Viewed 620 times
1
Mensch
- 65,388
Ahmed Hawary
- 427
1 Answers
4
Here's a bubble:
\documentclass{standalone}
\usepackage{tikz
\usetikzlibrary{calc,fit,intersections}
\begin{document}
\begin{tikzpicture}
\draw plot [smooth,tension=1.3] coordinates {(1.cm+rand*.1cm,.2cm+rand*.1cm) (0cm+rand*0cm,-1cm+rand*.1cm) (-1cm+rand*0.1cm,0cm+rand*0.1cm) (0cm+rand*0.1cm,1cm+rand*0.1cm) (1.cm+rand*.1cm,-.2cm+rand*.1cm)};
\end{tikzpicture}
\end{document}
You can always hard-code the coordinates instead of the random ones I've used.
Putting text inside it is quite simple
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit,intersections}
\begin{document}
\begin{tikzpicture}
\draw plot [smooth,tension=1.3] coordinates {(1.cm+rand*.1cm,.2cm+rand*.1cm) (0cm+rand*0cm,-1cm+rand*.1cm) (-1cm+rand*0.1cm,0cm+rand*0.1cm) (0cm+rand*0.1cm,1cm+rand*0.1cm) (1.cm+rand*.1cm,-.2cm+rand*.1cm)};
\node[text width=1.4cm] at(0,0) {\small{This is some text in a bubble}};
\end{tikzpicture}
\end{document}
Here it's a bit nicer to use:
\documentclass{standalone}
\usepackage{shapepar}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc,fit,intersections}
\newcommand\bubblenodetext[3]{\node [draw=none, shape=circle, text width=0cm, inner sep=5mm] at (#1,#2) (bubblenode){\shapepar{\circleshape} #3\par};
\def\startendoffset{3mm}
\draw plot [smooth,tension=1.3] coordinates {($(bubblenode.east)+(\startendoffset,\startendoffset)$) (bubblenode.south) (bubblenode.west) (bubblenode.north) ($(bubblenode.east)+(\startendoffset,-\startendoffset)$)};
}
%change draw=none to draw=red to see the line bounding the text.
\newcommand\randombubblenodetext[3]{\node [draw=none, shape=circle, text width=0cm, inner sep=2mm] at (#1,#2) (bubblenode){\shapepar{\circleshape} #3\par};
\def\startendoffset{3mm}
\def\randomfactor{3mm}
\draw plot [smooth,tension=1.3] coordinates {($(bubblenode.east)+(\startendoffset+rnd*\randomfactor,\startendoffset+rand*\randomfactor)$)
($(bubblenode.south)+(rand*\randomfactor,-rnd*\randomfactor)$)
($(bubblenode.west)+(-rnd*\randomfactor,rand*\randomfactor)$)
($(bubblenode.north)+(rand*\randomfactor,rnd*\randomfactor)$)
($(bubblenode.east)+(\startendoffset+rnd*\randomfactor,-\startendoffset+rand*\randomfactor)$)};
}
\begin{document}
\begin{tikzpicture}
\bubblenodetext {0}{0}{Now we automate the bubble. We can still fill it with text.}
\randombubblenodetext{5}{0}{This is a slightly random bubble. We can still fill it with text.}
\end{tikzpicture}
\end{document}
In the right-hand example I've also reduce the inner sep -- you need to strike a balance here as the text doesn't really respond to the shape I've drawn, but to a circle (see comment on line 10). The code was shamelessly stolen from Count Zero's answer to Fitting text to a shape in TikZ.
Things to tweak to get the look you're going for: inner sep tension, \startendoffset, \randomfactor (which you may want to set to 0).
-
You'd probably like a little more automation that that if you're going to use it a lot. I've got an idea – Chris H Jan 10 '17 at 12:51
-
It might be nice to try to combine with some of the suggestions at Simulating hand-drawn lines – Chris H Jan 10 '17 at 14:24
-




tikzcould help you – flav Jan 10 '17 at 09:58