File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -94,9 +94,15 @@ def repl_mathdefault(m):
9494
9595
9696def common_texification (text ):
97- """
97+ r """
9898 Do some necessary and/or useful substitutions for texts to be included in
9999 LaTeX documents.
100+
101+ This distinguishes text-mode and math-mode by replacing the math separator
102+ ``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
103+ are ignored.
104+
105+ The following characters are escaped in text segments: ``_^$%``
100106 """
101107 # Sometimes, matplotlib adds the unknown command \mathdefault.
102108 # Not using \mathnormal instead since this looks odd for the latex cm font.
Original file line number Diff line number Diff line change 1313import matplotlib .pyplot as plt
1414from matplotlib .testing .compare import compare_images , ImageComparisonFailure
1515from matplotlib .testing .decorators import image_comparison , _image_directories
16- from matplotlib .backends .backend_pgf import PdfPages
16+ from matplotlib .backends .backend_pgf import PdfPages , common_texification
1717
1818baseline_dir , result_dir = _image_directories (lambda : 'dummy func' )
1919
@@ -89,6 +89,16 @@ def create_figure():
8989 plt .ylim (0 , 1 )
9090
9191
92+ @pytest .mark .parametrize ('plain_text, escaped_text' , [
93+ (r'quad_sum: $\sum x_i^2$' , r'quad\_sum: \(\displaystyle \sum x_i^2\)' ),
94+ (r'no \$splits \$ here' , r'no \$splits \$ here' ),
95+ ('with_underscores' , r'with\_underscores' ),
96+ ('% not a comment' , r'\% not a comment' ),
97+ ('^not' , r'\^not' ),
98+ ])
99+ def test_common_texification (plain_text , escaped_text ):
100+ assert common_texification (plain_text ) == escaped_text
101+
92102# test compiling a figure to pdf with xelatex
93103@needs_xelatex
94104@pytest .mark .backend ('pgf' )
You can’t perform that action at this time.
0 commit comments