response
[ikiwiki] / doc / todo / Move_teximg_latex_preamble_to_config_file.mdwn
1 The [[plugins/teximg]] plugin currently has a TODO in the source code to make the preamble configurable.  The included [[patch]] makes this change.
2
3 The patch also makes some other changes:
4
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.
8
9 -- [[Will]]
10
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]]
16
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);
22      use HTML::Entities;
23      use IkiWiki 2.00;
24      
25     +my $default_prefix = <<EOPREFIX
26     +\\documentclass{article}
27     +\\usepackage{amsmath}
28     +\\usepackage{amsfonts}
29     +\\usepackage{amssymb}
30     +\\pagestyle{empty}
31     +\\begin{document}
32     +EOPREFIX
33     +;
34     +
35     +my $default_postfix = '\\end{document}';
36     +
37      sub import { #{{{
38         hook(type => "getsetup", id => "teximg", call => \&getsetup);
39         hook(type => "preprocess", id => "teximg", call => \&preprocess);
40     @@ -21,6 +33,26 @@ sub getsetup () { #{{{
41                         safe => 1,
42                         rebuild => undef,
43                 },
44     +           teximg_dvipng => {
45     +                   type => "boolean",
46     +                   description => "Should teximg use dvipng to render, or dvips and convert?",
47     +                   safe => 0,
48     +                   rebuild => 0,
49     +           },
50     +           teximg_prefix => {
51     +                   type => "string",
52     +                   example => $default_prefix,
53     +                   description => "LaTeX prefix for teximg plugin",
54     +                   safe => 0, # Not sure how secure LaTeX is...
55     +                   rebuild => 1,
56     +           },
57     +           teximg_postfix => {
58     +                   type => "string",
59     +                   example => $default_postfix,
60     +                   description => "LaTeX postfix for teximg plugin",
61     +                   safe => 0, # Not sure how secure LaTeX is...
62     +                   rebuild => 1,
63     +           },
64      } #}}}
65      
66      sub preprocess (@) { #{{{
67     @@ -105,25 +137,34 @@ sub gen_image ($$$$) { #{{{
68         my $digest = shift;
69         my $imagedir = shift;
70      
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;
81     +   }
82     +   if (!defined $config{teximg_postfix}) {
83     +           $config{teximg_postfix} = $default_postfix;
84     +   }
85     +   if (!defined $config{teximg_dvipng}) {
86     +           # TODO: Can we detect whether dvipng or convert is in the path?
87     +           $config{teximg_dvipng} = 1;
88     +   }
89     +   
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;
95      
96         my $tmp = eval { create_tmp_dir($digest) };
97         if (! $@ &&
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
110     +                   ) || 
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))) {
114                 return 1;
115         }
116         else {