# Interpolate variables into latex matrix in Pluto

**URL:** <https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698>\
**Category:** General Usage\
**Tags:** latex, markdown, pluto\
**Created:** [July 3, 2022, 12:36pm UTC](https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698 "2022-07-03T12:36:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 3, 2022, 12:36pm UTC](https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698/1 "2022-07-03T12:36:13Z")

</div>

Hi all,

As shown in the screen shot, I would like to reference variables inside a LaTeX matrix. [Here](https://gist.github.com/itsdfish/16e6c145fc43779a263d031d4dad6d72) is the code I am using. Any help would be greatly appreciated.

 ![Screenshot from 2022-07-03 08-31-32](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffd41f3a3b601f0a486d4281426f70e3dc53ed6e.png)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 4, 2022, 12:47pm UTC](https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698/2 "2022-07-04T12:47:10Z")

</div>

Perhaps another approach could parse a string of the code into markdown. This is what is described [here](https://stackoverflow.com/questions/45591286/for-r-markdown-how-do-i-display-a-matrix-from-r-variable) in R.

How can I convert

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

into markdown? I tried various solutions such as

```julia
md"""
     $(my_markdown)
"""

```

without success.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 8, 2022, 1:20pm UTC](https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698/3 "2022-07-08T13:20:45Z")

</div>

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](https://github.com/stevengj/LaTeXStrings.jl) package.

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

```julia
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 `$`:

```julia
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**
>
> ```julia
> ### 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
> 
> ```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 8, 2022, 2:48pm UTC](https://discourse.julialang.org/t/interpolate-variables-into-latex-matrix-in-pluto/83698/4 "2022-07-08T14:48:16Z")

</div>

> [@jd-foster](#):
>
> ```julia
> using LaTeXStrings
> L"""
> \mathbf{B} = \begin{bmatrix} %$(a) & %$(b) \\ %$(c) & %$(d) \end{bmatrix}
> """
> 
> ```

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