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 diff --git a/IkiWiki/Plugin/teximg.pm b/IkiWiki/Plugin/teximg.pm
12 index 369c108..8c3379f 100644
13 --- a/IkiWiki/Plugin/teximg.pm
14 +++ b/IkiWiki/Plugin/teximg.pm
15 @@ -10,6 +10,18 @@ use File::Temp qw(tempdir);
19 +my $default_prefix = <<EOPREFIX
20 +\\documentclass{article}
21 +\\usepackage{amsmath}
22 +\\usepackage{amsfonts}
23 +\\usepackage{amssymb}
29 +my $default_postfix = '\\end{document}';
32 hook(type => "getsetup", id => "teximg", call => \&getsetup);
33 hook(type => "preprocess", id => "teximg", call => \&preprocess);
34 @@ -21,6 +33,26 @@ sub getsetup () { #{{{
40 + description => "Should teximg use dvipng to render, or dvips and convert?",
46 + example => $default_prefix,
47 + description => "LaTeX prefix for teximg plugin",
48 + safe => 0, # Not sure how secure LaTeX is...
53 + example => $default_postfix,
54 + description => "LaTeX postfix for teximg plugin",
55 + safe => 0, # Not sure how secure LaTeX is...
60 sub preprocess (@) { #{{{
61 @@ -105,25 +137,34 @@ sub gen_image ($$$$) { #{{{
65 - #TODO This should move into the setup file.
66 - my $tex = '\documentclass['.$height.'pt]{scrartcl}';
67 - $tex .= '\usepackage[version=3]{mhchem}';
68 - $tex .= '\usepackage{amsmath}';
69 - $tex .= '\usepackage{amsfonts}';
70 - $tex .= '\usepackage{amssymb}';
71 - $tex .= '\pagestyle{empty}';
72 - $tex .= '\begin{document}';
73 + if (!defined $config{teximg_prefix}) {
74 + $config{teximg_prefix} = $default_prefix;
76 + if (!defined $config{teximg_postfix}) {
77 + $config{teximg_postfix} = $default_postfix;
79 + if (!defined $config{teximg_dvipng}) {
80 + # TODO: Can we detect whether dvipng or convert is in the path?
81 + $config{teximg_dvipng} = 1;
84 + my $tex = $config{teximg_prefix};
85 $tex .= '$$'.$code.'$$';
86 - $tex .= '\end{document}';
87 + $tex .= $config{teximg_postfix};
88 + $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
90 my $tmp = eval { create_tmp_dir($digest) };
92 - writefile("$digest.tex", $tmp, $tex) &&
93 - system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
94 - system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
95 - # ensure destination directory exists
96 - writefile("$imagedir/$digest.png", $config{destdir}, "") &&
97 - system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
98 + writefile("$digest.tex", $tmp, $tex) &&
99 + system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
100 + # ensure destination directory exists
101 + writefile("$imagedir/$digest.png", $config{destdir}, "") &&
102 + (($config{teximg_dvipng} &&
103 + system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
105 + (!$config{teximg_dvipng} &&
106 + system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
107 + system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0))) {