On 25 Oct 2023, at 16:45, Alexandre Christe
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