pdf 到 svg 轉換難題

pdf 到 svg 轉換難題

我正在尋找一種可靠的方法將 pdf 從 Latex 檔案轉換為 SVG 以在瀏覽器中顯示

\documentclass{article}
\pagenumbering{gobble}

\usepackage{blkarray}
\usepackage{xcolor}
\definecolor{darkred}{rgb}{0.55, 0.0, 0.0}

% -----------------------------------------------------------------------------------------------
\begin{document}
\begin{blockarray}{rrrrrrrrrr}
     \begin{block}{rrrr(cccc|c)!{\quad}l}
          &  &  &  & 1 & 2                   & 2  & 1  & 8  & the pivot is in row 2, col 2\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{$\boxed{1}$}}}       & -1 & 0  & 0  & It will be multiplied by\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{-2}}} & 1  & -2 & -5 & entries in column 1 of $E$ \\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{\ 1}}} & -1 & 1  & 2  & \\
     \end{block}
\end{blockarray}
\end{document}
  1. 我曾經使用過\documentclass{standalone},但不起作用,我最終刪除了頁碼並使用了以下醜陋的 shellscript
!/bin/sh
latexmk -pdflatex $1.tex && \
latexmk -pdflatex -c $1.tex && \
pdf2svg $1.pdf /tmp/temp_$1.svg && \
inkscape -D --without-gui --file=/tmp/temp_$1.svg --export-plain-svg $1.svg

問題

  • 有沒有更簡單的方法來創建 svg,以便將影像裁剪到實際範圍? (inkscape 的 -D 選項)
  • 有沒有更簡單的方法來建立 pdf,以便它只顯示該圖像,而不是整個頁面?
  • 包圍矩陣的括號在我大部分的嘗試中都被搞亂了。我需要做什麼來確保正確渲染括號
  • 任何解決方案都可以使用 xelatex 而不是 pdflatex 嗎?

答案1

我會使用TeXLive/MiKTeX 的一部分並積極維護,而不是pdf2svg未維護的。dvisvgm

--exact和的組合--zoom=-1將輸出修剪為可見內容,並使其具有響應性,這有助於嵌入到 HTML 中(選項已解釋)這裡)。

latex input
dvisvgm --exact --zoom=-1 --font-format=woff2 input

\documentclass{article}
\pagestyle{empty}

\usepackage{nicematrix}
\usepackage{blkarray}
\usepackage{xcolor}
\definecolor{darkred}{rgb}{0.55, 0.0, 0.0}

% -----------------------------------------------------------------------------------------------

\begin{document}
\begin{blockarray}{rrrrrrrrrr}
     \begin{block}{rrrr(cccc|c)!{\quad}l}
          &  &  &  & 1 & 2                   & 2  & 1  & 8  & the pivot is in row 2, col 2\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{$\boxed{1}$}}}       & -1 & 0  & 0  & It will be multiplied by\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{-2}}} & 1  & -2 & -5 & entries in column 1 of $E$ \\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{\ 1}}} & -1 & 1  & 2  & \\
     \end{block}
\end{blockarray}
\end{document}

答案2

只需添加缺少的amsmath.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{blkarray}
\usepackage{xcolor}
\definecolor{darkred}{rgb}{0.55, 0.0, 0.0}

% -----------------------------------------------------------------------------------------------
\begin{document}
\begin{blockarray}{rrrrrrrrrr}
     \begin{block}{rrrr(cccc|c)!{\quad}l}
          &  &  &  & 1 & 2                   & 2  & 1  & 8  & the pivot is in row 2, col 2\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{$\boxed{1}$}}}       & -1 & 0  & 0  & It will be multiplied by\\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{-2}}} & 1  & -2 & -5 & entries in column 1 of $E$ \\
          &  &  &  & 0 & {\colorbox{yellow}{\color{darkred}{\ 1}}} & -1 & 1  & 2  & \\
     \end{block}
\end{blockarray}
\end{document}

然後按照您的建議編譯並從 PDF 轉換為 SVG。

#!/bin/bash
latexmk -pdflatex $1.tex && \
latexmk -pdflatex -c $1.tex && \
pdf2svg $1.pdf $1.svg 

答案3

每個工具鏈在不同的情況下都會失敗,這取決於乳膠引擎、文件類和使用的套件。

我添加了當前(python/linux)版本的函數,將建立工具鏈tex_program -> svg_converter -> svg_crop作為命令參數列表,可在 python 呼叫中使用來運行子進程。不是參數就是將被呼叫的nexec次數。tex_program它並不適用於所有組合...

def build_commands( tex_program=["pdflatex"], svg_converter=[["pdf2svg"],".pdf"], use_xetex=False, use_dvi=False, crop=False, nexec=1):
    if isinstance( tex_program, (list,)) is False:
        tex_program = [tex_program]

    if tex_program[0]  == "pdflatex":
        if use_xetex is True:
            if use_dvi is True:
                if nexec > 1:
                    _tex_program = ["xelatex", "--no-pdf", "-etex" ]
                    _svg_converter = [["dvisvgm", "--font-format=woff2", "--exact"], ".xdv"]
                else:
                    _tex_program = ["latexmk", "-xelatex", "-etex" ]
                    _svg_converter = [["dvisvgm", "--font-format=woff2", "--exact"], ".xdv"]
            else:
                if nexec > 1:
                    _tex_program = ["xelatex", "-etex" ]
                    _svg_converter = [["pdf2svg"], ".pdf"]
                else:
                    _tex_program = ["latexmk", "-xelatex", "-etex" ]
                    _svg_converter = [["pdf2svg"], ".pdf"]
        else:
            if use_dvi is True:
                _tex_program = ["latexmk", "-etex", "-dvi" ]
                _svg_converter = [["dvisvgm", "--font-format=woff2", "--exact"], ".dvi"]
            else:
                _tex_program = ["latexmk", "-etex", "-pdf" ]
                _svg_converter = [["pdf2svg"], ".pdf"]
    else:
       _tex_program = tex_program
       _svg_converter = svg_converter
    if crop:
        _svg_crop = (["inkscape", "-D", "--without-gui", "--file"], ["--export-plain-svg"])
    else:
        _svg_crop = None

    return _tex_program, _svg_converter,_svg_crop

為了調用程序,我使用以下程式碼

tex_program.append( tex_path )
for _ in range(nexec-1):
    run_subprocess(tex_program, cwd=working_dir)
    check_output(tex_program, cwd=working_dir)

    svg_program = svg_converter[0] + [pdf_path, svg_path]
    check_output(svg_program, cwd=working_dir)

    if svg_crop is not None:
        crop_program = svg_crop[0] + [svg_path] + svg_crop[1] + [svg_path]
        check_output( crop_program, cwd=working_dir)

相關內容