4

I want to pass a variable to the minimum size key of a node, but it doesn't work. Here is what I tried:

\documentclass{article}

\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \pgfmathsetmacro{\R}{1.2 + 0.2}
  \node[draw,circle,minimum size=\R] {};
\end{tikzpicture}

\end{document}

Clearly I could use an explicit number e.g. minimum size= 1.4 cm but that's not what I want. So what would be the correct way to pass a variable to minimum size as above?

  • Use \R cm or multiply the macro result with a unit \R*1cm etc. Not every key is converted to canvas unit vectors. Minimum size is independent from the canvas coordinates – percusse Jun 16 '16 at 18:04
  • @percusse Thanks this works. Is there any way to make tikz interpret everything in terms of canvas unit vectors? – Julia Jun 16 '16 at 18:08
  • No. The math engine uses point system. But TikZ prefers centimeters. That's why it feels inconsistent but it is actually not – percusse Jun 16 '16 at 18:11
  • @percusse I don't really see why this is not inconsistent. Is there any reason why it would be a bad idea to open a feature request for this on sourceforge.net/p/pgf/feature-requests? – Julia Jun 16 '16 at 18:17
  • Because TeX arithmetic works on point system. You can open a feature request of course but I don't see the benefit. You just need to give a unit and you are done. – percusse Jun 16 '16 at 18:50
4

You must specify the dimensions:

\pgfmathsetmacro{\R}{1.2cm + 0.2cm}

Otherwise it is interpreted as points and the size of the circle is of the inner sep.

Edit:

\node is a command of Tikz and the default is in cm, as per the manual:

If you do not provide a unit, as in (2,1), you specify a point in pgf’s xy-coordinate system. By default, the unit x-vector goes 1cm to the right and the unit y-vector goes 1cm upward.

\pgfmathsetmacro is a pgfmath command and the default is in pt, as per the manual:

The mathematical engine of pgf is implicitly used whenever you specify a number or dimension in a higher-level macro. [...] As was explained earlier, the parser commands like \pgfmathparse will always return a result without units in it and all dimensions that have a unit like 10pt or 1in will first be converted to TEX points (pt) and, then, the unit is dropped.

  • 1
    If you do something like \draw (0,0) circle (2) the numbers are not interpreted as a point (instead of a multiple of a unit length). However as you write, if I don't give explicit dimensions in my example the number is interpreted as points. This seems to be inconsistent. Is there also a global option such that everything in a tikz picture is interpreted as a multiple of a unit vector as my first example? – Julia Jun 16 '16 at 18:01
  • @Julia What is a unit vector (in TikZ world) ? The xy coordinate system of TikZ uses two vectors : x and y. – Paul Gaborit Jun 16 '16 at 22:54
  • @Julia Here we are using two things. One is Tikz and the other is pgfmath. If no units are given, Tikz sets them as cm by default. This is natural since Tikz is at user level and cm is a "human" dimension. To the contrary, pgfmathuses points as the default length and converts all dimensions first to points. This is logical since it is working at a lower level where cm have no meaning. – Raoul Kessels Jun 17 '16 at 5:51
3

Not actually sure how useful/advisable this is:

\documentclass[tikz,border=5]{standalone}
\newdimen\tmpvec
\tikzset{Minimum Size/.code={%
    \pgfpointxy{#1}{#1}%
    \tikzset{minimum width/.expanded=\the\csname pgf@x\endcsname,
      minimum height/.expanded=\the\csname pgf@y\endcsname}%
}}
\begin{document}
\begin{tikzpicture}[every node/.style={draw, thick, dashed,
    anchor=south west, outer sep=0}]
  \draw [help lines] grid (3,2);
  \node [red, Minimum Size=1] {A};
  \node [green, y=(90:2cm), Minimum Size=1] at (2,0) {B};
  \node [blue, x=(0:2cm), y=(90:3cm), Minimum Size=1] at (0,0) {C};
\end{tikzpicture}
\end{document}

enter image description here

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.