Displaying JS Variables

132 views
Skip to first unread message

Thiago Brevidelli

unread,
Apr 3, 2018, 12:39:00 PM4/3/18
to MathJax Users
Hey guys, sort of a dumb question but I can't figure it out...
I got the following equation:

$$
R( \theta ) = sin^{ \class{hover}{P} }(\frac{\class{hover}{B} ⋅ \class{hover}{S} ⋅ \class{hover}{J} ⋅ \theta ⋅ ( \theta -\class{hover}{S})}{\class{
hover}{N}})  ⋅  \sqrt{\class{hover}{E}^{2} ⋅ cos^{2 ⋅ \class{hover}{H}}(\theta)+\class{hover}{F}^{2} ⋅ sin^{2 ⋅ \class{hover}{Z}}(\theta)}
$$

The thing is all the coefficients marked on red are actually global js variables (each one got it's own value which is randomly defined when the html document is opened), is there any way I can reference those variables so that the displayed equation looks something like this? :

$$
R( \theta ) = sin^{ \class{hover}{3} }(\frac{\class{hover}{6} ⋅ \class{hover}{2} ⋅ \class{hover}{13} ⋅ \theta ⋅ ( \theta -\class{hover}{2})}{\class{hover}{1800}})  ⋅  \sqrt{\class{hover}{1.2}^{2} ⋅ cos^{2 ⋅ \class{hover}{3}}(\theta)+\class{hover}{0.85}^{2} ⋅ sin^{2 ⋅ \class{hover}{2}}(\theta)}
$$
(coefficients marked on blue are just placeholder arbitrary values)

Thanks for the attention!

Davide Cervone

unread,
Apr 3, 2018, 12:45:14 PM4/3/18
to mathja...@googlegroups.com
There have been examples of this in the past, though I can't seem to locate the one I had in mind.  Here is one on the StackExchange network:


It shows how to use a javascript variable inside a MathJax expression and how to cause the expression to update.  That should point you in the right direction.

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thiago Brevidelli

unread,
Apr 3, 2018, 6:55:30 PM4/3/18
to MathJax Users
Thank you Davide, you were very helpful!

wdan...@gmail.com

unread,
May 11, 2020, 7:16:26 PM5/11/20
to MathJax Users
Hi I am new to MathJax. I am trying to do a simple calculation like adding 2 numbers, multiply 2 numbers, divide 2 numbers etc. and display the results using MathJax but could not figure out how. Could anyone help? Thanks!

David Farmer

unread,
May 11, 2020, 8:28:09 PM5/11/20
to MathJax Users

MathJax is for displaying mathematics, written in LaTeX
notation, in a web page. Not sure what you meant by
"do a simple calculation", so from what you wrote it is
not clear to me what you want MathJax to do.
> --
> You received this message because you are subscribed to the Google Groups "MathJax Users"
> group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> mathjax-user...@googlegroups.com.
> To view this discussion on the web visithttps://groups.google.com/d/msgid/mathjax-users/207663a7-f120-4372-b890-4d5bc7a9fc69%40googleg
> roups.com.
>
>

oscar...@ambassador.es

unread,
May 12, 2020, 5:25:46 AM5/12/20
to MathJax Users
You have posted in a wrong place. You have posted your question as answer of another person question. ¿Why would you need Mathjax to insert a simple number in HTML?. Mathjax is not meant to do math calculations, it is meant to insert math expressions in HTML. If you want to do math calculations in a HTML page you only need javascript.

William Dang

unread,
May 12, 2020, 7:59:28 PM5/12/20
to mathja...@googlegroups.com
Thanks for your reply David.
I just want to know if MathJax can actually add 2 numbers and give a result or it simply displays the math formulae or equations etc. Let say I want to get a total counts for the below 2:
\def\myTotal 
\myTotal = {{PAGESINCATEGORY:CAT-A|all}} + {{PAGESINCATEGORY:CATB|all}}
then I want to display \myTotal as a result at runtime.

Can I do that with MathJax?

Thanks!

Dang




You received this message because you are subscribed to a topic in the Google Groups "MathJax Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-users/88Hz2EFv_BI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathjax-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathjax-users/alpine.LRH.2.21.2005112025440.19313%40li375-150.members.linode.com.

dr.j.r....@gmail.com

unread,
May 13, 2020, 11:30:37 AM5/13/20
to MathJax Users
On Wednesday, 13 May 2020 00:59:28 UTC+1, William Dang wrote:
Thanks for your reply David.
I just want to know if MathJax can actually add 2 numbers and give a result or it simply displays the math formulae or equations etc. Let say I want to get a total counts for the below 2:
\def\myTotal 
\myTotal = {{PAGESINCATEGORY:CAT-A|all}} + {{PAGESINCATEGORY:CATB|all}}
then I want to display \myTotal as a result at runtime.

Can I do that with MathJax?



In my untested opinion, it depends on what you mean by "with". As far as
I know, MathJax itself is not able do general arithmetic.

But it seems to me quite likely that your arithmetic could be done in
JavaScript with the results being written to elements of the DOM tree,
and that those elements could then be re-rendered by one or more calls
to one or more MathJax routines.

The experts will know.
-- 
  (c) John Stockton, near London, UK.  Using Google Groups.           |
 Mail: J.R.""""""""@physics.org - or as Reply-To, if any.             |

David Farmer

unread,
May 13, 2020, 11:38:31 AM5/13/20
to MathJax Users

I also am not enough of an expert to say you can't do that
in MathJax, but I doubt that would work, and it does not
seem like the type of thing MathJax is designed to do.

But it is common to do calculations in Javascript, output
the results in LaTeX, and then have MathJax render it.
> --
> You received this message because you are subscribed to the Google Groups "MathJax Users"
> group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> mathjax-user...@googlegroups.com.
> To view this discussion on the web visithttps://groups.google.com/d/msgid/mathjax-users/98b3d66d-266c-4285-9498-3ba104fb5c81%40googleg
> roups.com.
>
>

William Dang

unread,
May 13, 2020, 12:50:06 PM5/13/20
to mathja...@googlegroups.com
Thanks for the notes David!

If MathJax has the built-in feature to support on the fly calculation then that would be more doable from my point of view; Otherwise, I will explore the JavaScript option.
Thanks and I appreciate your inputs David.

Dang

You received this message because you are subscribed to a topic in the Google Groups "MathJax Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-users/88Hz2EFv_BI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathjax-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathjax-users/alpine.LRH.2.21.2005131136330.28228%40li375-150.members.linode.com.

dr.j.r....@gmail.com

unread,
May 14, 2020, 6:25:30 PM5/14/20
to MathJax Users
On Wednesday, 13 May 2020 16:30:37 UTC+1, dr.j.r...@gmail.com wrote:

 
But it seems to me quite likely that your arithmetic could be done in
JavaScript with the results being written to elements of the DOM tree,
and that those elements could then be re-rendered by one or more calls
to one or more MathJax routines.


 The following works, except that the line marked *** needs to be extended.

function processCodeEval() {
  var string = eval(document.getElementById('h7j').value)
  document.getElementById('codeInput').value = string
  string = ("$$" + string + "$$").replace(/\s/g, "\\ ") // ***
  document.getElementById('outputDiv').innerHTML = string
  MathJax.Hub.Queue(["Typeset", MathJax.Hub, "outputDiv"]);
  return }

Enter a JavaScript calculation in element ID=h7j
Call processCodeEval
The Javascript result is shown in element ID=codeInput 
That is insufficiently pre-processed on the *** line
That is written to ID=outputDiv
And is then typeset

Peculiarities because I added a little part to an existing
HTML page, copied then modified an existing function by
adding the eval & replace, and want to go to bed.

Tested on JavaScript :   new Date + 5

The essential is that one constructs a MathJax representation
of whatever is wanted, loads it into a DIV, and queues that
for typesetting.  Which seems obvious by hindsight.
Reply all
Reply to author
Forward
0 new messages