6

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:

screenshot of output

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.

3
  • 1
    With LuaLaTeX, you could define \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, but math.atan is a Lua command that can be accessed by LaTeX via a \directlua call.
    – Mico
    Mar 28 at 4:32
  • TikZ does evaluate everything that defines a coordinate, a distance, an anchor (angle) and such things with PGFMath. It will never apply the same for any text of a node (which is where you use \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.) Mar 28 at 9:04
  • \directlua isn't working for me: ! File ended while scanning use of \@argdef. Mar 28 at 10:18

2 Answers 2

5

You can set up a framework for named variables. I use \? for “evaluate the variable”. In order to print a value, use siunitx features.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn

\NewDocumentCommand{\setfp}{mm}
 {
  \fp_zero_new:c { l_phenomniverse_var_#1_fp }
  \fp_set:cn { l_phenomniverse_var_#1_fp } { #2 }
 }

\NewExpandableDocumentCommand{\?}{m}
 {
  \fp_use:c { l_phenomniverse_var_#1_fp }
 }

\ExplSyntaxOff

\begin{document}

\setfp{earthx}{-5}
\setfp{earthy}{3}
\setfp{shipx}{4}
\setfp{shipy}{-4}
\setfp{rotation}{60}

\setfp{trajectoryGradient}{(\?{earthy}-\?{shipy})/(\?{earthx}-\?{shipx})}
\setfp{trajectoryDeg}{atan(\?{trajectoryGradient})*180/pi}

\num{\?{trajectoryDeg}}

\num[round-mode=places,round-precision=0]{\?{trajectoryDeg}}

\num[round-mode=places,round-precision=2]{\?{trajectoryDeg}}

\end{document}

enter image description here

2
  • This seems to work quite well, although it does add some extra steps in places where the value would have been evaluated anyway. What is the relationship between \setfp and \def or \newcommand? With the latter two I can access the variable with \<variable name> whereas with \setfp it seems to require \?{<variable name>} to use. Is that correct? Mar 28 at 11:52
  • 1
    This answer works the best for everything that I tested it for, which included incorporating results of calculations into text, into tikzpicture nodes and node labels, and using the values to do further calculations both inside and outside of the tikzpicture scope. Mar 28 at 13:37
5

You can do calculations using Lua(LaTeX):

%!TEX program = lualatex
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{luacode}
\begin{document}

\newcommand\w{\directlua{tex.sprint(1/3)}}

This is my graph, it has width of \w and it looks very nice

\includegraphics[width=\w\textwidth]{}

\end{document}

Example copied from here

1
  • Thanks for your input. I played around with luatex options and found that it was throwing a lot of errors for reasons that I couldn't fathom, even after I got it working for some calculations. Mar 28 at 13:39

Not the answer you're looking for? Browse other questions tagged .