Skip to content

Commit 3cde19b

Browse files
committed
escape ampersand as well
1 parent ec66cbb commit 3cde19b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

test/test_escape_chars_reference.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
\begin{axis}[
44
tick align=outside,
55
tick pos=left,
6-
title={Foo & Bar Dogs\_N\_Cats},
6+
title={Foo \& Bar Dogs\_N\_Cats},
77
x grid style={white!69.01960784313725!black},
8-
xlabel={Foo & Bar Dogs\_N\_Cats},
8+
xlabel={Foo \& Bar Dogs\_N\_Cats},
99
xmin=-0.055, xmax=0.055,
1010
xtick style={color=black},
1111
y grid style={white!69.01960784313725!black},
12-
ylabel={Foo & Bar Dogs\_N\_Cats},
12+
ylabel={Foo \& Bar Dogs\_N\_Cats},
1313
ymin=-0.055, ymax=0.055,
1414
ytick style={color=black}
1515
]

tikzplotlib/axes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
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 common_texification as mpl_common_texification
44

55
from . import color
66

77

8+
def _common_texification(string):
9+
return mpl_common_texification(string).replace("&", "\\&")
10+
11+
812
class Axes:
913
def __init__(self, data, obj): # noqa: C901
1014
"""Returns the PGFPlots code for an axis environment.
@@ -36,14 +40,14 @@ def __init__(self, data, obj): # noqa: C901
3640
title = obj.get_title()
3741
data["current axis title"] = title
3842
if title:
39-
title = mpl_backend_pgf.common_texification(title)
43+
title = _common_texification(title)
4044
self.axis_options.append(u"title={{{}}}".format(title))
4145

4246
# get axes titles
4347
xlabel = obj.get_xlabel()
4448
xrotation = obj.xaxis.get_label().get_rotation()
4549
if xlabel:
46-
xlabel = mpl_backend_pgf.common_texification(xlabel)
50+
xlabel = _common_texification(xlabel)
4751
self.axis_options.append(u"xlabel={{{}}}".format(xlabel))
4852
if xrotation != 0:
4953
self.axis_options.append(
@@ -52,7 +56,7 @@ def __init__(self, data, obj): # noqa: C901
5256
ylabel = obj.get_ylabel()
5357
yrotation = obj.yaxis.get_label().get_rotation()
5458
if ylabel:
55-
ylabel = mpl_backend_pgf.common_texification(ylabel)
59+
ylabel = _common_texification(ylabel)
5660
self.axis_options.append(u"ylabel={{{}}}".format(ylabel))
5761
if yrotation != 90:
5862
self.axis_options.append(
@@ -559,7 +563,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
559563
# store the label anyway
560564
label = ticklabel.get_text()
561565
if ticklabel.get_visible():
562-
label = mpl_backend_pgf.common_texification(label)
566+
label = _common_texification(label)
563567
pgfplots_ticklabels.append(label)
564568
else:
565569
is_label_required = True

0 commit comments

Comments
 (0)