3

Quick question: See below, I am not able to use veclen value within a basic math calculation. I think - but I am not sure - that it is an issue about which kind of unit we get from veclen, I understand it is not cm, but I do not see how to convert to it.

\documentclass[24pt,a4paper,landscape]{scrartcl}  %%KOMA class
\setkomafont{sectioning}{\rmfamily\bfseries\boldmath}  %%
\usepackage{tikz}
\usepackage{calculator}
\usepackage{pgfplots}
\usetikzlibrary{rulercompass}
\usetikzlibrary{intersections,quotes,angles}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}
    \draw [color=black!5] (0,0) grid (6,5); 
    \draw (6,0) coordinate (a) node[right, below] {x}
    -- (0,0) coordinate (b) node[left] {(0,0)} 
    -- (0,5) coordinate (c) node[left] {y};


    \path  (0,3) coordinate (ad)   -- (35:4cm) coordinate (dd);
    \path  (0,0) coordinate (ad)   -- (30:4cm) coordinate (dd);
    \draw (ad) -- (dd) coordinate[pos=0.455](c1);
    \coordinate (c2) at ($(c1)!2*2.365 cm!(dd)$); 
    \draw[->,  ultra thick, red]
    let
    \p1=(ad),\p2=(c2),  \n1={{veclen(\x2-\x1,\y2-\y1)}}
    in
    (\x1,\y1) -- (\x2,\y2)
    node[label={{$*$}}] {}
    \pgfextra{\xdef\var{\n1}} ;
    \end{tikzpicture}

    \DEGREESSIN{30}{\sol}
    \TRUNCATE[2]{\sol}{\sol} 

    $sin(30)$ = \sol \newline
    \sol*\var \newline
    \MULTIPLY{\var}{\sol}{\res}  <<< IT DOES NOT WORK!!
    \begin{equation}  
     V  = \var + \res   <<< IT DOES NOT WORK!!
     \end{equation}
\end{document}

enter image description here

Note, I would like to use the package calculator, but otherwise is fine too.

mario
  • 761
  • you haven't defined \res. see if "Mathematical and Object-Oriented Engines" from tikz package (or \pgfmath package in case if tikz is not loaded) is useful for your calculation. – Zarko May 29 '17 at 00:57
  • @Zarko Yes, I see: \pgfmathparse{ \var * \sol} \pgfmathresult. Still, why isn't \MULTIPLY{\var}{\sol}{\res} defined? – mario May 29 '17 at 06:28
  • sorry, but where is MULTIPLY defined? I see this macro first time :-( – Zarko May 29 '17 at 06:51
  • @Zarko calculator package. – Torbjørn T. May 29 '17 at 07:25

1 Answers1

4

You can use the PGF function scalar to remove the unit (pt) from \var, for example as below. Personally I would probably not use calculator, but after \pgfmathsetmacro\var{scalar(\var)}, I think you can use \var with that package as well.

enter image description here

\documentclass[24pt,a4paper,landscape]{scrartcl}  %%KOMA class
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\round[2][2]{\pgfmathprintnumber[precision=#1]{#2}}

\begin{document}
\begin{tikzpicture}
\draw [color=black!5] (0,0) grid (6,5); 
\draw (6,0) coordinate (a) node[right, below] {$x$}
-- (0,0) coordinate (b) node[left] {(0,0)} 
-- (0,5) coordinate (c) node[left] {$y$};

\coordinate (ad) at (3,0);
\coordinate (ad) at (0,0);
\coordinate (dd) at ((30:4cm);
\draw (ad) -- (dd) coordinate[pos=0.455](c1);
\coordinate (c2) at ($(c1)!2*2.365 cm!(dd)$); 
\draw[->,  ultra thick, red]
let
\p1=(ad),\p2=(c2),  \n1={{veclen(\x2-\x1,\y2-\y1)}}
in
(\x1,\y1) -- (\x2,\y2)
node[label={{$*$}}] {}
\pgfextra{\xdef\var{\n1}} ;
\end{tikzpicture}

% strip away unit
\pgfmathsetmacro\var{scalar(\var)}

\pgfmathsetmacro\sol{sin(30)}
\pgfmathsetmacro\res{\sol*\var}
$\sin(30) = \sol$ and   $\sol\cdot\var$

\begin{equation}  
   V  = \round{\var} + \round{\res}
 \end{equation}
or with one decimal
\begin{equation}  
   V  = \round[1]{\var} + \round[1]{\res} 
\end{equation}
\end{document}
Torbjørn T.
  • 206,688
  • Of course, the value of \var and \res refer to the length in points, do you want to convert it to centimetres? – Torbjørn T. May 29 '17 at 11:13
  • I am not sure if I need, but it could be useful to know how, how do I do? – mario May 29 '17 at 11:20
  • @mario As I said in a comment on another question of yours, 1pt = 1/72.27in. And 1in is as you know 2.54cm, so to convert from pt to cm, multiply by 2.54/72.27. See also https://tex.stackexchange.com/questions/8260 – Torbjørn T. May 29 '17 at 11:27