How to use LaTeX symbols in Julia
LaTeX is a technique consisting of developing documents using clear text stylized with specific markup tags in a similar way to HTML/CSS or Markdown. This technique is mainly used for authoring scientific papers.
In Julia's ecosystem, we can leverage the capabilities of the package Latexify.jl to convert a wide assortment of Julia objects to LaTeX-formatted strings.
This package allows changing objects of different types to the LaTeX format like:
Expressions
Strings
Numbers
And many more
Let's go through the following examples showing how to render a simple formula in the LaTeX format:
Before delving into these examples, let's clarify that the latexify function included within the package Latexify.jl, which only generates a LaTeXString object, but does not provide any visualization on its own. Therefore, to render this LaTeXString object, we will leverage the capabilities of the powerful, simple-to-use, and free Google Chart API.
With the Google Chart API, we can easily create a chart from some data and embed it in a webpage. An HTTP request targeting this API is submitted with the data and formatting parameters. Consequently, the API returns an image of the chart generated in PNG format. Using this API, we can generate an image showing a mathematical formula while submitting the data in LaTeX format.
Code example 1
This example shows how to render a simple formula in LaTeX Format using the package Latexify.jl and the Google Chart API:
using LatexifyLatexify.set_default(; env=:inline)formula = "a/(b_2)"output = latexify(formula)print(output)link = "https://chart.googleapis.com/chart?cht=tx&chl=" * outputprint("<br>",link)download(link, "/usercode/output/latex.png")
Code explanation
Now, let's scrutinize the code widget above:
Line 1: We load the package
Latexify.jl.Line 3: We set the output environment parameter of the package to
inlineto surround the output generated with$signs for inline rendering.Line 4: We specify the formula to transform.
Line 5: We invoke the
latexifyfunction to transform the specified formula to LaTeX format and store it in the variableoutput. Noting that thelatexifyfunction generates aLaTeXStringobject but does not provide any visualization on its own.Line 6: We display the output generated.
Line 8: We devise a link targeting the Google Chart API.
Line 9: We display the composed link.
Line 10: We call the link and download the generated image.
Code example 2
This example illustrates how to render a complex formula in the LaTeX Format:
using Latexifyusing HTTPLatexify.set_default(; env=:inline)formula = ["a/b" 5//8 5+8im; 1 :P_x :(gamma(2))]output = latexify(formula)print("<br>",output)encoded_output = HTTP.escapeuri(output)print("<br>",encoded_output)link = "https://chart.googleapis.com/chart?cht=tx&chl=" * encoded_outputprint("<br>",link)download(link, "/usercode/output/latex.png")
Code explanation
Now, let's explain the code widget above:
Line 1–2: We load the packages
Latexify.jlandHTTP.jl.Line 4: We set the output environment parameter of the package to
inlineto surround the output generated with$signs for inline rendering.Line 5: We specify the formula to transform provided in the form of an array, including a range of equations.
Line 6: We invoke the
latexifyfunction to transform the specified formula to LaTeX format and store it in the variableoutput.Line 7: We display the output generated.
Line 8: We call the
escapeurifunction included within the HTTP package to encode the value stored in the variableoutputin a URL-encoded format.Line 9: We display the encoded output.
Line 11: We devise a link targeting the Google Chart API.
Line 12: We display the composed link.
Line 14: We call the link and download the generated image.
Free Resources