diff --git a/doc/usage/extensions/math.rst b/doc/usage/extensions/math.rst index a3dba8aac..1c64c5d17 100644 --- a/doc/usage/extensions/math.rst +++ b/doc/usage/extensions/math.rst @@ -106,8 +106,9 @@ built: installed (on Ubuntu xenial, it is available as `preview-latex-style`_). Therefore, the default for this option is ``False``. - Currently this option is only used when ``imgmath_image_format`` is - ``'png'``. + .. versionchanged:: 2.1.0 + + This option can also be used with ``imgmath_image_format`` set to ``'svg'``. .. confval:: imgmath_add_tooltips diff --git a/sphinx/ext/imgmath.py b/sphinx/ext/imgmath.py index 327be8973..6d007407a 100644 --- a/sphinx/ext/imgmath.py +++ b/sphinx/ext/imgmath.py @@ -87,6 +87,25 @@ DOC_BODY_PREVIEW = r''' ''' depth_re = re.compile(br'\[\d+ depth=(-?\d+)\]') +depthsvg_re = re.compile(br'.*, depth=(.*)pt') + + +def read_svg_depth(filename): + # type: (str) -> int + """Read the depth from comment at last line of SVG file + """ + with open(filename, 'r') as f: + for line in f: + pass + return int(line[11:-4]) + + +def write_svg_depth(filename, depth): + # type: (str, int) -> None + """Write the depth to SVG file as a comment at end of file + """ + with open(filename, 'a') as f: + f.write('\n' % depth) def generate_latex_macro(math: str, config: Config, confdir: str = '') -> str: @@ -95,6 +114,7 @@ def generate_latex_macro(math: str, config: Config, confdir: str = '') -> str: 'fontsize': config.imgmath_font_size, 'baselineskip': int(round(config.imgmath_font_size * 1.2)), 'preamble': config.imgmath_latex_preamble, + 'tightpage': '' if image_format == 'png' else ',tightpage', 'math': math } @@ -201,8 +221,18 @@ def convert_dvi_to_svg(dvipath: str, builder: Builder) -> Tuple[str, int]: command.extend(builder.config.imgmath_dvisvgm_args) command.append(dvipath) - convert_dvi_to_image(command, name) - return filename, None + stdout, stderr = convert_dvi_to_image(command, name) + + depth = None + if builder.config.imgmath_use_preview: + for line in stderr.splitlines(): # not stdout ! + matched = depthsvg_re.match(line) + if matched: + depth = round(float(matched.group(1)) * 100 / 72.27) # assume 100ppi + write_svg_depth(filename, depth) + break + + return filename, depth def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]: @@ -223,13 +253,19 @@ def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]: if image_format not in SUPPORT_FORMAT: raise MathExtError('imgmath_image_format must be either "png" or "svg"') - latex = generate_latex_macro(math, self.builder.config, self.builder.confdir) + latex = generate_latex_macro(image_format, + math, + self.builder.config, + self.builder.confdir) filename = "%s.%s" % (sha1(latex.encode()).hexdigest(), image_format) relfn = posixpath.join(self.builder.imgpath, 'math', filename) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename) if path.isfile(outfn): - depth = read_png_depth(outfn) + if image_format == 'png': + depth = read_png_depth(outfn) + elif image_format == 'svg': + depth = read_svg_depth(outfn) return relfn, depth # if latex or dvipng (dvisvgm) has failed once, don't bother to try again diff --git a/sphinx/templates/imgmath/preview.tex_t b/sphinx/templates/imgmath/preview.tex_t index 8dd24c108..f3fdcda07 100644 --- a/sphinx/templates/imgmath/preview.tex_t +++ b/sphinx/templates/imgmath/preview.tex_t @@ -9,7 +9,7 @@ \pagestyle{empty} <%= preamble %> -\usepackage[active]{preview} +\usepackage[active<%= tightpage %>]{preview} \begin{document} \begin{preview}