Skip to content

Commit b188ab6

Browse files
authored
Merge pull request #342 from nschloe/fix-332
title escaping
2 parents 84a22fb + 732086b commit b188ab6

8 files changed

+99
-51
lines changed

test/test_basic_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def plot():
2323

2424

2525
def test():
26-
assert_equality(plot, "test_basic_sin_reference.tex", table_row_sep="\\\\\n")
26+
assert_equality(plot, "test_basic_sin_reference.tex")
2727
return
2828

2929

test/test_basic_sin_reference.tex

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black, fill=white!89.80392156862746!black},
1111
tick align=outside,
1212
tick pos=left,
13-
title={Simple plot $\frac{\alpha}{2}$},
13+
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
1414
x grid style={white},
1515
xlabel={time(s)},
1616
xmajorgrids,
@@ -23,51 +23,51 @@
2323
ytick style={color=white!33.33333333333333!black}
2424
]
2525
\addplot [semithick, color0, mark=*, mark size=3, mark options={solid}]
26-
table [row sep=\\] {%
27-
0 0\\
28-
0.1 0.587785252292473\\
29-
0.2 0.951056516295154\\
30-
0.3 0.951056516295154\\
31-
0.4 0.587785252292473\\
32-
0.5 1.22464679914735e-16\\
33-
0.6 -0.587785252292473\\
34-
0.7 -0.951056516295154\\
35-
0.8 -0.951056516295154\\
36-
0.9 -0.587785252292473\\
37-
1 -2.44929359829471e-16\\
38-
1.1 0.587785252292474\\
39-
1.2 0.951056516295154\\
40-
1.3 0.951056516295154\\
41-
1.4 0.587785252292473\\
42-
1.5 3.67394039744206e-16\\
43-
1.6 -0.587785252292473\\
44-
1.7 -0.951056516295154\\
45-
1.8 -0.951056516295154\\
46-
1.9 -0.587785252292473\\
26+
table {%
27+
0 0
28+
0.1 0.587785252292473
29+
0.2 0.951056516295154
30+
0.3 0.951056516295154
31+
0.4 0.587785252292473
32+
0.5 1.22464679914735e-16
33+
0.6 -0.587785252292473
34+
0.7 -0.951056516295154
35+
0.8 -0.951056516295154
36+
0.9 -0.587785252292473
37+
1 -2.44929359829471e-16
38+
1.1 0.587785252292474
39+
1.2 0.951056516295154
40+
1.3 0.951056516295154
41+
1.4 0.587785252292473
42+
1.5 3.67394039744206e-16
43+
1.6 -0.587785252292473
44+
1.7 -0.951056516295154
45+
1.8 -0.951056516295154
46+
1.9 -0.587785252292473
4747
};
4848
\addlegendentry{sin}
4949
\addplot [very thick, color1, opacity=0.3, mark=*, mark size=3, mark options={solid}]
50-
table [row sep=\\] {%
51-
0 1\\
52-
0.1 0.809016994374947\\
53-
0.2 0.309016994374947\\
54-
0.3 -0.309016994374948\\
55-
0.4 -0.809016994374947\\
56-
0.5 -1\\
57-
0.6 -0.809016994374947\\
58-
0.7 -0.309016994374948\\
59-
0.8 0.309016994374947\\
60-
0.9 0.809016994374947\\
61-
1 1\\
62-
1.1 0.809016994374947\\
63-
1.2 0.309016994374947\\
64-
1.3 -0.309016994374947\\
65-
1.4 -0.809016994374947\\
66-
1.5 -1\\
67-
1.6 -0.809016994374948\\
68-
1.7 -0.309016994374946\\
69-
1.8 0.309016994374947\\
70-
1.9 0.809016994374947\\
50+
table {%
51+
0 1
52+
0.1 0.809016994374947
53+
0.2 0.309016994374947
54+
0.3 -0.309016994374948
55+
0.4 -0.809016994374947
56+
0.5 -1
57+
0.6 -0.809016994374947
58+
0.7 -0.309016994374948
59+
0.8 0.309016994374947
60+
0.9 0.809016994374947
61+
1 1
62+
1.1 0.809016994374947
63+
1.2 0.309016994374947
64+
1.3 -0.309016994374947
65+
1.4 -0.809016994374947
66+
1.5 -1
67+
1.6 -0.809016994374948
68+
1.7 -0.309016994374946
69+
1.8 0.309016994374947
70+
1.9 0.809016994374947
7171
};
7272
\addlegendentry{cos}
7373
\end{axis}

test/test_escape_chars.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://github.com/nschloe/tikzplotlib/issues/332
2+
import matplotlib.pyplot as plt
3+
4+
from helpers import assert_equality
5+
6+
7+
def plot():
8+
fig = plt.figure()
9+
plt.plot(0, 0, "kx")
10+
plt.title("Foo & Bar Dogs_N_Cats")
11+
plt.xlabel("Foo & Bar Dogs_N_Cats")
12+
plt.ylabel("Foo & Bar Dogs_N_Cats")
13+
return fig
14+
15+
16+
def test():
17+
assert_equality(plot, "test_escape_chars_reference.tex")
18+
return

test/test_escape_chars_reference.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
\begin{tikzpicture}
2+
3+
\begin{axis}[
4+
tick align=outside,
5+
tick pos=left,
6+
title={Foo \& Bar Dogs\_N\_Cats},
7+
x grid style={white!69.01960784313725!black},
8+
xlabel={Foo \& Bar Dogs\_N\_Cats},
9+
xmin=-0.055, xmax=0.055,
10+
xtick style={color=black},
11+
y grid style={white!69.01960784313725!black},
12+
ylabel={Foo \& Bar Dogs\_N\_Cats},
13+
ymin=-0.055, ymax=0.055,
14+
ytick style={color=black}
15+
]
16+
\addplot [semithick, black, mark=x, mark size=3, mark options={solid}, only marks]
17+
table {%
18+
0 0
19+
};
20+
\end{axis}
21+
22+
\end{tikzpicture}

test/test_externalize_tables_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
axis line style={white},
99
tick align=outside,
1010
tick pos=left,
11-
title={Simple plot $\frac{\alpha}{2}$},
11+
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
1212
x grid style={white},
1313
xlabel={time(s)},
1414
xmajorgrids,

test/test_line_color_marker_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
axis line style={white},
99
tick align=outside,
1010
tick pos=left,
11-
title={Simple plot $\frac{\alpha}{2}$},
11+
title={Simple plot \(\displaystyle \frac{\alpha}{2}\)},
1212
x grid style={white},
1313
xlabel={time(s)},
1414
xmajorgrids,

test/test_pandas_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib.pyplot as plt
2-
import pandas as pd
32

3+
import pandas as pd
44
from helpers import assert_equality
55

66

tikzplotlib/axes.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import matplotlib as mpl
22
import numpy
3-
from matplotlib.backends import backend_pgf as mpl_backend_pgf
3+
from matplotlib.backends.backend_pgf import (
4+
common_texification as mpl_common_texification,
5+
)
46

57
from . import color
68

79

10+
def _common_texification(string):
11+
# Work around <https://github.com/matplotlib/matplotlib/issues/15493>
12+
return mpl_common_texification(string).replace("&", "\\&")
13+
14+
815
class Axes:
916
def __init__(self, data, obj): # noqa: C901
1017
"""Returns the PGFPlots code for an axis environment.
@@ -36,13 +43,14 @@ def __init__(self, data, obj): # noqa: C901
3643
title = obj.get_title()
3744
data["current axis title"] = title
3845
if title:
46+
title = _common_texification(title)
3947
self.axis_options.append(u"title={{{}}}".format(title))
4048

4149
# get axes titles
4250
xlabel = obj.get_xlabel()
4351
xrotation = obj.xaxis.get_label().get_rotation()
4452
if xlabel:
45-
xlabel = mpl_backend_pgf.common_texification(xlabel)
53+
xlabel = _common_texification(xlabel)
4654
self.axis_options.append(u"xlabel={{{}}}".format(xlabel))
4755
if xrotation != 0:
4856
self.axis_options.append(
@@ -51,7 +59,7 @@ def __init__(self, data, obj): # noqa: C901
5159
ylabel = obj.get_ylabel()
5260
yrotation = obj.yaxis.get_label().get_rotation()
5361
if ylabel:
54-
ylabel = mpl_backend_pgf.common_texification(ylabel)
62+
ylabel = _common_texification(ylabel)
5563
self.axis_options.append(u"ylabel={{{}}}".format(ylabel))
5664
if yrotation != 90:
5765
self.axis_options.append(
@@ -558,7 +566,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
558566
# store the label anyway
559567
label = ticklabel.get_text()
560568
if ticklabel.get_visible():
561-
label = mpl_backend_pgf.common_texification(label)
569+
label = _common_texification(label)
562570
pgfplots_ticklabels.append(label)
563571
else:
564572
is_label_required = True

0 commit comments

Comments
 (0)