13

I notice that we can put text in a circle by

\node[draw,circle]{Text};

Is it possible to put text in any shapes with automatic line breaking? (e.g., in an ellipse, or in a closed curve that we draw)

EDIT

For the demand of "putting text in any closed curve we draw" is probably a duplicate of Fitting text to a shape in TikZ (\shapeparnode is used), though I'm looking forward to a shape option of \node that uses a closed curve we draw.

Hongying
  • 569

1 Answers1

15

Sure. SImply use text width with/without align:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\begin{tikzpicture}
\node[draw,fill=cyan!30,ellipse,text width=2cm] 
  {Some text goes here inside this ellipse};
\node[xshift=4cm,draw,fill=green!25,ellipse,text width=2cm,align=center] 
  {Some text goes here inside this ellipse};
\node[xshift=8cm,draw,fill=yellow!25,ellipse,text width=2cm,align=right] 
  {Some text goes here inside this ellipse};
\node[xshift=2cm,yshift=-3cm,draw,fill=orange!25,trapezium,text width=2cm,align=center] 
  {Some text goes here inside this trapezium};
\node[yshift=-3cm,xshift=6cm,draw,fill=magenta!25,regular polygon, regular polygon sides=6,text width=2cm,align=center] 
  {Some text goes here inside this hexagon};
\end{tikzpicture}

\end{document}

enter image description here

Regarding the part about putting text in any closed curve we draw, there might be a number of possible approaches here:

  • The \shapeparnode command defined by Paul Gaborit in his answer to Fitting text to a shape in TikZ.

  • Defining a new shape as described in Section 75.5 Declaring New Shapes of the PGF manual (this is a non trivial process, which might be simplified by the using the package in the following item).

  • Using the makeshape package to simplify the creation of new shapes. This is the abstract from the package documentation:

The makeshape package simplifies writing PGF shapes. Declaring a custom shape with a correct anchor border can be difficult. Complex shapes often need complicated calculations to find the touching point of a connecting line. This package only requires that a developer write a PGF path describing the anchor border. It also provides macros that help with the management of shape parameters and the definition of anchor points.

Gonzalo Medina
  • 505,128
  • 1
    Thanks for your answer! What if we can to put some text in a closed curve that we draw rather than predefined shapes, Is it possible to define a "text region" by drawing a closed curve, and give it a name as myTextRegion? Finally we use it as \node[draw,myTextRegion, text width=2cm]{some text}; ? (or maybe without option text width, just let it fit myTextRegion automatically) – Hongying Apr 20 '13 at 06:22
  • @Hongying I've added to my answer some possible approaches to deal with the "arbitrary closed curve". – Gonzalo Medina Apr 20 '13 at 17:38