ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Bruce Horrocks <ntg@scorecrow.com>
To: ntg-context mailing list <ntg-context@ntg.nl>
Subject: [NTG-context] Re: Combining python and lua
Date: Wed, 25 Oct 2023 20:17:35 +0100	[thread overview]
Message-ID: <4AC3B8EB-71B1-4132-A89C-5B453A6C3265@scorecrow.com> (raw)
In-Reply-To: <046d83a6-d4c5-4c01-99ed-57b83d555b86@Spark>

On 25 Oct 2023, at 16:45, Alexandre Christe <aleks.christe@gmail.com> 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.

You can use templates to subtitute values into a string and then send that out to be typeset.
For example: if you want to turn "sin(pi/4) = 1/2" to 

  \startformula
  \sin{\frac{\pi}{4}} = \frac{1}{2}
  \stopformula

Then use this Lua code:

  local myTrigTemplate = [[
    \startformula
    \%fname%{\frac{\pi}{%divisor%}} = \frac{%numerator%}{%denominator%}
    \stopformula
  ]]

  context.templates[myTrigTemplate] {
    fname = "sin",
    divisor = 4,
    numerator = 1,
    denominator = 2
  }

By combining them (using the result of a simple template as the parameter value into a larger template) you can create a function similar to Python's latex() that handles the trig functions you are using. That is the easy bit.

Converting the angle into radians and then getting the result as a fraction of pi requires you to implement a continued fraction algorithm to determine numerator and denominator.

Extending that to determine whether to use sqrt(3)/2 instead of some other fractional approximation is another significant step up in complexity.

I can send you some code that does that - but it's written in HP-71 BASIC so you'll have to adapt it. :-)

The real question is: do you really want to be able to generate an expression from *any* initial angle value or just those in your table? If it's just those in your table then I think it would be much, much less effort to extend 'angleList' with accompanying lists holding the values to substitute into the template. Or even just typeset the whole table by hand (prepare a list of the expressions and use editor macros to ConTeXtify them).

Regards,
—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
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	other threads:[~2023-10-25 19:22 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 ` [NTG-context] " Alexandre Christe
2023-10-25 19:17   ` Bruce Horrocks [this message]
2023-10-25 19:52   ` [NTG-context] " 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=4AC3B8EB-71B1-4132-A89C-5B453A6C3265@scorecrow.com \
    --to=ntg@scorecrow.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).