1 The [[plugins/teximg]] plugin currently has a TODO in the source code to make the preamble configurable. The included [[patch]] makes this change.
3 The patch also makes some other changes:
5 - The default latex preamble is changed to the international standard `article` class from the European `scrartcl` class.
6 - Removed the non-standard `mhchem` package from the default preamble.
7 - Allow the use of `dvipng` rather than `dvips` and `convert` (`convert` is not a standard part of a latex install). This is configurable.
11 > I like making this configurable. I do fear that changing what's included
12 > by default could break some existing uses of teximg? That needs to be
13 > considered, and either the breakage documented in NEWS, or avoided. Also,
14 > if mchem is dropped, I think the suggests on texlive-science in
15 > debian/control should probably go? --[[Joey]]
17 diff --git a/IkiWiki/Plugin/teximg.pm b/IkiWiki/Plugin/teximg.pm
18 index 369c108..8c3379f 100644
19 --- a/IkiWiki/Plugin/teximg.pm
20 +++ b/IkiWiki/Plugin/teximg.pm
21 @@ -10,6 +10,18 @@ use File::Temp qw(tempdir);
25 +my $default_prefix = <<EOPREFIX
26 +\\documentclass{article}
27 +\\usepackage{amsmath}
28 +\\usepackage{amsfonts}
29 +\\usepackage{amssymb}
35 +my $default_postfix = '\\end{document}';
38 hook(type => "getsetup", id => "teximg", call => \&getsetup);
39 hook(type => "preprocess", id => "teximg", call => \&preprocess);
40 @@ -21,6 +33,26 @@ sub getsetup () { #{{{
46 + description => "Should teximg use dvipng to render, or dvips and convert?",
52 + example => $default_prefix,
53 + description => "LaTeX prefix for teximg plugin",
54 + safe => 0, # Not sure how secure LaTeX is...
59 + example => $default_postfix,
60 + description => "LaTeX postfix for teximg plugin",
61 + safe => 0, # Not sure how secure LaTeX is...
66 sub preprocess (@) { #{{{
67 @@ -105,25 +137,34 @@ sub gen_image ($$$$) { #{{{
71 - #TODO This should move into the setup file.
72 - my $tex = '\documentclass['.$height.'pt]{scrartcl}';
73 - $tex .= '\usepackage[version=3]{mhchem}';
74 - $tex .= '\usepackage{amsmath}';
75 - $tex .= '\usepackage{amsfonts}';
76 - $tex .= '\usepackage{amssymb}';
77 - $tex .= '\pagestyle{empty}';
78 - $tex .= '\begin{document}';
79 + if (!defined $config{teximg_prefix}) {
80 + $config{teximg_prefix} = $default_prefix;
82 + if (!defined $config{teximg_postfix}) {
83 + $config{teximg_postfix} = $default_postfix;
85 + if (!defined $config{teximg_dvipng}) {
86 + # TODO: Can we detect whether dvipng or convert is in the path?
87 + $config{teximg_dvipng} = 1;
90 + my $tex = $config{teximg_prefix};
91 $tex .= '$$'.$code.'$$';
92 - $tex .= '\end{document}';
93 + $tex .= $config{teximg_postfix};
94 + $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
96 my $tmp = eval { create_tmp_dir($digest) };
98 - writefile("$digest.tex", $tmp, $tex) &&
99 - system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
100 - system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
101 - # ensure destination directory exists
102 - writefile("$imagedir/$digest.png", $config{destdir}, "") &&
103 - system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
104 + writefile("$digest.tex", $tmp, $tex) &&
105 + system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
106 + # ensure destination directory exists
107 + writefile("$imagedir/$digest.png", $config{destdir}, "") &&
108 + (($config{teximg_dvipng} &&
109 + system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
111 + (!$config{teximg_dvipng} &&
112 + system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
113 + system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0))) {