]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/Move_teximg_latex_preamble_to_config_file.mdwn
(no commit message)
[ikiwiki.git] / 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     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);
16      use HTML::Entities;
17      use IkiWiki 2.00;
18      
19     +my $default_prefix = <<EOPREFIX
20     +\\documentclass{article}
21     +\\usepackage{amsmath}
22     +\\usepackage{amsfonts}
23     +\\usepackage{amssymb}
24     +\\pagestyle{empty}
25     +\\begin{document}
26     +EOPREFIX
27     +;
28     +
29     +my $default_postfix = '\\end{document}';
30     +
31      sub import { #{{{
32         hook(type => "getsetup", id => "teximg", call => \&getsetup);
33         hook(type => "preprocess", id => "teximg", call => \&preprocess);
34     @@ -21,6 +33,26 @@ sub getsetup () { #{{{
35                         safe => 1,
36                         rebuild => undef,
37                 },
38     +           teximg_dvipng => {
39     +                   type => "boolean",
40     +                   description => "Should teximg use dvipng to render, or dvips and convert?",
41     +                   safe => 0,
42     +                   rebuild => 0,
43     +           },
44     +           teximg_prefix => {
45     +                   type => "string",
46     +                   example => $default_prefix,
47     +                   description => "LaTeX prefix for teximg plugin",
48     +                   safe => 0, # Not sure how secure LaTeX is...
49     +                   rebuild => 1,
50     +           },
51     +           teximg_postfix => {
52     +                   type => "string",
53     +                   example => $default_postfix,
54     +                   description => "LaTeX postfix for teximg plugin",
55     +                   safe => 0, # Not sure how secure LaTeX is...
56     +                   rebuild => 1,
57     +           },
58      } #}}}
59      
60      sub preprocess (@) { #{{{
61     @@ -105,25 +137,34 @@ sub gen_image ($$$$) { #{{{
62         my $digest = shift;
63         my $imagedir = shift;
64      
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;
75     +   }
76     +   if (!defined $config{teximg_postfix}) {
77     +           $config{teximg_postfix} = $default_postfix;
78     +   }
79     +   if (!defined $config{teximg_dvipng}) {
80     +           # TODO: Can we detect whether dvipng or convert is in the path?
81     +           $config{teximg_dvipng} = 1;
82     +   }
83     +   
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;
89      
90         my $tmp = eval { create_tmp_dir($digest) };
91         if (! $@ &&
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
104     +                   ) || 
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))) {
108                 return 1;
109         }
110         else {