1

I am looking for a reliable way to convert a pdf from a latex file to SVG for display in a browser

\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. I had used \documentclass{standalone} which did not work I ended up with removing the pagenumber and using the following ugly 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

Questions

  • Is there a simpler way to create the svg so that the image is cropped to the actual extent? (the -D option for inkscape)
  • Is there a simpler way to create the pdf so that it only displays that image, rather than the whole page?
  • The parentheses enclosing the matrix get messed up with most of my attempts. What would I need to do to ensure that parens will be correctly rendered
  • can whatever solution use xelatex rather than pdflatex?
ea42_gh
  • 83

3 Answers3

2

Instead of pdf2svg, which is unmaintained, I would use dvisvgm which is part of TeXLive/MiKTeX and actively maintained.

The combination of --exact and --zoom=-1 trims the output to the visible content and makes it responsive which facilitates embedding into HTML (options explained here).

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}

AlexG
  • 54,894
0

Just add the missing 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}

Then compile and convert from PDF to SVG as you suggested.

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

Each of the toolchains fails under diverse circumstances, depending on the latex engine, the document class and the packages used.

I am adding a current (python/linux) version of a function building the toolchain tex_program -> svg_converter -> svg_crop as a a list of command parameters that can be used in a python call to run a subprocess. Not the nexec parameter is the number of times tex_program will be invoked. It is not applicable for all combinations...

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

To invoke the programs, I use code along the lines of the following

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)

Marijn
  • 37,699
ea42_gh
  • 83