# Latex in Luxor

**URL:** <https://discourse.julialang.org/t/latex-in-luxor/78985>\
**Category:** Visualization\
**Tags:** question, package, luxor\
**Created:** [April 4, 2022, 5:40am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985 "2022-04-04T05:40:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcfish](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@marcfish](https://discourse.julialang.org/u/marcfish)\
**Post date:** [April 4, 2022, 5:40am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/1 "2022-04-04T05:40:00Z")

</div>

i want to text latex in luxor but it seems wrong with fonts

ther is the code:

```julia
using Luxor
using MathTeXEngine

@svg begin
background("khaki")
f(t) = Point(4cos(t) + 2cos(5t), 4sin(t) + 2sin(5t))
setline(15)
fontsize(35)
@layer begin
    setopacity(0.4)
    sethue("purple")
    poly(20f.(range(0, 2π, length=160)), :stroke)
end
sethue("grey5")
text(L"f(t) = [4cos(t) + 2cos(5t), 4sin(t) + 2sin(5t)]", halign=:center)
end

```

But i get the wrong result:

 ![QQ图片20220404133816](https://global.discourse-cdn.com/julialang/original/3X/6/1/61f89952ce48b412a734023c4f9bc2aefb7fbdc4.png)

i have installed the fonts in the MathTeXEngine into the system.  
where is the problem and what can i do

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 4, 2022, 6:28am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/2 "2022-04-04T06:28:34Z")

</div>

Does this help?

[https://github.com/JuliaGraphics/Luxor.jl/issues/32](https://github.com/JuliaGraphics/Luxor.jl/issues/32)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 4, 2022, 7:30am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/3 "2022-04-04T07:30:49Z")

</div>

Yes the fonts have to be installed correctly for it to work. In my testing, the fonts have to be available from two places: inside the MathTEXEngine folder _and_ from the system’s font folder (which depends on the OS, among other things). While it does work eventually (as you can see from the examples in the docs), it’s sometimes a struggle getting there. The problem is that successfully installing fonts on all OS combinations isn’t as easy as it should be.

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [October 10, 2022, 4:30am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/4 "2022-10-10T04:30:23Z")

</div>

Any update on this? I have the same problem with Ubuntu Linux. MathTexEngine is working, but the resulting font is not Computer Modern.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 10, 2022, 6:24am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/5 "2022-10-10T06:24:01Z")

</div>

I would recommend the Luxor GitHub page. If there is nothing about this in the documentation, add a comment on an issue if you find one or make an issue if you don’t.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [October 10, 2022, 8:15am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/6 "2022-10-10T08:15:47Z")

</div>

It’s an active issue at the moment.

> <https://github.com/JuliaGraphics/Luxor.jl/issues/241>
>
> MathTeXEngine version 0.5 changed their LaTeX code, so please could Luxor-LaTeXe…rs check and report any issues with Luxor 3.5.0? Thanks!

It’s fair to describe LaTEX support as experimental at present. But if you don’t see the right fonts at all, then it might be something to do with how you installed them. Perhaps you could provide more details in a new issue …

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [May 3, 2023, 2:36am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/7 "2023-05-03T02:36:15Z")

</div>

I got it working nicely. One question, though.  
Does anyone know if drawing math variables in italics, as in latex, is possible?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 3, 2023, 8:45am UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/8 "2023-05-03T08:45:18Z")

</div>

Could you give an example of what you’d normally type in LaTeX? I can check the code to see if it’s possible. (Note that Latex support in Luxor is currently on hold, pending developments in Cairo.jl.)

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [May 3, 2023, 2:24pm UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/9 "2023-05-03T14:24:35Z")

</div>

For example, the figure below shows the text “coordinate” and the function name “sin” as regular text, while the variable “x” is shown in italics. The input for xlabel would be `L"$x$ coordinate"` and for ylabel `L"$\sin(x^2)$"`.

![](https://global.discourse-cdn.com/julialang/original/3X/5/7/579c43fe2c762f16fe87ffe26531eeab034331ed.png)  
I noticed that Makie can render this way using MathTexEngine; however, I could not figure out how they implemented that.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 3, 2023, 5:27pm UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/10 "2023-05-03T17:27:46Z")

</div>

> [@RaulDurand](#):
>
> I noticed that Makie can render this way using MathTexEngine; however, I could not figure out how they implemented that.

Haha, yes, I don’t _think_ this is possible. The Makie folks have implemented a powerful font system that does all kind of things; Luxor isn’t in the same league… 🙂

There’s a possible workaround for text placement, an old experimental function which was kind of made obsolete when MathTeXEngine.jl came along: `Luxor.textplace()` . I had to use it once…

```julia
@drawsvg begin
    background("black")
    sethue("gold")
    fontface("Cmr10")
    textplace("sin x2", Point(-250, 0),
        [
            (face = "Cmr10", color = colorant"gold", size = 200,), # s
            (), # i
            (), # n 
            (), # ' '
            (face = "Cmmi10",), #x
            (size = 150, shift = 50,), #2
        ],
        action=:fill
    )
end

```

 ![Screenshot 2023-05-03 at 18.17.59](https://global.discourse-cdn.com/julialang/original/3X/4/c/4cb155c04c9ccdb42c6ffc9a54996f618749c56a.png)

but doing this by hand for more than two text strings isn’t going to be fun, though…

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [May 3, 2023, 5:57pm UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/11 "2023-05-03T17:57:59Z")

</div>

> [@cormullion](#):
>
> but doing this by hand for more than two text strings isn’t going to be fun, though…

sure… haha

I thought changing the font while traversing each character in a LaTeXString would be possible in Luxor. It does not seem to be the case.

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [October 16, 2023, 11:48pm UTC](https://discourse.julialang.org/t/latex-in-luxor/78985/12 "2023-10-16T23:48:45Z")

</div>

I managed to display regular and italic text in equations using Luxor, LaTeXStrings, and MathTexEngine. Thanks @cormullion, for the insights.

Below is an example figure and the code I used. The function setlatex is similar to the settext function but for latex strings. I believe that something like this can be easily added to Luxor.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/8/28f8a7b43ff292c92a735bacb7cbede9e6aa14bb.png)

```julia
function getwidth(texelems)
    fsize = get_fontsize()
    width = maximum([elem[2][1] for elem in texelems], init=0.5)
    return width*fsize
end

function getheight(texelems)
    fsize = get_fontsize()
    maxh = maximum([elem[2][2] for elem in texelems], init=1.0)
    minh = minimum([elem[2][2] for elem in texelems], init=0.0)
    return (maxh-minh)*fsize
end

function setlatex(str, pt; halign="center", valign="center", angle=0)
    str = latexstring(str)
    elems = generate_tex_elements(str)
    fsize = get_fontsize()

    width = getwidth(elems)
    height = getheight(elems)

    rw = halign=="center" ? 0.5 : halign=="right" ? 1.0 : 0.0
    rh = valign=="center" ? 0.5 : valign=="top" ? 1.0 : 0.0

    xref = pt.x + rw*width
    yref = pt.y - rh*height

    θ = angle*pi/180
    T = [cos(-θ) -sin(-θ); sin(-θ) cos(-θ)]

    for elem in elems
        point = elem[2]
        x = pt.x + point[1]*fsize
        y = pt.y - point[2]*fsize
        elem[1] isa TeXChar && (y += 0.4*fsize)

        x, y = T*[x-xref, y-yref] .+ [pt.x, pt.y]
        if elem[1] isa TeXChar
            texchar = elem[1]
            scale = elem[3]
            glyph = texchar.represented_char

            fnt = texchar.slanted ? "NewComputerModern Italic" : "NewComputerModern Regular"
            setfont(fnt, fsize*scale)
            settext(string(glyph), Pt(x,y), halign="left", valign="bottom", angle=angle)
        elseif elem[1] isa HLine
            hline = elem[1]
            w = hline.width*fsize
            setline(hline.thickness*fsize)
            line(Pt(x, y), Pt(x+w*cos(-θ), y+w*sin(-θ)), :stroke)
        end
    end
end

```
