Dear list,

I’d like to know the best approach to make this a bit more usable. I’m trying 
to typeset a table with sin/cos/tan values. I’d like to be able to convert the 
angles in degrees into radians (with math notation) and to compute the 
trigonometric function leading to sin(pi/4) = 1/2, or cos(pi/6) = sqrt(3)/2 and 
display it in math mode.

Here’s my current MWE, where I fail to convert the angles in radians and fail 
to display the result in symbolic form.

\starttext

\startluacode
local angleList = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 240, 270, 
300, 315, 330, 360}

context.bTABLE()
 context.bTR()
 context.bTD() context("$x$") context.eTD()
 context.bTD() context("$0$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.0f$",a) context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\cos(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.3f$",math.cos(math.rad(a))) 
context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\sin(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.3f$",math.sin(math.rad(a))) 
context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\tan(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 if a == 90 or a == 270 then
 context.bTD() context("$-$") context.eTD()
 else
 context.bTD() context("$\letterpercent.3f$",math.tan(math.rad(a))) 
context.eTD()
 end
 end
 context.eTR()
context.eTABLE()
\stopluacode

\stoptext

\stoptext

One possible way would be to use Python and the t-filter module, like this

\usemodule[filter]
%It depends on your OS. In mine it's python3
\defineexternalfilter
 [pythontyping]
 [filtercommand={python3 \externalfilterinputfile \space > 
\externalfilteroutputfile},
 output={\externalfilterbasefile.tex},
 cache=yes,
 readcommand=\typefile,
 spacebefore=medium,
 spaceafter=medium]
%We copy settings to save typing
\defineexternalfilter[pythonformula][pythontyping]
%But instead of typing, we input a formula
\setupexternalfilter[pythonformula]
 [readcommand=\samplefile] %\input should also work

\starttext

Convert angle to radians:
$\startpythonformula
 from sympy import *
 print(latex(pi/180*60))
 \stoppythonformula$

Compute trigonometric function:
$\startpythonformula
 from sympy import *
 print(latex(cos(pi/3)))
 \stoppythonformula$

\stoptext

However, I haven’t managed so far to combine things (lua and Python) in an 
automatic way.

How should I proceed?

A. Christe
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to