0

I have a small image I want to reuse in another tikz picture.

Let's say file img.tex contains:

\begin{tikzpicture}
    \draw (0,0) circle (1);
\end{tikzpicture} 

and I want it to appear in several places in an other image

\newcommand\mycuteimage[1]{\scalebox{#1}{\input{img}}}

\begin{tikzpicture}
    \draw (0,0) grid (10, 1);
    \foreach \x in {2,5,9}
    {
        \node at (\x - 0.5, 0.5) {\mycuteimage{0.1}};
    }
\end{tikzpicture}

Can I somehow set the color of the inserted image, say sometimes red, sometimes blue?

Example:

\mycuteimage[blue]{0.1}

I've tried to use

{\color{blue} \mycuteimage{0.1}}

but it only changes the color of lines I didn't specify a color for, i.e., if, some elements were red in the original image they will remain red.

  • Why do you have to make a whole img.tex just to contain a figure? I think you should add the figure directly to the command definition. – user156344 Feb 27 '19 at 12:32
  • i intend to reuse it in more images – dEmigOd Feb 27 '19 at 13:11
  • This nests tikzpictures, which should be avoided. You could use a pic. – user121799 Feb 27 '19 at 13:58
2

Your proposal nests tikzpictures, which should be avoided. TikZ has pics for such purposes.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\tikzset{pics/.cd,
my cute pic/.style={code={
    \draw (0,0) circle (1);
}}}

\begin{tikzpicture}
    \draw (0,0) grid (10, 1);
    \edef\LstColors{{"red","green","blue"}}
    \foreach \X [count=\Y starting from 0]in {2,5,9}
    {
        \pgfmathsetmacro{\mycolor}{\LstColors[\Y]}
        \pic at (\X - 0.5, 0.5) [scale=0.1,color=\mycolor]{my cute pic};
    }
\end{tikzpicture}
\end{document}

enter image description here

pics also take parameters.

| improve this answer | |
  • And the result is ... the same. It colors only color-unspecified lines. So, if I have colored lines in original code - it stays the same. On the other hand \input works in code section !! – dEmigOd Feb 28 '19 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.