hf-tikz和狮身人面像不能一起玩

Raf*_*azZ 7 latex tikz python-sphinx

我试图在狮身人面像中为矩阵添加一些颜色。我以前使用过hf-tikz。但是,当我将其添加到Sphinx时,它会错误地呈现它。

我想要得到的结果是

在此处输入图片说明

我得到的结果是

在此处输入图片说明

这是我的代码。

main.rst

.. math::

    \left(\begin{array}{cc}
        \tikzmarkin[style red]{a}a\tikzmarkend{a}
      & \tikzmarkin[style green]{b}b\tikzmarkend{b} \\
        \tikzmarkin[style blue]{c}c\tikzmarkend{c}
      & \tikzmarkin[style orange]{d}d\tikzmarkend{d} \\
    \end{array}\right)
    \star
    \left(\begin{array}{cc}
        \tikzmarkin[style red]{w}w\tikzmarkend{w}
      & \tikzmarkin[style green]{x}x\tikzmarkend{x} \\
        \tikzmarkin[style blue]{y}y\tikzmarkend{y}
      & \tikzmarkin[style orange]{z}z\tikzmarkend{z} \\
    \end{array}\right)
    =
    \left(\begin{array}{cc}
        \tikzmarkin[hor=style red]{aw}{a\star w}\tikzmarkend{aw}
      & \tikzmarkin[hor=style green]{bx}b\star x\tikzmarkend{bx} \\
        \tikzmarkin[hor=style blue]{cy}c\star y\tikzmarkend{cy}
      & \tikzmarkin[hor=style orange]{dz}d\star z\tikzmarkend{dz} \\
    \end{array}\right)

Run Code Online (Sandbox Code Playgroud)

conf.py

extensions = [
  'sphinx.ext.imgmath',
]

# Math configurations (https://tex.stackexchange.com/a/69770/51173)
imgmath_image_format = 'svg'
imgmath_use_preview = True
imgmath_latex_preamble = r'''
  \usepackage{xcolor}
  \usepackage[customcolors]{hf-tikz}
  \colorlet{myred}{red!50!purple!30}
  \colorlet{mygreen}{green!50!lime!60}
  \colorlet{myblue}{blue!50!white!50}
  \colorlet{myorange}{orange!80!red!60}
  \colorlet{mycyan}{cyan!90!blue!60}
  \colorlet{mymagenta}{magenta!90!red!60}
  \tikzset{
    style red/.style={
      set fill color=myred,
      set border color=white,
    },
    style green/.style={
      set fill color=mygreen,
      set border color=white,
    },
    style blue/.style={
      set fill color=myblue,
      set border color=white,
    },
    style orange/.style={
      set fill color=myorange,
      set border color=white,
    },
    style cyan/.style={
      set fill color=mycyan,
      set border color=white,
    },
    style magenta/.style={
      set fill color=mymagenta,
      set border color=white,
    },
    %
    hor/.style={
      above left offset={-0.15,0.31},
      below right offset={0.15,-0.125},
      #1
    },
    ver/.style={
      above left offset={-0.1,0.3},
      below right offset={0.15,-0.15},
      #1
    }
  }
'''
Run Code Online (Sandbox Code Playgroud)

生成文件

# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
SOURCEDIR     = source
BUILDDIR      = build

# Put it first so that "make" without argument is like "make help".
help:
    @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
    @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Run Code Online (Sandbox Code Playgroud)

make.bat

@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
    set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
    echo.
    echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
    echo.installed, then set the SPHINXBUILD environment variable to point
    echo.to the full path of the 'sphinx-build' executable. Alternatively you
    echo.may add the Sphinx directory to PATH.
    echo.
    echo.If you don't have Sphinx installed, grab it from
    echo.http://sphinx-doc.org/
    exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
Run Code Online (Sandbox Code Playgroud)

编辑:添加了用于生成rst的makefile

Raf*_*azZ 1

我想我找到了发生这种情况的原因:问题在于sphinx.ext.imgmath扩展名 + hf-tikz 不能很好地处理 DVI 文件。

当转换数学方程时,sphinx 创建一个非常基本的 Latex 文档,并将其编译latexpdf成 DVI 文件。之后,文件被转换为 SVG,并将生成的 svg 文件复制到 sphinx 的_build目录中。问题是 dvisvgm (由imgmath)无法转换 tikz 内容。另一种方法是使用扩展 DVI,但这也效果不佳。

解决方案是将所有内容编译为 PDF,然后将该 pdf 转换为 SVG。这有点问题,我发现的唯一方法是使用pdf2svg + pdfcrop。我最终将其修改imgmath.py为自定义扩展。以下是我在imgmath.py. 这些更改需要使用外部应用程序,因此我认为创建拉取请求没有任何优点(至少没有更具可扩展性的解决方案)。

变化imgmath.py

  1. 创建一个新方法:
def convert_pdf_to_svg(pdfpath, builder):
    # type: (str, Builder) -> Tuple[str, int]
    """Convert DVI file to SVG image."""
    tempdir = ensure_tempdir(builder)
    filename = path.join(tempdir, 'math.svg')

    name = 'pdfcrop'
    command = [name, pdfpath, pdfpath]
    run_external_command(command, name)

    name = 'pdf2svg'
    command = [name, pdfpath, filename]
    run_external_command(command, name)
    return filename, None
Run Code Online (Sandbox Code Playgroud)
  1. compile_math函数块内try,将 return 语句替换为以下内容
if builder.config.imgmath_pdf2svg:
  return path.join(tempdir, 'math.pdf')
else:
  return path.join(tempdir, 'math.dvi')
Run Code Online (Sandbox Code Playgroud)
  1. 在方法内部render_math,在块标题中# .dvi -> .png/svg,将try块替换为以下内容
try:
    if image_format == 'png':
        imgpath, depth = convert_dvi_to_png(dvipath, self.builder)
    elif image_format == 'svg':
        if self.builder.config.imgmath_pdf2svg:
            imgpath, depth = convert_pdf_to_svg(dvipath, self.builder)
        else:
            imgpath, depth = convert_dvi_to_svg(dvipath, self.builder)
except InvokeError:
    self.builder._imgmath_warned_image_translator = True  # type: ignore
    return None, None
Run Code Online (Sandbox Code Playgroud)
  1. 最后,在最后添加一个新的配置条目imgmath.py
try:
    if image_format == 'png':
        imgpath, depth = convert_dvi_to_png(dvipath, self.builder)
    elif image_format == 'svg':
        if self.builder.config.imgmath_pdf2svg:
            imgpath, depth = convert_pdf_to_svg(dvipath, self.builder)
        else:
            imgpath, depth = convert_dvi_to_svg(dvipath, self.builder)
except InvokeError:
    self.builder._imgmath_warned_image_translator = True  # type: ignore
    return None, None
Run Code Online (Sandbox Code Playgroud)

之后,您可以在conf.py中写入以启用tikz图像。

    app.add_config_value('imgmath_pdf2svg', False, 'html')
Run Code Online (Sandbox Code Playgroud)

imgmath扩展补丁。包括一些其他的东西:)

补丁是a-> b

imgmath_image_format = 'svg'
imgmath_latex = 'latexmk'
imgmath_latex_args = ['-pdf']
imgmath_pdf2svg = True  # Available only in the custom `imgmath.py`
Run Code Online (Sandbox Code Playgroud)