eqnarray 数学转 svg

eqnarray 数学转 svg

我想编写一个 perl 处理器,从 html 图像中过滤方程式并为其创建 svg 替换,以便我的方程式可以在 epub 和主流浏览器中很好地显示。(我知道文本浏览器和电子邮件客户端会很常见。)

思考

<html>
<body>
   <h1>Visible</h1>
   <p>I can see this and change its size in my browser and in epub.
   <pre class="math">
      y &=& \frac{\sqrt{x}}{z} \\
         &&+\frac{\sqrt{z}}{\mathtt{myvar}} \nonumber
   </pre>
</body>
</html>

我可以轻松解析预内容。然后我可以将它们粘贴到 latex 文件中。

\documentclass[12pt]{article}

\RequirePackage{charter} 
\RequirePackage[mdbch,ttscaled=true]{mathdesign} 
\usepackage{microtype}
\usepackage[english]{babel}

\begin{document}
\begin{eqnarray}
    y &=& \frac{\sqrt{x}}{z} \\
       && + \frac{\sqrt{z}}{\mathtt{myvar}}
\end{eqnarray}

\end{document}

大概,然后我跑

 pdflatex -output dvi file.tex ; dvisvgm file.dvi .

我使用 dvisvgm 的原因是 texlive 不包含 pdfsvgm,而 pdf2svg 让我害怕,因为它引用了 poppler 和 cairo(我希望我的方程式保持完全呈现/可缩放)。但是,我确实需要运行的结果文档

1. 我如何让 Latex 创建一个仅包含方程式边界框的页面?

2. 是否有任何推荐的其他参数我应该交给 dvisvgm 以实现我的目的?(例如,精确?)。

此后,我想将其编码回我的 html 源代码中。大概,现在我想做类似的事情

<html>
<body>
   <h1>Visible</h1>
   <p>I can see this and change its size in my browser and in epub.
   <svg>
      ... (whatever I see in file.svg)
   </svg>
</body>
</html>

3. 这是解决问题的正确方法吗?还是我在这里犯了一个错误?

感谢帮助。

问候,

/iaw

PS:如果有人读到这篇文章,链接位于http://www.latex2html.org/node2.html好像坏了。这里的日期是 2001 年的。

答案1

eqnarray 似乎不太容易实现,但正规方程可能可以,并且数组环境可以包含多行方程。下面是一个例子。从

\documentclass[border={5pt 5pt 5pt 5pt}]{standalone}
  % left bottom right top .  warning: does not understand ex or em

\RequirePackage[T1]{fontenc}
\RequirePackage{textcomp}
\RequirePackage{charter}
\RequirePackage[mdbch,ttscaled=true]{mathdesign}
\usepackage{microtype}

\usepackage[english]{babel}

\pagestyle{empty}

\begin{document}

  \LARGE

  $ \begin{array}{cccc}
     y &=& \frac{\sqrt{x}}{z}\\
       && + \frac{\sqrt{z}}{x} \end{array} $

\end{document}

然后

$ latex test.tex
$ dvisvgm -n test.dvi

将创建一个 svg 文件。如果没有 -n,质量会很差。有了它,质量就很好了。(谢谢,马丁)。该数组是一个数学模式 eqnarray 替代品,非常可行 - 并且可以对齐两个以上的数组。

该示例没有方程式行编号。据推测,可以使用 float:right 在编号上轻松做到这一点。

此示例生成一个大约 4KB 的 svg 文件。即使有 100 个方程式,我们仍然会看到一个比包含 mathjax 的文件小一个数量级的 html 文件。它也应该可以在 epub 中使用,尽管我还没有测试过。如果 mathjax 已经被浏览器加载和缓存,则会很有优势。

我附上了一个基本的 perl 技巧,它将依赖于 mathjax 的 html 文件转换为不依赖于 mathjax 的 html 文件。它存在基本问题,可能还有其他一些错误。但这是一个起点。

#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings FATAL => qw{ uninitialized };

=pod

=head1 Title

  htmltexmath2svg.pl --- replaces mathjax-style eqns in html files with inline svg images
    (to remove dependency on mathjax)

=head1 Requirements

  texlive or equivalent (with latex and dvisvgm)

  Perl6::slurp

  Unix type OS (OSX or linux)


=head1 Output

  the output will be a file with -svg before the file extension,
  where all mathjax equations have been replaced with svg.

  you should define a span class of mathsvg and mathsvgblock for \(..\) and \[..\]

  the basic creation file is attached in the __DATA__ section and can be fixed up.


=head1 Bugs

  this program still has baseline alignment problems.

  this is a hack.  probably many bugs.  as I use this program, I will
  probably update and fix various issues.  It has worked on some simple
  sample files.  parsing could be more intelligent.

  we overwrite temp.* files every time, and do not clean up after ourselves.

  the program is slow, but one should run it only rarely anyway.

=head1 Versions

  0.0: Sat Jan 18 15:32:02 2014, Ivo Welch.  no copyright claim.  free further distribution.

=cut

################################################################

use Perl6::Slurp;  ## only dependency

################################################################

my $usage= "usage: $0 filename.*ml ... creates filename-svg.*ml";

(@ARGV) or die "$usage\n";
(-e $ARGV[0]) or die "$0: file $_[0] does not exist\n";
($ARGV[0] =~ /ml$/) or die "$0: file $_[0] must end with file extension *ml\n";
($ARGV[0] =~ /-svg\.$/) and die "$0: file $_[0] seems to already contain -svg.\n";
(my $ofname= $ARGV[0]) =~ s/(.*)\.(.*)/$1-svg.$2/;
(defined($ofname)) or die "$0: cannot parse output file anem!\n";

my $textemplate= slurp \*DATA;

my $file_contents = slurp $ARGV[0];

($file_contents =~ /mathjax\.org/) and warn "\n\n$0: your file still contains the string mathjax.org, which should no longer be necessary. consider deleting it.\n\n";

$file_contents =~ s/\<span class=\"math\"(\>.*?)<\/span\>/runme($1)/gmse;  ## good enough parsing

open my $FOUT, ">", $ofname or die "cannot open '$ofname' for write: $!\n";
print $FOUT $file_contents;
close $FOUT;

print "$0: successfully converted $ARGV[0] to $ofname\n";


################################################################
sub runme {
  (my $convert=$_[0]) =~ s/^\>//;  ## we are going to extend the tag with an alt, so we needed to remove it

  $convert=~ s/\&amp;/\&/g;  $convert=~ s/\&lt;/\</g; $convert=~ s/\&gt;/\>/g;  ## undo previous html mangling.  coult be more unmangling;
  $convert =~ s/^\s+//gms; $convert =~ s/\s+$//gms;  ## remove leading and trailing spaces

  print STDERR "[convert '$convert' ";

  my $classname;
  if ($convert =~ s/^\\\(//) {
    $convert =~ s/\\\)$//;
    $classname="mathsvg";
  } elsif ($convert =~ s/^\\\[//) {
    $convert =~ s/\\\]$//;
    $classname="mathsvgblock";
  } else {
    die "$0: I do not understand equation type in '$convert'\n";
  }

  open(my $FOUT, ">", "temp.tex") or die "$0: I cannot open temp.tex for writing: $!\n";

  my $texfile= $textemplate;
  $texfile=~ s/\*\*\*\*MATHJAXHERE\*\*\*\*/$convert/;
  print $FOUT $texfile;
  close($FOUT);

  system("rm -f temp.dvi temp.svg ; latex -halt-on-error temp.tex &> /dev/null ; dvisvgm -n temp.dvi &> /dev/null");

  my $svgcontents= (-e "temp.svg") ? slurp "temp.svg" : "svg conversion failed";
  $svgcontents =~ s/<!DOCTYPE .*?\>//;  ## we are inlining this one, so no doctype
  ($svgcontents =~ /<\/svg>/) or $svgcontents="svg conversion failed (incomplete)";

  print STDERR (($svgcontents =~ /svg conversion failed/) ? "failed" : "ok")."\n";

  return "<span class=\"$classname\" alt=\"$convert\">$svgcontents\<\/span>";
}



__DATA__

\documentclass[border={1pt 1pt 1pt 1pt}]{standalone}
%%%% left bottom right top .  standalone does not understand ex or em!

\RequirePackage[T1]{fontenc}
\RequirePackage{textcomp}
\RequirePackage{charter} 
\RequirePackage[mdbch,ttscaled=true]{mathdesign} 
\usepackage{microtype}

\usepackage[english]{babel}

\pagestyle{empty}

\begin{document}

$****MATHJAXHERE****$

\end{document}
);

相关内容