- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 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)
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
}
}
'''
# 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)
@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
最佳答案
我想我找到了为什么会这样:问题出在 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
的变化:
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
compile_math
函数,内部 try
块,将 return 语句替换为以下 if builder.config.imgmath_pdf2svg:
return path.join(tempdir, 'math.pdf')
else:
return path.join(tempdir, 'math.dvi')
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
imgmath.py
的最后添加一个新的配置条目: app.add_config_value('imgmath_pdf2svg', False, 'html')
imgmath_image_format = 'svg'
imgmath_latex = 'latexmk'
imgmath_latex_args = ['-pdf']
imgmath_pdf2svg = True # Available only in the custom `imgmath.py`
a
->
b
.
--- a/imgmath.py
+++ b/imgmath.py
@@ -15,7 +15,7 @@
import sys
import tempfile
from hashlib import sha1
-from os import path
+from os import path, symlink
from subprocess import CalledProcessError, PIPE
from typing import Any, Dict, List, Tuple
@@ -157,6 +157,11 @@
with open(filename, 'w', encoding='utf-8') as f:
f.write(latex)
+ for add_file in builder.config.imgmath_latex_additional_files:
+ filename = path.join(tempdir, path.basename(add_file))
+ if not path.exists(filename):
+ symlink(path.join(builder.confdir, add_file), filename)
+
# build latex command; old versions of latex don't have the
# --output-directory option, so we have to manually chdir to the
# temp dir to run it.
@@ -165,9 +170,15 @@
command.extend(builder.config.imgmath_latex_args)
command.append('math.tex')
+ output_extension = 'dvi'
+ if builder.config.imgmath_latex == 'xelatex':
+ output_extension = 'xdv'
+ if builder.config.imgmath_pdf2svg:
+ output_extension = 'pdf'
+
try:
subprocess.run(command, stdout=PIPE, stderr=PIPE, cwd=tempdir, check=True)
- return path.join(tempdir, 'math.dvi')
+ return path.join(tempdir, 'math.' + output_extension)
except OSError:
logger.warning(__('LaTeX command %r cannot be run (needed for math '
'display), check the imgmath_latex setting'),
@@ -177,7 +188,7 @@
raise MathExtError('latex exited with error', exc.stderr, exc.stdout)
-def convert_dvi_to_image(command: List[str], name: str) -> Tuple[bytes, bytes]:
+def run_external_command(command: List[str], name: str) -> Tuple[bytes, bytes]:
"""Convert DVI file to specific image format."""
try:
ret = subprocess.run(command, stdout=PIPE, stderr=PIPE, check=True)
@@ -203,7 +214,7 @@
command.append('--depth')
command.append(dvipath)
- stdout, stderr = convert_dvi_to_image(command, name)
+ stdout, stderr = run_external_command(command, name)
depth = None
if builder.config.imgmath_use_preview:
@@ -227,7 +238,7 @@
command.extend(builder.config.imgmath_dvisvgm_args)
command.append(dvipath)
- stdout, stderr = convert_dvi_to_image(command, name)
+ stdout, stderr = run_external_command(command, name)
depth = None
if builder.config.imgmath_use_preview:
@@ -239,6 +250,21 @@
break
return filename, depth
+
+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
def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]:
@@ -291,7 +317,10 @@
if image_format == 'png':
imgpath, depth = convert_dvi_to_png(dvipath, self.builder)
elif image_format == 'svg':
- imgpath, depth = convert_dvi_to_svg(dvipath, self.builder)
+ 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
@@ -396,8 +425,10 @@
['-gamma', '1.5', '-D', '110', '-bg', 'Transparent'],
'html')
app.add_config_value('imgmath_dvisvgm_args', ['--no-fonts'], 'html')
+ app.add_config_value('imgmath_pdf2svg', False, 'html')
app.add_config_value('imgmath_latex_args', [], 'html')
app.add_config_value('imgmath_latex_preamble', '', 'html')
+ app.add_config_value('imgmath_latex_additional_files', [], 'html')
app.add_config_value('imgmath_add_tooltips', True, 'html')
app.add_config_value('imgmath_font_size', 12, 'html')
app.connect('build-finished', cleanup_tempdir)
关于latex - hf-tikz 和 sphinx 打得不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57127182/
我正在尝试绘制一些曲线,但效果不是很好。 我需要什么 我得到了什么 \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots}
我正在尝试使用 Tikz 包绘制(无向)图,以便图中的某些节点之间存在多条边。有可能做这样的事情吗?我尝试了以下代码来尝试在节点之间获得至少两条边,但无济于事: \begin{tikzpicture}
我有一张tikz图片: \documentclass{standalone} \usepackage{tikz} \usetikzlibrary{positioning, shapes, shadow
我正在尝试 quarto对于我的幻灯片,想知道如何在 tkiz 中使用 \pause代码。最低工作示例如下,供引用。 --- title: "Test Slides" format: reveal
假设我想在 TikZ 中构建一个循环图,节点数量可变,标记为 v0、v1、...、v_n-1,我选择了一些 n(比如 5)。 我希望能够在 TikZ 中使用\foreach 命令迭代地 (1) 定义节
这个问题是由here问题引起的 我想产生一个跨大括号的花括号。 问题是我必须手动对齐x坐标,这不是一个干净的解决方案。 目前我使用 \begin{frame}{Example} \begin{item
我想拍一张 PGF 图片,将其缩小(也可以缩放文本,所以 \resizebox 将是完美的)以获得一定的高度,然后在图片的左侧放置一个旋转的文本并创建一个包含所有内容的节点。 像这样的东西: 但我希望
我有这个阴影圆圈: \shade [ball color=black!80!white] (0,0) circle (1); 是否可以用node样式封装? (包括固定圆半径?) 最佳答案 \node[
我正在尝试使用宏来构建我的树,但我没有得到我想要的结果。这是一个最小的例子: \documentclass{article}\usepackage{tikz}\usetikzlibrary{trees
尽管图表足够宽(轴线样式),但以下图表的外部列被切掉了。设置图表的宽度也无济于事。任何想法?增加条形宽度需要设置或放大什么? \begin{tikzpicture} \begin{axis}[
如何在 Mac Os X(10.6.8) 上的 gnuplot 中启用 tikz 终端? 我有工作 tikz 的 latex 。现在我从 http://www.lua.org/ 安装了 lua并下载g
我可以使用\path 绘制一条穿过 2 条边的线吗? 考虑: \documentclass{article} \usepackage{tikz} \usetikzlibrary{positioning
这里有人是 Tikz-timing 方面的大师吗? 我正在寻找一种用注释来标记时序图的方法,但垂直比例太压缩了。我找不到\timing 命令的完整选项列表(或者即使找到了,也只是一个微薄的列表)。 我
我需要将图表的每一行与中心对齐。我正在尝试用 xshift 来做到这一点。代码如下: \begin{tikzpicture}[node distance=1.5cm, auto, text c
我想弄清楚如何在 tikz 中的节点和其他两个节点之间的边缘标签之间绘制一条边。这是我正在尝试做的一个例子: 这是我的代码: \documentclass[11pt]{article} \usepac
这是一个后续问题 here .如图所示,我需要制作一个圆形灰色框。 根据答案,我尝试了一些东西,但没有那么富有成效。所以,另一个问题来了。 我想出了以下命令,但它不起作用。 minipage 中的逐字
我的 R 包小插图使用 tikz/pgf 图形。 R CMD check 命令在未安装 LaTeX 包 tikz/pgf 的操作系统上抛出错误消息。例如,在 vanilla Ubuntu 系统上,必须
这就是我想要做的 \begin{tikzpicture} [node distance = 1cm, auto,font=\footnotesize, % STYLES
我是第一次用TikZ,不知道这个分类图怎么画。 我只绘制了 2 列,代码如下: \documentclass{article} \usepackage{tikz} \usetikzlibrary{sh
是否有一种简单的方法可以使用以下内容增加箭头提示的大小: \tikzset{myptr/.style=->, ????} 没有从头开始设计新的箭头样式? 最佳答案 一种解决方案,非常快,只是缩放箭头是
我是一名优秀的程序员,十分优秀!