def test_rcupdate(): rc_sets = [{ 'font.family': 'sans-serif', 'font.size': 30, 'figure.subplot.left': .2, 'lines.markersize': 10, 'pgf.rcfonts': False, 'pgf.texsystem': 'xelatex' }, { 'font.family': 'monospace', 'font.size': 10, 'figure.subplot.left': .1, 'lines.markersize': 20, 'pgf.rcfonts': False, 'pgf.texsystem': 'pdflatex', 'pgf.preamble': ('\\usepackage[utf8x]{inputenc}' '\\usepackage[T1]{fontenc}' '\\usepackage{sfmath}') }] tol = [0, 13.2] if _old_gs_version else [0, 0] for i, rc_set in enumerate(rc_sets): with mpl.rc_context(rc_set): for substring, pkg in [('sfmath', 'sfmath'), ('utf8x', 'ucs')]: if (substring in mpl.rcParams['pgf.preamble'] and not _has_tex_package(pkg)): pytest.skip(f'needs {pkg}.sty') create_figure() compare_figure(f'pgf_rcupdate{i + 1}.pdf', tol=tol[i])
def test_usetex_packages(pkg): if not _has_tex_package(pkg): pytest.skip(f'{pkg} is not available') mpl.rcParams['text.usetex'] = True fig = plt.figure() text = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() mpl.rcParams['text.latex.preamble'] = ( r'\PassOptionsToPackage{dvipsnames}{xcolor}\usepackage{%s}' % pkg) fig = plt.figure() text2 = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() np.testing.assert_array_equal(text2.get_window_extent(), text.get_window_extent())
def test_xelatex(): rc_xelatex = {'font.family': 'serif', 'pgf.rcfonts': False} mpl.rcParams.update(rc_xelatex) create_figure() try: _old_gs_version = \ mpl._get_executable_info('gs').version < parse_version('9.50') except mpl.ExecutableNotFoundError: _old_gs_version = True # test compiling a figure to pdf with pdflatex @needs_pdflatex @pytest.mark.skipif(not _has_tex_package('ucs'), reason='needs ucs.sty') @pytest.mark.backend('pgf') @image_comparison(['pgf_pdflatex.pdf'], style='default', tol=11.7 if _old_gs_version else 0) def test_pdflatex(): if os.environ.get('APPVEYOR'): pytest.xfail("pdflatex test does not work on appveyor due to missing " "LaTeX fonts") rc_pdflatex = { 'font.family': 'serif', 'pgf.rcfonts': False, 'pgf.texsystem':
mpl.style.use("mpl20") mpl.rcParams['font.size'] = fontsize heights = {} fig = plt.figure() for vals in [(1, ), (-1, ), (-1, 1)]: fig.clf() for x in vals: fig.text(.5, .5, f"${x}$", usetex=True) fig.canvas.draw() # The following counts the number of non-fully-blank pixel rows. heights[vals] = ((np.array(fig.canvas.buffer_rgba())[..., 0] != 255).any(axis=1).sum()) assert len({*heights.values()}) == 1 @pytest.mark.skipif(not _has_tex_package('xcolor'), reason='xcolor is not available') def test_usetex_xcolor(): mpl.rcParams['text.usetex'] = True fig = plt.figure() text = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}' fig = plt.figure() text2 = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() np.testing.assert_array_equal(text2.get_window_extent(), text.get_window_extent())