From: Alexandre Christe <aleks.christe@gmail.com>
To: Hans Hagen via ntg-context <ntg-context@ntg.nl>
Subject: [NTG-context] Combining python and lua
Date: Wed, 25 Oct 2023 17:45:46 +0200 [thread overview]
Message-ID: <046d83a6-d4c5-4c01-99ed-57b83d555b86@Spark> (raw)
In-Reply-To: <990d094b-4733-492b-83f2-8014d51658c5@Spark>
[-- Attachment #1.1: Type: text/plain, Size: 2690 bytes --]
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
[-- Attachment #1.2: Type: text/html, Size: 3710 bytes --]
[-- Attachment #2: Type: text/plain, Size: 511 bytes --]
___________________________________________________________________________________
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
___________________________________________________________________________________
next parent reply other threads:[~2023-10-25 15:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <990d094b-4733-492b-83f2-8014d51658c5@Spark>
2023-10-25 15:45 ` Alexandre Christe [this message]
2023-10-25 19:17 ` [NTG-context] " Bruce Horrocks
2023-10-25 19:52 ` Aditya Mahajan
2023-10-26 9:21 ` Alexandre Christe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=046d83a6-d4c5-4c01-99ed-57b83d555b86@Spark \
--to=aleks.christe@gmail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).