5

I want to add a pattern to a node in TikZ; how can I, for example, fill in the number 5 with the numbers 5 (ie a picture in a picture ...)?

Here is my MWE:

\documentclass[border=5pt,tikz]{standalone}
\usepackage[outline]{contour}
\begin{document}
    \begin{tikzpicture}
        \foreach \x in {1,1.2,...,3}
        \foreach \y in {1,1.3,...,4.2}
        {
            \node[xshift=-2cm,yshift=-2.5cm] at (\x,\y) {5};
        }
        \node[white] {\resizebox{3cm}{3cm}{\contour{blue}{5}}};
        \node[red] at (0,1.2) {5}; % <--| where it should be; kind of „invert the pattern onto the node“
    \end{tikzpicture}
\end{document}

P.S. excuses the messy source code, this is just a "shell". Of course I will clean it up myself.

Torbjørn T.
  • 206,688
current_user
  • 5,235

1 Answers1

5

Here is a proposal based on this answer and this answer. UPDATE: Empirically I find that after adding \path (5.south) -- ++ (0,-0.1); to the tikzfadingfrompicture environment, the result becomes better. I do not know why that seems to be necessary, nor how to avoid that manual shift.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{fadings}
\usepackage{contour}
\contournumber{32}
\begin{tikzfadingfrompicture}[name=A]
\node[transparent!0,scale=15] (5) at (0,0) {5};
\path (5.south) -- ++ (0,-0.1);
\end{tikzfadingfrompicture}
\begin{document}
    \begin{tikzpicture}
        \node[white,scale=15] {\contour{blue}{5}};
        \foreach \x in {1,1.2,...,3.4}
        \foreach \y in {0.1,0.4,...,4.5}
        {
            \path[path fading=A,fit fading=false] 
            ({\x-2},{\y-2.5}) node {5};
        }
\end{tikzpicture}
\end{document}

enter image description here

UPDATE: For completeness: a non-manual and more accurate fix by StefanH (full credit to him, of course).

\documentclass[border=5pt,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings,positioning}
\usepackage{contour}
\contournumber{32}
\begin{tikzfadingfrompicture}[name=5-0]
  \node[transparent!0,scale=15] (5) at (0,0) {5};
\end{tikzfadingfrompicture}
%%%
\begin{document}
\begin{tikzpicture}
  \makeatletter
  \node[white,scale=15,yshift=-0.5*\con@base@length]  {\contour{blue}{5}};
  \makeatother
  \foreach \x in {1,1.2,...,3.4}
  \foreach \y in {0.1,0.4,...,4.5}
  {
    \path[path fading=5-0,fit fading=false] 
    ({\x-2},{\y-2.5}) node {5};
  }
\end{tikzpicture}
\end{document}

enter image description here