Inline Math Rules / Reference / Cheat Sheet?

Is there a reference page somewhere in the Julia documentation with the rules for properly writing inline math strings? The string is then able to be converted with Latexify and used with Markdown to graphically render equations as shown below.

Step 1 - The inline math - julia> expression = :(y = x^2 / y - 2x)

Step 2 - Result using Latexify - y = \frac{x^{2}}{y} - 2 \cdot x

Step 3 - y = \frac{x^{2}}{y} - 2 \cdot x

Adding $ to front and end of Step 2 result and using Markdown will render formula as image - works here because this forum uses Markdown.

Latexify provides the result in Step 2 from Step 1 and Markdown uses that result and produces the image in Step 3. But itā€™s up to me to correctly input the original inline math string from a given equation. The algebra expression used here is easy to figure out, but for calculus and more complex equations, I need to know Juliaā€™s rules and methods to correctly provide that inline math string for a given expression. Anyone know of a reference or cheat sheet for doing this?

Iā€™m not sure if I understand your question completely. Juliaā€™s Markdown standard library can represent LaTeX, but you need to use something like Documenter.jl to render: LaTeX Syntax Ā· Documenter.jl. The renderer should handle all of the usual TeX math expressions - if youā€™re looking for help on the TeX side, there are many good cheat sheets out there, e.g. https://tug.ctan.org/info/undergradmath/undergradmath.pdf.

5 Likes

Excellent - Yes, I needed help on the TeX way of doing things.

For ex: x * x is written as x^2, but thereā€™s a lot more to know and I wanted it all in one place. I had thought Iā€™d need Julia to provide it, but learned right after posting that even though each language (Julia, Python, Matlab) handles the process in its own way, itā€™s LaTeX that creates the rules and conventions for the inline math. The link you sent is exactly what I needed - thanks.

It is still not clear to me what your starting point and your goal is. If you clarify those, Iā€™m sure people can direct you to more specific resources.

If you are just trying to render math nicely, I would skip Julia and use a tool like here to directly convert LaTeX code into an image.

Julia expressions, written with :() are parsed according to the normal rules of Julia code defined by JuliaSyntax when evaluated. Latexify has its own set of rules for converting Julia code to LaTeX code. Then whatever renderer you use may tweak things slightly from there. Note that Latexify includes its own render function to create an image (link). render will work in VSCode natively or in the basic REPL if you also add the package ElectronDisplay.jl. Alternatively, the display function will work if you are in a notebook (link).

expression = :(y = x^2 / y - 2x)
render(latexify(expression))

You keep referencing ā€œmath stringsā€, but you do not have any strings in your examples. Julia strings are written inside quotation marks. Use the Meta.parse function to transform a String into an Expression. This may be useful if you are reading equations from some external file and need to manipulate them.

s = "y = x^2 / y - 2x"
render(latexify(s))  # latexify also works on strings directly

expression = Meta.parse(s)
render(latexify(expression))

Also note that Unicode characters are valid Julia code, so you may not need Latexify to make your equations readable.

Ī± = 1:5        # \alpha<tab>
Ī±Ā² = Ī±.^2      # \^2<tab>
āˆ‘(x) = sum(x)  # \sum<tab>
julia> āˆ‘(Ī±Ā²)
55
2 Likes

Why convert to a image? If you are using latex already in which situation you would want to generate a image instead of feeding latex code to whatever generates your final document?

I wouldnā€™t convert to an image file. Itā€™s not really an image Iā€™m referring to - Iā€™m getting the terms wrong. Iā€™m referring to whatever you want to call this:

y = \frac{x^{2}}{y} - 2 \cdot x

Well the LaTex Equation Editor is pretty nice. I figured that the term ā€˜stringsā€™ wasnā€™t exactly right - but whatā€™s the term then? Iā€™m referring to this:

s = "y = x^2 / y - 2x"

You guys are the best here. But I have to admit that Iā€™m overwhelmed. Iā€™m a retired engineering tech, not a programmer. I just like to learn. But despite Juliaā€™s elegant syntax and the versatility and power of its packages, itā€™s really too complex for me. I may have to be content learning that which Iā€™m able to comprehend at my age (70) - bash scripting and TUI programs for Linux. Thanks for your kind support and the education

:sweat: Iā€™m sorry. I didnā€™t mean to overwhelm you. You are using the term ā€œstringā€ correctly this time. I was only picking on your wording because the format type of the equations changes the method needed for manipulating them. I am also just an engineer.

Julia itself is written as plain text which is stored in .jl files. It cannot show formatted math on its own. There are many options for rendering math to formats that can display it nicely (.html, .pdf, .jpg, etc.), and there are many Julia-adjacent tools that can help translating between these formats.

If you explain what your goal is in more detail, I can provide more focused guidance (that may or may not include Julia):

  • Where are you getting your equations from: copying them manually from a book or reading them automatically from a computer file?
  • What do you need to do with your equations: solve them, manipulate them, or render/view them?
  • Do you need a specific output format: document, image, or website?

My previous post threw a lot of potential solutions at you based on my guesses at what your problem might be, but I think I may not have been on the right track. I will hazard one more premature solution: Pluto.jl notebooks can both evaluate Julia code and render Markdown math in the same document (.html).

2 Likes

Itā€™s OK and I appreciate your help. Youā€™ve already given me enough to figure out how to proceed.

My situation re: Julia is maybe unique in that at my age, Iā€™m trying to choose one programming language to better understand programming itself. I began with C, which is unwieldy, outdated and problematic, but certainly vital and still at the root of our global systems. I liked it as a first language because you can see how the syntax and algorithms evolved together in those early days. But though you can create a program in C to say, create the Fibonacci series, itā€™s really a systems programming language - a fascinating area of computing, but very difficult.

I like Julia as a high-level language for its elegant syntax, amazing REPL and packages and for its focus on math and science computation. I donā€™t want to design websites or create games, so I felt that I found a language that I could stay with because of its math and science focus. Iā€™m reviewing Algebra in a serious way and I like that Julia can plot functions like parabolas, fractals and so much more. I learned that I could use Julia, Latexify and Markdown to render formulas, which led to my questions here. I learned from you that I donā€™t necessarily need Julia to do this - but thatā€™s why I like this forum. I may use LaTeX in algebra articles I write or maybe on GitHub. But just learning how to latexify and post an expression here on this forum is a tool I wanted to be able to use.

Again, my ā€˜needsā€™ are purely academic and Iā€™m aware that there are many here who have serious work to do. Iā€™m just playing with Julia - jumping around and learning. So Iā€™m a bit modest about asking questions here on Discourse. When I do, I get kindness and encouragement, as well as some tough-love answers and theyā€™re all on target. I am then overwhelmed by the complexity of the information that I requested!

Since you asked, I thought Iā€™d be clear about my personal approach here. I hope that my esoteric questions may prove useful in some small way and perhaps open topics that those who are busy with serious work wouldnā€™t have the time to contemplate. These questions will likely all be at an elementary level. As someone whoā€™s coming to love this language, I think itā€™s something I can offer to the community and Juliaā€™s creators - ie - not answers, but maybe good questions.

4 Likes

Thatā€™s a perfectly valid goal. Donā€™t be modest. Those who feel like helping will, and those who donā€™t wonā€™t.

Julia has a computer algebra package called Symbolics.jl. Here is a quick example using Symbolics and Plots in a Pluto notebook. (Symbolics uses Latexify automatically in Pluto notebooks.)

Iā€™ve used Pluto and Jupyter notebooks and also just used Plots and ā€˜savefigā€™ with Julia on my Linux PC to generate a .png file I can view and keep. But I see youā€™re fitting in Latexify here as well. I didnā€™t realize that it was possible to use a Julia package within the notebooks like this. Symbolics.jl is clearly the way to go and using the notebook sets up a good environment in which to learn and do it all. Thanks.

I would definitely put in a recommendation to play around with Pluto notebooks. You can embed LaTeX equations into a Markdown string, execute that code block, and the result will be rendered automatically. I find that itā€™s way more convenient than writing up something more formal in LaTeX, and if you hide all the code (by clicking a pop-up icon on the left side of the blocks) all thatā€™s left visible is a nice-looking report

md"""
The following equation can be used
```math
f(a) = \frac{1}{2\pi}\int_{0}^{2\pi} (\alpha+R\cos(\theta))d\theta
```
"""

More detailed example I just threw together:

Report appearance

Same notebook but with code blocks expanded

3 Likes

I like that idea of putting package dependencies at the bottom of the notebook in a Reference section! I generally find Pluto reactivity limiting, but that is a nice benefit.

Certainly a beautiful result. Iā€™ve been using Jupyter Notebook, but I like that Pluto is dynamic. In fact, just installed Pluto and will be working with it going forward - thanks.

Thanks. I love the fact that code block ordering doesnā€™t have to be linear in Pluto, since the blocks are automatically executed in dependency order anyway. Because of that, I usually wind up pushing minor implementation details down to kind of an appendix section at the bottom.

1 Like

Just copied your code and easily displayed the formula in Pluto.

My original question asked for some good sources for the LaTeX math / code.
stillyslalom provided this link:
https://tug.ctan.org/info/undergradmath/undergradmath.pdf - itā€™s a very helpful cheat sheet, but I wouldnā€™t mind having a couple of others. Do you know of any?

Hereā€™s an online cheat sheet Iā€™ve referenced before for LaTeX equation formatting:
LaTeX Cheat Sheet & Quick Reference

Also, hereā€™s a fun one if you know what symbol youā€™re looking for but donā€™t know what the symbol for it is:
Detexify LaTeX handwritten symbol recognition (kirelabs.org)

Itā€™s worth noting that the LaTeX math parser built into a lot of these tools, like the one in Pluto that translates Markdown blocks into displayed text/images, may not support some of the more exotic features that a full-fat LaTeX engine can handle, or those implemented by certain LaTeX libraries.

Outside of the Julia ecosystem, I tend to use TeXstudio for editing actual LaTeX documents. Also, I donā€™t personally use it, but I know some people who like to use GNU TeXmacs for generating documents: itā€™s not actually TeX/LaTeX-based, but a friendlier WYSIWYG editor that produces TeX-like documents.

1 Like

These are great resources and just what I was after - thanks very much.