TransWikia.com

Tikz def arithmetic division

TeX - LaTeX Asked by Mike Tanner on September 2, 2021

This is, I hope, a trivial question, but one that has cost me a lot of time. I am drawing a simple TikZ picture. I define some variables which specify the coordinates of the lines, nodes and so on.

def Xa {4};
and then I can do
def Wdth {6};
def Xb (Xa+Wdth};

but the following does not work, it runs but uses the wrong value.

def Xmid {Xa+Wdth/2};

A second question is why can I not use variable names such as X1 rather then Xa

One Answer

Without knowing more about what you are trying to do it is difficult to say but I suspect that you are going about this the wrong way. If you want an easy way to access or reuse coordinates in tikz then you should use named nodes or coordinates:

coordinate (O) at (0,0);
coordinate (A) at (2,3);
draw (O)--(A);
node (B) at (3,2){B here};
draw(A)--(B);

If you want to set widths etc for nodes, or any tikz object, then tikzset is your friend:

tikzset{
  mynode/.style = {
     draw = blue,
     fill = yellow!10,
     minimum width = 20mm
  }
}

begin{tikzpicture}
  node[mynode] at (3,3){stuff}
end{tikzpicture}

Importantly, you can have parameters inside tikzset and you can also define pics, which are a very powerful way of reusing tikz code.

Finally, if you do really want to do calculations with variables like this then for integer calculations you can use:

numexprXa+Wdthrelax

and for non-integer calculations

 pgfmathparse{Xa+Wdth/2}pgfmathresult

or, if you need to save the result in a macro,

pgfmathsetmacroXmid{Xa+Wdth/2}

Of course, you can use pgfmathparse for integer calculations too but if you want an honest integer then it is advisable to declare this explicitly:

pgfmathparse{int(Xa+Xb)}pgfmathresult

Answered by user30471 on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP