Interpolate variables into latex matrix in Pluto

Hi all,

As shown in the screen shot, I would like to reference variables inside a LaTeX matrix. Here is the code I am using. Any help would be greatly appreciated.

Perhaps another approach could parse a string of the code into markdown. This is what is described here in R.

How can I convert

my_markdown = "\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}"

into markdown? I tried various solutions such as

md"""
     $(my_markdown)
"""

without success.

Hi Christopher. There are at least two ways to do this, one awkward way using the Julia standard library, and another one with the small LaTeXStrings package.

Standard library way, but involves lots of escaping on \:

import Markdown: MD, LaTeX
MD(LaTeX("\\mathbf{B} = \\begin{bmatrix} $(a) & $(b) \\\\ $(c) & $(d) \\end{bmatrix}"))

Using LaTeXStrings, with the interpolation done with %$ instead of just $:

using LaTeXStrings
L"""
\mathbf{B} = \begin{bmatrix} %$(a) & %$(b) \\ %$(c) & %$(d) \end{bmatrix}
"""

You can copy from below and paste into a .jl file to get a Pluto noteboook:

Pluto code
### A Pluto.jl notebook ###

# v0.19.9

using Markdown

using InteractiveUtils

# ╔═╡ 67da1efe-f879-4fd6-8f36-f0c60bc80d3c

using LaTeXStrings

# ╔═╡ 2f96d0a8-1b64-4928-9bbf-6e2e27fb2339

begin

a = 1

b = 2

c = 3

d = 4

end

# ╔═╡ 52142a20-febc-11ec-2ab2-b9ed33ccfa8e

import Markdown: MD, LaTeX

# ╔═╡ 2ecb04c0-e21e-4fb5-84e3-34cb4145e90e

MD(LaTeX("\\mathbf{B} = \\begin{bmatrix} $(a) & $(b) \\\\ $(c) & $(d) \\end{bmatrix}"))

# ╔═╡ dbdba330-1f1f-44c2-b3f9-044b676fba1f

L"""

\mathbf{B} = \begin{bmatrix} %$(a) & %$(b) \\ %$(c) & %$(d) \end{bmatrix}

"""

# ╔═╡ b5b63942-7c45-4c4a-97af-025a3e961d1c

# ╔═╡ 00000000-0000-0000-0000-000000000001

PLUTO_PROJECT_TOML_CONTENTS = """

[deps]

LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"

Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

[compat]

LaTeXStrings = "~1.3.0"

"""

# ╔═╡ 00000000-0000-0000-0000-000000000002

PLUTO_MANIFEST_TOML_CONTENTS = """

# This file is machine-generated - editing it directly is not advised

julia_version = "1.7.1"

manifest_format = "2.0"

[[deps.Base64]]

uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.LaTeXStrings]]

git-tree-sha1 = "f2355693d6778a178ade15952b7ac47a4ff97996"

uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"

version = "1.3.0"

[[deps.Markdown]]

deps = ["Base64"]

uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

"""

# ╔═╡ Cell order:

# ╠═2f96d0a8-1b64-4928-9bbf-6e2e27fb2339

# ╠═52142a20-febc-11ec-2ab2-b9ed33ccfa8e

# ╠═2ecb04c0-e21e-4fb5-84e3-34cb4145e90e

# ╠═67da1efe-f879-4fd6-8f36-f0c60bc80d3c

# ╠═dbdba330-1f1f-44c2-b3f9-044b676fba1f

# ╠═b5b63942-7c45-4c4a-97af-025a3e961d1c

# ╟─00000000-0000-0000-0000-000000000001

# ╟─00000000-0000-0000-0000-000000000002
3 Likes

Thank you for your help. Indeed, the solution using LaTeXStrings is very nice and simple. Thanks again!