Usage
A simple example
Let's begin by rendering a simple formula:
using LatexSVG
svg_output = latexsvg(raw"""
\[
i \hbar \frac{\mathrm{d}}{\mathrm{d} t} | \phi(t) \rangle = \hat{\mathcal{H}} | \phi(t) \rangle
\]
""")
latexsvg
is the main function of this package. Here with the most simple usage, we pass a string of LaTeX code and latexsvg
outputs the SVG image. Here it is automatically captured by Documenter.jl
and displayed inline in this webpage. Any SVG- or HTML-capable display environment, such as Documenter.jl
, Jupyter, VS Code, and Pluto.jl
, can display the SVG output.
In addition to the latexsvg
function, you can also use the @Lsvg_str
macro, which is functionally equivalent to latexsvg
:
svg_output = Lsvg"""
\[
i \hbar \frac{\mathrm{d}}{\mathrm{d} t} | \phi(t) \rangle = \hat{\mathcal{H}} | \phi(t) \rangle
\]
"""
The output of latexsvg
and Lsvg"..."
is not just a string of SVG code; it is a LaTeXSVG
object that contains the SVG as well as your LaTeX input and the preamble for record-keeping (You can customize the preamble; see the section Customizing the preamble.) Thus, if you are in an environment that cannot display SVG/HTML natively, such as the Julia REPL, this is what you'll see:
julia> svg_output = latexsvg(raw""" \[ i \hbar \frac{\mathrm{d}}{\mathrm{d} t} | \phi(t) \rangle = \hat{\mathcal{H}} | \phi(t) \rangle \] """)
LaTeXSVG SVG: <?xml version='1.0' encoding='UTF-8'?> <!-- This file was generated by dvisvgm 2.11.1 --> <svg version='1.1' xmlns='http://www.w3.org/200... <defs/> <g id='page1'> <path d='M183.513002 81.430038C183.513002 81.37... <path d='M187.17169 76.564285H189.490992C189.69... ......... LaTeX: \[ i \hbar \frac{\mathrm{d}}{\mathrm{d} t} | \phi(t) \rangle = \hat{\mathcal{H}} | \phi(t) \rangle \] preamble: \usepackage{amsmath,amsthm,amssymb} \usepackage{color}
In this case, you can either load an SVG/HTML-capable display or save the output directly to a file:
julia> savesvg("/path/to/file.svg", svg_output)
The functions and macros introduced so far have a number of extra features. Here are the complete descriptions:
LatexSVG.latexsvg
— Functionlatexsvg(latex::AbstractString, engine=texengine(); standalone=false, extra_args=[])
Renders latex
as an SVG string. latex
is the LaTeX code that you want to render, engine
is the LaTeX engine to use and defaults to the one currently in use for this session. You can change the engine for this session with texengine!
, or set the default permanently with config!
.
The output can be configured with a few keyword arguments:
- If
standalone=true
, it is assumed thatlatex
is a complete document, thus the preamble will be ignored. Otherwise (and this is the default)latex
will be inserted into a LaTeX document, whose preamble can be configured withadd_preamble!
and reset withreset_preamble!
. You can get the current complete preamble withpreamble
. extra_args
allows you to pass additional commandline flags/arguments to the LaTeX engine. For instance, if your LaTeX code containsminted
code blocks (for some reason), you would need to setextra_args=["--shell-escape"]
.
LatexSVG.@Lsvg_str
— MacroLsvg"..."
Renders the input as SVG with the current preamble and LaTeX engine.
Similar to raw"..."
strings, you do not have to escape special characters like \
and $
, which makes it easier to write LaTeX.
You can interpolate variables using the %$
syntax, which is the same as the L"..."
string macro in LaTeXStrings.jl
. For instance, if there is a variable x = 10
, then Lsvg"number %$x"
renders the string "number 10"
. However, unlike the L"..."
macro, Lsvg"..."
does not automatically wrap your input in a pair of dollar signs, since it is very much possible that you'll want to use some other LaTeX environments.
LatexSVG.LaTeXSVG
— TypeLaTeXSVG(latex::AbstractString, svg::String; standalone::Bool=false)
This type contains the LaTeX code to be rendered, the preamble, and the rendered SVG string. Both latexsvg
and @Lsvg_str
return objects of this type.
LaTeXSVG
objects contain 4 fields:
latex
contains the LaTeX input.standalone
indicates whetherlatex
is a complete LaTeX document. Ifstandalone=true
thenpre
is empty.pre
is a vector of strings containing the preamble, which is recorded from the global preamble when aLaTeXSVG
object is contructed, namely whenlatexsvg
or@Lsvg_str
is called.svg
contains the SVG string.
You can access these fields with the usual dot syntax.
You should never need to contruct a LaTeXSVG
object yourself.
If you are using VSCode, by default the color of the SVG displayed in the plot pane adapts to the color scheme of VSCode (black for light mode and white for dark mode.) Rest assured that this adaptive color is not written into the SVG itself. Also, if you defined custom colors in your LaTeX, it should be displayed (and saved to file) faithfully.
Configurations
Customizing the preamble
By default, the preamble is populated with
\usepackage{amsmath,amsthm,amssymb}
\usepackage{color}
You can configure the preamble with add_preamble!
and reset_preamble!
:
LatexSVG.add_preamble!
— Functionadd_preamble!(pre::AbstractString...; reset=false)
Adds preamble statements on top of the default preamble. pre
is any number of AbstractString
s.
If you already have a number of preamble statements and you would like to get rid of them first before adding new statements, pass reset=true
.
LatexSVG.reset_preamble!
— Functionreset_preamble!()
Resets the preamble to the default.
For example, here we can do
add_preamble!(
"\\usepackage[no-math]{fontspec}",
"\\setmainfont{texgyrepagella}[Extension=.otf,UprightFont=*-regular,ItalicFont=*-italic,BoldFont=*-bold,BoldItalicFont=*-bolditalic]",
"\\usepackage[euler-hat-accent,euler-digits]{eulervm}",
"\\usepackage{physics}"
)
6-element Vector{String}:
"\\usepackage{amsmath,amsthm,amssymb}"
"\\usepackage{color}"
"\\usepackage[no-math]{fontspec}"
"\\setmainfont{texgyrepagella}[Ex" ⋯ 68 bytes ⋯ "ld,BoldItalicFont=*-bolditalic]"
"\\usepackage[euler-hat-accent,euler-digits]{eulervm}"
"\\usepackage{physics}"
You can inspect the complete preamble with preamble
:
LatexSVG.preamble
— Functionpreamble()
Returns the current preamble.
add_preamble!
also returns the complete preamble immediately, as shown above.
Now let's render the same formula as the one in the previous section:
svg_output = Lsvg"""
\[
i \hbar \dv{t} \ket{\phi(t)} = \hat{\mathcal{H}} \ket{\phi(t)}
\]
"""
The input is now significantly more concise and the output has a much more distinct look.
One more example, just for fun:
maxwell = Lsvg"""
\begin{align*}
\div{ \vb{E} } &= \frac{\rho}{\epsilon_0} \\
\div{ \vb{B} } &= 0 \\ % someone please find
\curl{ \vb{E} } &= - \pdv{\vb{B}}{t} \\ % the magnetic monopole
\curl{ \vb{B} } &= \mu_0 \vb{J} + \mu_0 \epsilon_0 \pdv{\vb{E}}{t}
\end{align*}
"""
Changing LaTeX engine
At load time, LatexSVG searches for xelatex
, pdflatex
, and lualatex
in your PATH in order, and uses the first one it finds. You can also choose from the available options with texengine!
:
LatexSVG.texengine!
— Functiontexengine!(eng)
Sets the LaTeX engine for this session. eng
can be PDFLaTeX
, XeLaTeX
, or LuaLaTeX
, e.g.
texengine!(PDFLaTeX)
LatexSVG.texengine
— Functiontexengine()
Returns the current LaTeX engine.
LatexSVG.XeLaTeX
— TypeXeLaTeX
The xelatex
engine.
Command line options -no-pdf
, -quiet
, and -halt-on-error
are passed to the xelatex
executable, and both the input and output directories are also handled. If you would like to pass additional command line arguments, use the latexsvg
function.
LatexSVG.PDFLaTeX
— TypePDFLaTeX
The pdflatex
engine.
Command line options -output-format=dvi
, -quiet
, and -halt-on-error
are passed to the pdflatex
executable, and both the input and output directories are also handled. If you would like to pass additional command line arguments, use the latexsvg
function.
LatexSVG.LuaLaTeX
— TypeLuaLaTeX
The lualatex
engine.
Command line options --output-format=dvi
, --interaction=batchmode
, and --halt-on-error
are passed to the lualatex
executable, and both the input and output directories are also handled. If you would like to pass additional command line arguments, use the latexsvg
function.
Additionally, you can pass your LaTeX engine of choice directly to latexsvg
, as shown in the previous section.
Persisting the configurations
You can store your configurations and have them persist between Julia sessions using the config!
function:
LatexSVG.config!
— Functionconfig!(; texengine=nothing, preamble=nothing, export_prefs=false)
Configure persistent settings for the LaTeX engine and the preamble:
texengine
can beXeLaTeX
,PDFLaTeX
, orLuaLaTeX
preamble
can be anAbstractString
or aVector
ofAbstractString
s.
Two examples:
julia> config!(texengine=PDFLaTeX)
This sets the default LaTeX engine to be PDFLaTeX
for all future sessions.
julia> config!(preamble=["\usepackage{mathtools}", "\usepackage{xcolor}"])
This sets the default preamble to be
\usepackage{mathtools}
\usepackage{xcolor}
for all future sessions, replacing the original default of
\usepackage{amsmath,amsthm,amssymb}
\usepackage{color}
You can also call config!()
without any keyword argument. In this case your current LaTeX engine and preamble will be stored as the default.
These preferences are stored in a LocalPreferences.toml
file in your active project. If export_prefs=true
, they will instead be written into your Project.toml
. Either way, if you have multiple projects using this package you need to set the preference for them individually.
As another example, so far we've left XeLaTeX
as the LaTeX engine and configured a number of preamble statements; as a result, when we call config!()
, a LocalPreferences.toml
file is created in our project with this content:
[LatexSVG]
preamble = ["\\usepackage{amsmath,amsthm,amssymb}", "\\usepackage{color}", "\\usepackage[no-math]{fontspec}", "\\setmainfont{texgyrepagella}[Extension=.otf,UprightFont=*-regular,ItalicFont=*-italic,BoldFont=*-bold,BoldItalicFont=*-bolditalic]", "\\usepackage[euler-hat-accent,euler-digits]{eulervm}", "\\usepackage{physics}"]
texengine = "XeLaTeX"
Then, every time we load this package in the future, these preferences get loaded as the default.
You can also inspect your config with config
:
LatexSVG.config
— Functionconfig(key::String)
Obtain the persistent settings for the LaTeX engine and the preamble. key
can be "texengine"
or "preamble"
. Use config!
to configure these settings.
Use with other packages
The latexsvg
function accepts any AbstractString
object as input. Notably, output from LaTeXStrings.jl and Latexify.jl work out of the box. For example, you can do
using Latexify, LatexSVG
latexify(
[2//3 "e^(-c*t)" :(x/(x+k_1))
1+3im "gamma(n)" :(log10(x))], starred=true
) |> latexsvg
and the latexified matrix will be nicely rendered as SVG.