5

I am following the second answer from this post and I want to add text to each piece of the puzzle, centered in the middle. What should be added? Here is the code:

\documentclass{standalone}

\usepackage{tikz}

\newcommand{\side}[1]{ (0.5,0.5) -- (0.0,#10.00) .. controls (0.0,#10.00) and (0.4,#1-0.04) .. (0.4,#10.04) .. controls (0.4,#10.11) and (0.2,#10.26) .. (0.5,#10.26) .. controls (0.8,#10.26) and (0.6,#10.11) .. (0.6,#10.04) .. controls (0.6,#1-0.04) and (1.0,#10.00) .. (1.0,#1*0.00) }

\newcommand{\piece}[5][white]{ \fill[#1] \side{#2} [rotate around={90:(0.5,0.5)}] \side{#3} [rotate around={180:(0.5,0.5)}] \side{#4} [rotate around={270:(0.5,0.5)}] \side{#5} -- cycle; }

\begin{document}

\begin{tikzpicture}

\begin{scope} \piece[red]{1}{1}{0}{0} \end{scope} \begin{scope}[xshift=1cm] \piece[blue]{1}{-1}{-1}{0} \end{scope} \begin{scope}[xshift=2cm] \piece[green]{1}{0}{1}{0} \end{scope}

\begin{scope}[yshift=-1cm] \piece[green]{1}{-1}{0}{-1} \end{scope} \begin{scope}[xshift=1cm,yshift=-1cm] \piece[red]{1}{-1}{1}{-1} \end{scope} \begin{scope}[xshift=2cm,yshift=-1cm] \piece[blue]{-1}{0}{1}{-1} \end{scope}

\begin{scope}[yshift=-2cm] \piece[blue]{0}{-1}{0}{-1} \end{scope} \begin{scope}[xshift=1cm,yshift=-2cm] \piece[green]{0}{-1}{1}{-1} \end{scope} \begin{scope}[xshift=2cm,yshift=-2cm] \piece[red]{0}{0}{1}{1} \end{scope}

\end{tikzpicture}

\end{document}

aripod
  • 107

2 Answers2

6

I added an extra argument (#6) to \piece, and this to the end of its definition" \node [white] at (.5,.5) {#6};

\documentclass{standalone}

\usepackage{tikz}

\newcommand{\side}[1]{ (0.5,0.5) -- (0.0,#10.00) .. controls (0.0,#10.00) and (0.4,#1-0.04) .. (0.4,#10.04) .. controls (0.4,#10.11) and (0.2,#10.26) .. (0.5,#10.26) .. controls (0.8,#10.26) and (0.6,#10.11) .. (0.6,#10.04) .. controls (0.6,#1-0.04) and (1.0,#10.00) .. (1.0,#1*0.00) }

\newcommand{\piece}[6][white]{ \fill[#1] \side{#2} [rotate around={90:(0.5,0.5)}] \side{#3} [rotate around={180:(0.5,0.5)}] \side{#4} [rotate around={270:(0.5,0.5)}] \side{#5} -- cycle; \node [white] at (.5,.5) {#6}; }

\begin{document}

\begin{tikzpicture}

\begin{scope} \piece[red]{1}{1}{0}{0}{A} \end{scope} \begin{scope}[xshift=1cm] \piece[blue]{1}{-1}{-1}{0}{B} \end{scope} \begin{scope}[xshift=2cm] \piece[green]{1}{0}{1}{0}{C} \end{scope}

\begin{scope}[yshift=-1cm] \piece[green]{1}{-1}{0}{-1}{D} \end{scope} \begin{scope}[xshift=1cm,yshift=-1cm] \piece[red]{1}{-1}{1}{-1}{E} \end{scope} \begin{scope}[xshift=2cm,yshift=-1cm] \piece[blue]{-1}{0}{1}{-1}{F} \end{scope}

\begin{scope}[yshift=-2cm] \piece[blue]{0}{-1}{0}{-1}{G} \end{scope} \begin{scope}[xshift=1cm,yshift=-2cm] \piece[green]{0}{-1}{1}{-1}{H} \end{scope} \begin{scope}[xshift=2cm,yshift=-2cm] \piece[red]{0}{0}{1}{1}{I} \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

If you wanted to be able to add more text, I would recommend invoking as \begin{tikzpicture}[scale=2] (or some other value). This will grow the size of the pieces without changing the size of the overlaid text. For example, with a scale of 2, it looks thus:

enter image description here

SUPPLEMENT

Based on an OP follow up: how to get an outline? And how to add more text?

Two things need to change. The \fill[#1] command is changed to \draw[#1,postaction={fill=gray!10}] and the interior \node's color may need re-evaluation, here as \node [black].

To increase the amount of text in the puzzle piece, I use a \Centerstack from the stackengine package. I also grew the scale= of the tikzpicture, which can always be shrunk down later, after the fact.

\documentclass{standalone}

\usepackage{tikz,stackengine} \setstackEOL{\}

\newcommand{\side}[1]{ (0.5,0.5) -- (0.0,#10.00) .. controls (0.0,#10.00) and (0.4,#1-0.04) .. (0.4,#10.04) .. controls (0.4,#10.11) and (0.2,#10.26) .. (0.5,#10.26) .. controls (0.8,#10.26) and (0.6,#10.11) .. (0.6,#10.04) .. controls (0.6,#1-0.04) and (1.0,#10.00) .. (1.0,#1*0.00) }

\newcommand{\piece}[6][white]{ \draw[#1,postaction={fill=gray!10}] \side{#2} [rotate around={90:(0.5,0.5)}] \side{#3} [rotate around={180:(0.5,0.5)}] \side{#4} [rotate around={270:(0.5,0.5)}] \side{#5} -- cycle; \node [black] at (.5,.5) {\Centerstack{#6}}; }

\begin{document}

\begin{tikzpicture}[scale=3.5]

\begin{scope} \piece[red]{1}{1}{0}{0}{my text\goes right\here} \end{scope} \begin{scope}[xshift=1cm] \piece[blue]{1}{-1}{-1}{0}{B} \end{scope} \begin{scope}[xshift=2cm] \piece[green]{1}{0}{1}{0}{C} \end{scope}

\begin{scope}[yshift=-1cm] \piece[green]{1}{-1}{0}{-1}{D} \end{scope} \begin{scope}[xshift=1cm,yshift=-1cm] \piece[red]{1}{-1}{1}{-1}{E} \end{scope} \begin{scope}[xshift=2cm,yshift=-1cm] \piece[blue]{-1}{0}{1}{-1}{F} \end{scope}

\begin{scope}[yshift=-2cm] \piece[blue]{0}{-1}{0}{-1}{G} \end{scope} \begin{scope}[xshift=1cm,yshift=-2cm] \piece[green]{0}{-1}{1}{-1}{H} \end{scope} \begin{scope}[xshift=2cm,yshift=-2cm] \piece[red]{0}{0}{1}{1}{I} \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

  • I forgot to ask......What about the outline of each piece? The code has \fill. However, by adding \draw didn't work like a traditional node... – aripod May 27 '21 at 10:11
  • 1
    @aripod Please see my SUPPLEMENT – Steven B. Segletes May 27 '21 at 10:22
  • That worked perfectly! I currently have font=\sffamily\tiny to make the text fit (I have more text, not letters). That's why I chose \tiny. I would like to have a larger font but that makes the text overlap with the edges. Is it possible to make the pieces larger? – aripod May 27 '21 at 13:30
  • @aripod As I indicated in my answer, the scale= parameter will grow the figure, which you can later scale down. Also, you could use, for example, a \Centerstack (from the stackengine package) to get multi-line text in the puzzle piece. – Steven B. Segletes May 27 '21 at 13:36
  • @aripod Please see my updated SUPPLEMENT – Steven B. Segletes May 27 '21 at 13:41
  • Ugh, yes! I left scale at 2 and missed it! Thanks for the great help! – aripod May 27 '21 at 13:55
1

You can draw jigsaw pieces including text like this:

\documentclass{standalone}

\usepackage{jigsaw}

\tikzset{ every node/.style={draw}, foo/.style={draw=none,gray,xshift=0.5cm,yshift=0.5cm} }

\begin{document}

\begin{tikzpicture} \path (0,1) pic [fill=red] {piece={1}{1}{0}{0}} node[foo] {A}; \path (1,1) pic [fill=green] {piece={1}{0}{0}{-1}} node[foo] {B}; \path (0,0) pic [fill=blue] {piece={0}{-1}{-1}{0}} node[foo] {C}; \path (1,0) pic [fill=yellow] {piece={0}{0}{-1}{1}} node[foo] {D}; \end{tikzpicture}

\end{document}

enter image description here