3

I want to arrange the tikz ducks inside a node in several lines:

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

\begin{document}
\begin{tikzpicture}[
    BOX/.style = {draw=orange,rectangle,fill=orange!20,minimum height=5cm, minimum width=4cm, inner sep=0,rounded corners=5pt,dashed,line width=1pt}
]
    \def\DUCKFIVE{% TO PUT DUCKS IN NODES
        \begin{tikz}
            \tikz\shuffleducks\duck[\randomhead,scale=0.3];
            \tikz\shuffleducks\duck[\randomhead,scale=0.3,xshift=1cm];
            \tikz\shuffleducks\duck[\randomhead,scale=0.3,xshift=2cm];
            \tikz\shuffleducks\duck[\randomhead,scale=0.3,xshift=-1cm];
            \tikz\shuffleducks\duck[\randomhead,scale=0.3,xshift=-2cm];
        \end{tikz}
    }
    \node[BOX=orange!30](TREE){
        \DUCKFIVE
        \\
        \DUCKFIVE
        \newline
        \DUCKFIVE
        \linebreak
        \DUCKFIVE
    };
\end{tikzpicture}
\end{document}

I tried to put \\, \newline, \linebreak between the ducks, but they didn't work: enter image description here How can I draw 4 × 5 ducks in a node?

1

1 Answer 1

5

Nesting tikzpictures is usually not a good idea. I suggest to use a tikz matrix instead:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{ducks,backgrounds}
\begin{document}
\begin{tikzpicture}
\matrix {
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\      
};
\begin{scope}[on background layer]
  \path[draw=orange,rectangle,fill=orange!20,minimum height=5cm, minimum width=4cm, inner sep=0,rounded corners=5pt,dashed,line width=1pt] (current bounding box.south west) rectangle (current bounding box.north east);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here


To include a title:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{ducks,backgrounds,positioning}
\begin{document}
\begin{tikzpicture}
\node (a) at (0,0) {some title above};
\matrix[below=0cm of a] {
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\
  \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; & \shuffleducks\pic[/duck/\randomhead,scale=0.3] {duck}; \\      
};
\begin{scope}[on background layer]
  \path[draw=orange,rectangle,fill=orange!20,minimum height=5cm, minimum width=4cm, inner sep=0,rounded corners=5pt,dashed,line width=1pt] (current bounding box.south west) rectangle (current bounding box.north east);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Is it possible to add a title above the first row and inside the dashed box, like "Duck Matrix"? I tried add a node as a row but I couldn't make it across the four columns Dec 6, 2023 at 18:29
  • @TianjianQin See my updated question. However if that's your whole tikz picture, are you sure that tikz is the right tool? If you just want a box with a title, you could use a tcolorbox. Dec 6, 2023 at 20:44
  • This is an example of a piece of the whole picture, I actually have several to put together Dec 7, 2023 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .