1

I try to make a table with a title in Python using LaTeX.

The following code creates a figure with a title, as I want the title to be.

import matplotlib.pyplot as plt

s1="a_b_c"
s2=r'''\begin{tabular}{ c | c | c } & 1 & 2 \\\hline 1 & 1 & 2 \\\hline 2 & 2 & 4 \end{tabular}'''

plt.figure(figsize=(4,5))
ax=plt.subplot2grid((1,1),(0,0))

ax.text(0.5,0.8,s1,ha="center",va="center",transform=ax.transAxes)

plt.show()

right title

But now I add two lines to the upper code which make the title wrong.

import matplotlib.pyplot as plt

s1="a_b_c"
s2=r'''\begin{tabular}{ c | c | c } & 1 & 2 \\\hline 1 & 1 & 2 \\\hline 2 & 2 & 4 \end{tabular}'''

plt.figure(figsize=(4,5))
ax=plt.subplot2grid((1,1),(0,0))

ax.text(0.5,0.8,s1,ha="center",va="center",transform=ax.transAxes)

###
plt.rc('text', usetex=True)
ax.text(0.3,0.2,s2,ha="left",va="bottom",transform=ax.transAxes,size=20)
###

plt.show()

enter image description here

What can I do to use LaTeX fonts and usual python fonts at the same time? Of course, without modifying the strings s1 and s2.

2 Answers 2

4

All you need to do is add a few lines to your file:

import matplotlib.pyplot as plt

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

And you have LaTeX fonts!

To get the a_b_c working like you wanted, you need to do:

a_{b_c}

Or the b and c will be at the same level.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and it still looks like the second figure I posted.. my issue is the "a" with underscore "bc", I want it just to be "a_b_c" as in the first figure
Oh, ha, sorry, I misunderstood. Then it's your LaTeX that's wrong - you need to do a_{b_c} and it should be fine. I'll edit my answer.
actually, I hoped to do it without modifying the string... is there no other way?
0

You can use the LaTeX function \textit{a_b_c} to write normal text.

Reference: https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.