2

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:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{math}

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

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

\begin{document}
\begin{tikzpicture}
  \myline[length=1] % the argument has no units here
\end{tikzpicture}
\end{document}

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.

2

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:

    \documentclass[]{article}
    \usepackage{tikz}
    \usepackage{amsmath,amsthm}
    \usetikzlibrary{math, calc}

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

    \newcommand{\testfam}[1][]{
    \pgfkeys{/testfam, default, #1}
    \tikzmath{
      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);
    }

    \begin{document}

        \begin{tikzpicture}
            \testfam[x = 1cm, y=1] %Two cases: with and without units, 
        \end{tikzpicture}

    \end{document}

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.