PDF から SVG への変換パズル

PDF から SVG への変換パズル

ブラウザで表示するために、LaTeXファイルからPDFを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}ものはうまくいかなかったので、ページ番号を削除して次の醜いシェルスクリプトを使うことにしました。
!/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 を作成するより簡単な方法はありますか?
  • 行列を囲む括弧は、ほとんどの試みで台無しになります。括弧が正しく表示されるようにするには、何をする必要がありますか?
  • どのようなソリューションでも pdflatex ではなく xelatex を使用できますか?

答え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

各ツールチェーンは、LaTeX エンジン、ドキュメント クラス、および使用されるパッケージに応じて、さまざまな状況で失敗します。

tex_program -> svg_converter -> svg_cropサブプロセスを実行するための Python 呼び出しで使用できるコマンド パラメータのリストとして、ツールチェーンを構築する関数の現在の (Python/Linux) バージョンを追加しています。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)

関連情報