I would like to specify the units of an argument of a \newcommand that uses pgfkeys and that goes into a \tikzmath enviroment as a variable as follows:


 /myfam/.is family, /myfam,
  default/.style = {length = 10cm},
  length/.estore in = \mylen,

\pgfkeys{/myfam, default, #1}
 real \mylen, \mylennew;
 \mylennew = \mylen+1;   % do some math operation here
\draw (0,0) -- (\mylennew, \mylennew);   % this will specify the units as cm

  \myline[length=1] % the argument has no units here

In the above, \myline[length=1] results the variable in the \draw command have units in cm.

My questions are:

  1. Right after the \tikzmath enviroment, how can I specify the units of the variable \mylennew in the \draw command; may be something like:

    \draw (0,0) -- (\mylennew cm, \mylennew pt);
  2. If I would like to do something like \myline[length=1cm] or \myline[length=1pt], it does not give the result that I want. How can I specify the units with the arguments of \newcommand that uses pgfkeys.


I found out that, tikzmath converts any variable with centimeter (or milimeter) unit to corresponding point values (1 cm = 28.45274 pt) without keeping the unit (i.e. point). This is what happens within Tikzmath block:

\x = 1cm;
\newx = \x;  %\newx has a value of 28.45274

I am guessing that this is related to Tex math engine as well?

I provide two solutions on a different example:

    \usetikzlibrary{math, calc}

     /testfam/.is family, /testfam,
      default/.style={x = 1cm, y = 1},
      x/.estore in = \x, y/.estore in = \y}

    \pgfkeys{/testfam, default, #1}
      real \x, \y, \newx, \newy; 
      coordinate \cd;
      \newx1 = \x;
      \newx2 = \x/28.45274;
      \newy = \y;
      \cd1 = (\newx1 pt,1 cm);
      \cd2 = (2 cm,1 cm);
      \cd3 = (\newx2 cm,2 cm);
      \cd4 = (2 cm,2 cm);    
      \cd5 = (4 cm,0 cm);
      \cd6 = (4 cm,\y cm);
      print {$x=\x $};
      print {$newx1=\newx1 $};  
      print {$newx2=\newx2 $};
      print {$y=\y $}
      print {$newy=\newy $};

    \draw [line width = 1pt] (\cd1) -- (\cd2);

    \draw [line width = 1pt, color=blue] (\cd3) -- (\cd4);

    \draw [line width = 1pt, color=red] (\cd5) -- (\cd6);


            \testfam[x = 1cm, y=1] %Two cases: with and without units, 


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.