I'm wanting to perform some calculations in latex for the purpose of building a diagram that can be re-used for different inputs. Specifically its related to drawing lines connecting two points and arcs relative to those points.
I'm finding that its pretty hard to find syntax for doing calculations because for the most part people are interested in finding out how to generate symbols to represent equations, rather than in actually doing calculations.
I actually need a bit of both of those.
In general though, where are good places to look for tips on writing maths functions in latex (as opposed to using latex to print nice looking maths equations)?
In particular, I want to calculate the arctangent of a slope connecting two points, where the x and y values of those points are stored in variables.
For example,
\def\xa{-5}
\def\ya{3}
\def\xb{4}
\def\yb{-4}
\def\rotation{60}
... works fine for both referring to those numbers as x,y coordinates for a node, etc, or as node text or labels. And I can perform calculations on them in a tikzpicture context, such as:
\draw [dashed, thick] (\xa, \ya) -- (\xa+4, {(tan(\rotation)*4)+\xa});
But if I try to define a new variable based on a calculation performed on my existing variables, it's just giving me the result as a string, rather than performing the calculation. For example,
\def\trajectoryGradient{(\earthy-\shipy)/(\earthx-\shipx)}
\def\trajectoryDeg{arctan(\trajectoryGradient)}
\node[label=below:{$\trajectoryDeg$}] at (-2, -2){};
... what I get is just the string version, not the calculated result:
How can I get it to do the calculation here? Maybe arctan isn't a latex command, but finding out if that is the case relates to the first part of my question.
\newcommand\slope{\directlua{ tex.sprint( math.atan(-4-3)/(4-(-5)))}}
and then use\slope
wherever it's needed.\arctan
isn't a (basic) LaTeX command, butmath.atan
is a Lua command that can be accessed by LaTeX via a\directlua
call.\trajectoryDeg
which is just text). You could use PGFMath here by using\pgfmathprint{atan2(-4-3,4--5)}
but it won't give you a good precision. Better use\fpeval
or Lua. (There are also extensions to PGFMath which provide higher precision but they are not as easy to set up.)