1

Is there a way to put a formula in a variable (as in like a programming language variable) so that it can be referred to and rendered later? I was able to do this in Mathematica, but I'm moving away from Mathematica because I don't want to pay for it. I want to change to something more openly available like LaTex for displaying math formulas.

Note: I'm using Jupyter Notebook, which happens to use MathJax for LaTex rendering.

Though I mentioned programming language variables, Mathematica, and Jupyter Notebook, I am asking about doing this purely in LaTex (MathJax in my case).

I didn't have a lot of time with Mathematica, but here's an example of something that could be done in Mathematica

some_function = a/b
x = some_function/c
Display[x];

The result would be something like this:

a/b/c

  • 2
    Hi, I am slightly confused as you mention Mathematica and Jupyther (Pyton). Both are more a programming language. LaTeX is more for typesetting. Could you explain your questions a little further. Maybe with an example? Otherwise, take a look at \newcommand, which could go in the right direction. \pgfmathdeclarfunction could also be an option for you. – Gunter Jun 04 '21 at 14:17
  • 2
    jupyter notebooks don't (I think) runlatex by default. Are you using mathjax (which is a javascript emulation of a subset of latex math markup) it is rather hard to tell what your intended input and output is. – David Carlisle Jun 04 '21 at 14:22
  • 1
    https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/python-markdown/readme.html perhaps? Edit: that has nothing to do with LaTeX though, as said above a Notebook doesn't actually use LaTeX. – Torbjørn T. Jun 04 '21 at 16:17
  • Jupyter Notebook uses Mathjax by default, and Mathjax supports LaTex. – Frank Henard Jun 04 '21 at 20:01
  • 2
    mathjax emulates a subset of latex syntax but does not use tex at all, it is mostly off topic on this site. (the answer to your meta question is that mathjax is not enabled on this site, to show tex output post an image of the rendered document) – David Carlisle Jun 04 '21 at 21:30
  • @DavidCarlisle, I see what you're saying. I'm so glad that this site is kind enough to allow questions that aren't only about proprietary tex, but also about general usage of latex syntax. – Frank Henard Jun 04 '21 at 22:12
  • but it isn't clear what you are asking, you could define tex commands as in Gunter's answer, mathjax would support that more or less, but if you are asking about getting python (or julia or whatever language) variables from your jupyter notebook into the mathjax rendering then that is really a question about those languages and off topic here (apart from which I don't know the answer) – David Carlisle Jun 04 '21 at 22:15
  • @DavidCarlisle I was asking about doing it purely in MathJax's LaTex, but I will see if I can edit the question to make that more clear. Thanks for your input!

    I did also wonder about putting the LaTex string in the Jupyter Notebook's kernel language (Python or whatever chosen) and then somehow output that for Mathjax, but I realize that's out of the scope of this site, and thus I will avoid that for this question.

    – Frank Henard Jun 04 '21 at 22:17
  • @FrankHenard just put the newcommand lines inside $ so mathjax sees them then the posted answer should work. – David Carlisle Jun 04 '21 at 22:28
  • @FrankHenard one more thing: Mathematica is a functional programming language, with great symbolic math capabilities. This combination enables Mathematica to use the equation for calculation and can also create the LaTeX output. I am not familiar with MathJax, but Pythons capabilities is not really comparable to Mathematica in this sense. So you may need to change the way of working. Despite that, I would be happy to read your clarification of the question. Maybe I could learn other use cases of my tools at hand :-) – Gunter Jun 05 '21 at 18:16
  • Hey friends, I made some changes to the question in a way that I hope clarifies it. If you think that it could be worded better, please feel free to suggest changes, or if you have edit capabilities, go for it. – Frank Henard Jun 07 '21 at 15:17
  • @FrankHenard, thanks for the clarification. Is there a need for you to stick with Jupyter? Maybe other LaTex-solutions would suit your needs better. – Gunter Jun 07 '21 at 15:26
  • @Gunter, I am certainly open to suggestions. Here are my reasons for Jupyter Notebook: programmability, display math formulas (replace Mathematica), generally popular and accepted for data science. I would love to hear about alternatives though! – Frank Henard Jun 07 '21 at 15:29
  • OK, if the programmability is really necessary, than I cannot really suggest solutions. It just sounded that the LaTeX support is the only thing you might be using of Jupyter. – Gunter Jun 07 '21 at 15:33
  • @Gunter, What do you recommend for pure LaTex? Maybe I could give it a try too. – Frank Henard Jun 07 '21 at 15:38
  • @FrankHenard, we may leave the scope of the original question ;-). I am working on Windows and use MikTeX with TeXStudio as editor. That's great for me. This way I would not be limited to some subset of LaTeX. Overleaf should be very good as an online editor. – Gunter Jun 07 '21 at 15:52

1 Answers1

3

Given your example, I would come up with this for pure LaTeX:

\documentclass{standalone}

\newcommand{\somefunction}{\frac{a}{b}} \newcommand{\x}{\frac{\somefunction}{c}}

\begin{document} $\x$ \end{document}

enter image description here

Depending on the use cases, one could also consider placing the $ inside of the \newcommand, but this would limit the nesting.

Having no Jupyter at hand, I can not test, if this works also there.

Gunter
  • 443