Hi,

Thanks for your inputs Aditya and Bruce.

I’m still having some issues when trying to include the output of the filter module within lua.

MNWE:

\defineexternalfilter 
 [sympy]
 [filtercommand={/Users/aleks/miniconda3/bin/python3 \externalfilterinputfile\space > \externalfilteroutputfile},
 bufferbefore=init,
 output={\externalfilterbasefile.out},
 readcommand=\typefile,
 read=no,]

\startbuffer[init]
from sympy import *
\stopbuffer

\starttext

\startluacode
 local deg2rad = [[
 %\startsympy

 print(latex(rad(%angleVal%)))
 %\stopsympy
]]

local calcTrigFunction = [[
 \startsympy
 from sympy import *

 print(latex(%fname%(rad(%angleVal%))))
 \stopsympy
]]

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

for _, a in ipairs(angleList) do
 context.templates[calcTrigFunction] {
 fname = "sin",
 angleVal = a
 }
end

\stopluacode

\stoptext

In particular, it fails as soon as  \startsympy and \stopsympy are part of the template. Should I protect these?

A. Christe
Le 25 oct. 2023 à 21:56 +0200, Aditya Mahajan <adityam@umich.edu>, a écrit :
On Wed, 25 Oct 2023, Alexandre Christe wrote:

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

there is math.sind, cosd, etc. for computing sin for angle in degrees. The implementation is l-math.lua is:

local pipi = 2*math.pi/360

function math.sind(d) return sin(d*pipi) end
function math.cosd(d) return cos(d*pipi) end
function math.tand(d) return tan(d*pipi) end

and fail to display the result in symbolic form.

Context doesn't do symbolic math. You can try checking if one of the symbolic math libraries in lua provides all the features that you want.

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

This can work as follows. Take the array of angles, and convert them to the desired format in python and write them to a temp file. Read that temp file in lua and typeset it as you want.

Another option is an old proof of concept by Luigi: https://github.com/bastibe/lunatic-python which allowed two-way communication between python and luatex. Not sure if it still works.

Aditya

___________________________________________________________________________________
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
___________________________________________________________________________________