[Tex/LaTex] includegraphics in tikz node

graphicstikz-pgf

I wanted to do this:

Crop jpeg into circular tikz node

but write with a variable width:

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\def\mywidth{2cm}
\tikz\node[circle,draw,minimum size=1.2*\mywidth
           text=white,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=1.2\mywidth]{frog}
               };
           }]{I'm watching you!};
\end{document}

Why does this not work? The includegraphics command does not display the image in 1.2\mywidth but some arbitrary size. The circle is the right size.

Second examples

Code 1

\documentclass[tikz,border=5mm]{standalone}
\begin{document}

\def\firstwidth{1cm}
\def\secondwidth{2\firstwidth}

\tikz\node[circle,draw,minimum size=\firstwidth,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=\firstwidth]{blackbox.png}
               };
           }]{};
\end{document}

Result 1

enter image description here

Code 2

\documentclass[tikz,border=5mm]{standalone}
\begin{document}

\def\firstwidth{1cm}
\def\secondwidth{2\firstwidth}

\tikz\node[circle,draw,minimum size=\firstwidth,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=0.5\secondwidth]{blackbox.png}
               };
           }]{};
\end{document}

Result 2 (should be like Result 1)

enter image description here

Best Answer

It seems that the problem is how are defined lengths \mywidth, \firstwidth and \secondwidth. If insted of \def\mywidth{2cm} etc use definitions:

\newlength\mywidth\setlength\mywidth{2cm}

\newlength\firstwidth
\setlength\firstwidth{1cm}

\newlength\secondwidth
\setlength\secondwidth{2\firstwidth}

Your example will work as you expected (of course, if I correctly understand your problem):

enter image description here

Complete MWE is:

\documentclass[tikz,border=5mm]{standalone}
    \usepackage{graphicx}

\begin{document}
\newlength\mywidth\setlength\mywidth{2cm}

\newlength\firstwidth
\setlength\firstwidth{1cm}

\newlength\secondwidth
\setlength\secondwidth{2\firstwidth}

\tikz\node[circle,draw,minimum size=1.2\mywidth,
           text=white,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=1.2\mywidth]{example-image-a}
               };
           }]{see};

\tikz\node[circle,draw,minimum size=\firstwidth,
           text=white,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=\firstwidth]{example-image-b}
               };
           }]{see};

\tikz\node[circle,draw,minimum size=\firstwidth,
           text=white,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=0.5\secondwidth]{example-image-c}
               };
           }]{see};
\end{document}