1

I am working on reusable image parts with Tikz. Thanks to the answer to TikZ 3.0---Multiple arguments for pic, I found out how to do macros with parameters.

But, the problem with using predefined pics is that tikzscale does not scale them with the rest of the image. Here a small example.

The main file contains the tikz macro and the include statements:

\documentclass{article}
\usepackage{tikz,tikzscale}
\usetikzlibrary{math}

\tikzset{pics/tripile/.style args={#1}{code={
  \coordinate (top) at (0,#1);
  \foreach \i in{0,...,#1}
  \foreach \j in{0,...,\i}
  {
    \tikzmath{
      \y = .3*(2/3*#1-\i)*cos(30);
      \x = .3*(\i/2-\j);}
    \draw (\x,\y) circle (3pt);
  }
}}}

\begin{document}
\thispagestyle{empty}
\includegraphics[width=.2\textwidth]{test.tikz}
\includegraphics[width=.3\textwidth]{test.tikz}
\includegraphics[width=.4\textwidth]{test.tikz}
\end{document}

The file test.tikz then provides the image

\begin{tikzpicture}[thick]
  \draw (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
  \pic at (1,1) {tripile=3};
\end{tikzpicture}

The output looks like

wrong image

but I would like the proportions between the square and the pile of circles to remain the same while scaling as in

correct image

Is there a way of defining a scalable pic?

  • By default, pics don't scale. You can use the key transform shape to enable this behaviour (pgf manual p. 252/section 18.2, pic syntax). (I.e. \pic at (1,1) [transform shape] {tripile=3}; Does that help you? – Huang_d Jun 08 '17 at 08:38
  • Great! If you write it as an answer, I can mark the problem as solved – Guido Kanschat Jun 08 '17 at 09:10

1 Answers1

3

By default, pics don't scale. You can use the key transform shape to enable this behaviour (pgf manual p. 252/section 18.2, pic syntax). In your particular example, this would be

\pic at (1,1) [transform shape] {tripile=3};

As a side-note, the pgf manual states that pics are slow; If you just draw circles, it may be faster to do this with nodes (which can be scaled using the same key, transform shape.)

Huang_d
  • 1,797