PyPlot and Latex labels using a variable

1,442 views
Skip to first unread message

Štěpán Starosta

unread,
Dec 15, 2015, 11:52:19 AM12/15/15
to julia-users
Hi,

this works for me

using PyPlot
imshow
([1 1])
title
(L"\frac{1}{2}")

I get the latex compiled. I want to print a label in latex based on a variable, i.e., something like this

s = "\frac{1}{2}"
title
(L"$s")

but I end up with "$s" printed in the image. I'd be grateful for any advice.

Mauro

unread,
Dec 15, 2015, 3:00:06 PM12/15/15
to julia...@googlegroups.com
`latexstring` is what you're looking for. Have a look at the readme of
https://github.com/stevengj/LaTeXStrings.jl

Example:

latexstring("an equation: \$1 + \\alpha^2\$")

Yichao Yu

unread,
Dec 15, 2015, 3:07:10 PM12/15/15
to Julia Users
On Tue, Dec 15, 2015 at 2:59 PM, Mauro <maur...@runbox.com> wrote:
> `latexstring` is what you're looking for. Have a look at the readme of
> https://github.com/stevengj/LaTeXStrings.jl
>
> Example:
>
> latexstring("an equation: \$1 + \\alpha^2\$")

I think for PyPlot it works equally well without `latexstring` since
pyplot will handle that directly.

Mauro

unread,
Dec 15, 2015, 3:54:54 PM12/15/15
to julia...@googlegroups.com
>> Example:
>>
>> latexstring("an equation: \$1 + \\alpha^2\$")
>
> I think for PyPlot it works equally well without `latexstring` since
> pyplot will handle that directly.

Sorry that was a bad example without interpolation. But the original
example needs latexstring, no?

julia> s = "\\frac{1}{2}"
"\\frac{1}{2}"

julia> title(latexstring("\$$s\$"))
PyObject <matplotlib.text.Text object at 0x7fb99a7ce6a0>

(Note the `\\` in `s`)

Yichao Yu

unread,
Dec 15, 2015, 4:00:52 PM12/15/15
to Julia Users
On Tue, Dec 15, 2015 at 3:54 PM, Mauro <maur...@runbox.com> wrote:
>>> Example:
>>>
>>> latexstring("an equation: \$1 + \\alpha^2\$")
>>
>> I think for PyPlot it works equally well without `latexstring` since
>> pyplot will handle that directly.
>
> Sorry that was a bad example without interpolation. But the original
> example needs latexstring, no?

I believe `title("\$\\frac{1}{2}\$")` works with PyPlot as well as the
string splicing version of it.

Mauro

unread,
Dec 15, 2015, 4:16:28 PM12/15/15
to julia...@googlegroups.com

On Tue, 2015-12-15 at 22:00, Yichao Yu <yyc...@gmail.com> wrote:
> On Tue, Dec 15, 2015 at 3:54 PM, Mauro <maur...@runbox.com> wrote:
>>>> Example:
>>>>
>>>> latexstring("an equation: \$1 + \\alpha^2\$")
>>>
>>> I think for PyPlot it works equally well without `latexstring` since
>>> pyplot will handle that directly.
>>
>> Sorry that was a bad example without interpolation. But the original
>> example needs latexstring, no?
>
> I believe `title("\$\\frac{1}{2}\$")` works with PyPlot as well as the
> string splicing version of it.

Sure thing, `title("\$$s\$")` does indeed work. Thanks! So neither
`L"..."` nor latexstring is ever needed in PyPlot?

Yichao Yu

unread,
Dec 15, 2015, 4:18:17 PM12/15/15
to Julia Users
On Tue, Dec 15, 2015 at 4:13 PM, Mauro <maur...@runbox.com> wrote:
>
> On Tue, 2015-12-15 at 22:00, Yichao Yu <yyc...@gmail.com> wrote:
>> On Tue, Dec 15, 2015 at 3:54 PM, Mauro <maur...@runbox.com> wrote:
>>>>> Example:
>>>>>
>>>>> latexstring("an equation: \$1 + \\alpha^2\$")
>>>>
>>>> I think for PyPlot it works equally well without `latexstring` since
>>>> pyplot will handle that directly.
>>>
>>> Sorry that was a bad example without interpolation. But the original
>>> example needs latexstring, no?
>>
>> I believe `title("\$\\frac{1}{2}\$")` works with PyPlot as well as the
>> string splicing version of it.
>
> Sure thing, `title("\$$s\$")` does indeed work. Thanks! So neither
> `L"..."` nor latexstring is ever needed in PyPlot?

latexstring shouldn't be needed AFAIK. @L_str helps to avoid
backslashes but should be functionally the same.
Message has been deleted

j2b2

unread,
Dec 16, 2015, 10:07:20 AM12/16/15
to julia-users
Sorry, because I'm a new user, Google needed almost 24h before publishing my answer, which is now obsolete

Le mercredi 16 décembre 2015 16:02:09 UTC+1, j2b2 a écrit :
julia> s="\\frac{1}{2}"
"\\frac{1}{2}"

julia> latexstring("$s")
L"$\frac{1}{2}$"


Reply all
Reply to author
Forward
0 new messages