# What is the best way to do math inside LaTeX macros?

I'm interested in writing a little personal package (or sharing it if it's actually useful) to speed up typesetting Trigonometry. I'd like commands like

\triangle{\alpha}{\beta}{\theta}{a}{b}{c}{30}{60}{90}
%% (label angles, label sides, absolute angles)


to draw the simple drawings that worksheets/notes need over and over. In addition, I'd like commands such as \solvedegtan{24} to display tan(24°) = 0.445 in the document.

I know I could probably accomplish this using python.sty (I've done that in the past) but what is the proper way to make LaTeX do the calculations for drawing (I'll do the actual drawing in TikZ) and solving?

Here's a LuaLaTeX-based method for setting up the macro \solvedegtan{24}:

% !TEX TS-program = lualatex
\documentclass{article}
\newcommand\solvedegtan[1]{%
\directlua{ tex.sprint ( math.tan ( math.rad (#1) ) ) }}
\begin{document}
\solvedegtan{24}
\end{document}


When dealing with the % character, which is "special" to both TeX and Lua (but in different ways), it's possible to "escape" the percent character while using the \directlua function. However, it's generally more convenient, coding-wise, to load the luacode package and to set up separate Lua-side and TeX-side code blocks. (Within a luacode environment, only the \ (backslash) character needs to be escaped.)

The following example illustrates the operation of the string.format function, set to show 5 digits after the decimal point. (The Lua function string.format is a front end to the C function sprintf; hence, rounding is applied if necessary.)

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment

%% Lua-side code
\begin{luacode}
function solvedegtan(x,prec)
return( tex.sprint (
string.format( "%."..prec.."f", math.tan ( math.rad (x) ) ) ) )
end
\end{luacode}

%% TeX-side code
\newcommand\solvedegtan[2]{\directlua{ solvedegtan(#1,#2) }}

\begin{document}
\solvedegtan{24}{5}
\end{document}

• Yeah, I had the feeling I'd end up learning Lua... I really wish PyTex was as good as LuaTeX seems... Any way to do it inside TeX or LaTeX though? – Ryan Feb 2 '15 at 20:05
• @ryan - There are various floating-point libraries that let you do trigonometric calculations within pdfLaTeX. One of the nice things about LuaLaTeX -- to me at least -- is that it's a superset of pdfLaTeX for the most part, i.e., apart from some things related to input encodings and font loading procedures. On top of that, you get access to Lua via a fairly simple interface. One such interface, the TeX-side macro \directlua, is used in the answer above; other interfaces exist as well. – Mico Feb 2 '15 at 20:21
• LuaTex is pretty cool! I'm liking the Lua, just stuck on how to round numbers... How does the % symbol fit into all this, since LaTeX uses it for comments and Lua uses it for string comprehension? – Ryan Feb 2 '15 at 20:39
• @Ryan - I've provided an addendum to show how to set up separate Lua and TeX code blocks, so that it's straightforward to use the % symbol in its Lua sense without having to go through escape contortions. – Mico Feb 2 '15 at 21:05
• Thank you, that's far superior to the solution I found using luatextra in this case. – Ryan Feb 2 '15 at 21:09

The pgf math routines loaded by tikz give pure LaTeX computations:

\documentclass{article}

\usepackage{tikz}
\newcommand\solvedegtan[1]{\pgfmathparse{tan(#1)}$$\tan(#1^\circ) = \pgfmathresult$$}

\begin{document}

\solvedegtan{24}

\end{document}


There is also a fixed point arithmetic library that you should investigate and a math library for programmatic constructions. Here is an example with the first library

\documentclass{article}

\usepackage{tikz,fp}
\usetikzlibrary{fixedpointarithmetic}

\newcommand\solvedegtan[1]{\pgfkeys{/pgf/fixed point arithmetic}%
\pgfmathparse{tan(#1)}$$\tan(#1^\circ) = \pgfmathresult$$}

\begin{document}

\solvedegtan{24}

\end{document}


And here is the original code use the tikz math library:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math}
\newcommand\solvedegtan[1]{$$\tan(#1^\circ) = \tikzmath{ \t = tan(#1); print \t;}$$}

\begin{document}

\solvedegtan{24}

\end{document}

• Does \pgfmathparse perform truncation rather than rounding by default? Or, is the fact that the last digit that's shown is 2 rather 3 a result of lack of precision in the tan function? – Mico Feb 2 '15 at 21:17
• @Mico I think it is lack of precision - as mentioned there is an fpu library one can load for better results. – Andrew Swann Feb 3 '15 at 7:32
• pgf is sometimes fickle to work with (is it a string or a number?) but works well otherwise. – Raphael Feb 3 '15 at 7:56

You can use this implementation of \solvedegtan in every place TeX is expecting a decimal number, even in something like

\setlength{\textheight}{\solvedegtan{60}\textwidth}


Here's the code

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\solvedegtan}{O{5}m}
{
\fp_eval:n { round ( tand (#2) , #1 ) }
}
\ExplSyntaxOff

\begin{document}

$\tan 45^\circ=\solvedegtan{45}$

$\tan 88^\circ=\solvedegtan[2]{88}$

\end{document}


The optional argument tells the maximum number of decimal digits for rounding.

The advantage over the nice LuaTeX solution by Mico is that this works with any TeX engine.