aboutsummaryrefslogtreecommitdiff
path: root/doc/todo/Move_teximg_latex_preamble_to_config_file.mdwn
blob: 3cedd5ae31e30fcb45975b056afcf824811006ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
The [[plugins/teximg]] plugin currently has a TODO in the source code to make the preamble configurable.  The included [[patch]] makes this change.

The patch also makes some other changes:

  - The default latex preamble is changed to the international standard `article` class from the European `scrartcl` class.
  - Removed the non-standard `mhchem` package from the default preamble.
  - Allow the use of `dvipng` rather than `dvips` and `convert` (`convert` is not a standard part of a latex install).  This is configurable.

-- [[Will]]

> I like making this configurable. I do fear that changing what's included
> by default could break some existing uses of teximg? That needs to be
> considered, and either the breakage documented in NEWS, or avoided. Also,
> if mchem is dropped, I think the suggests on texlive-science in
> debian/control should probably go? --[[Joey]]

>> Yes, changing the defaults could break some existing uses.  I think in
>> this case, documenting in NEWS and dropping texlive-science is the
>> best option.  In fact, NEWS should probably document the config
>> setting to return things to how they were.
>>
>> The reason I prefer dropping `mchem` rather than keeping it is that `mchem`
>> is non-standard.  Now that things are configurable and it is easy to
>> add in if you want it, having only standard packages by default is a
>> good thing.  Here is a proposed NEWS entry:

File: TexImg standard preamble changed

The [[plugins/teximg]] [[plugin]] now has a configurable LaTeX preamble.
As part of this change the `mchem` LaTeX package has been removed from
the default LaTeX preamble as it wasn't included in many TeX installations.

The previous behaviour can be restored by adding the following to your ikiwiki setup:

    	teximg_prefix => '\documentclass{scrartcl}
    		\usepackage[version=3]{mhchem}
    		\usepackage{amsmath}
    		\usepackage{amsfonts}
    		\usepackage{amssymb}
    		\pagestyle{empty}
    		\begin{document}',

In addition, the rendering mechanism has been changed to use `dvipng` by default.
If you would like to return to the old rendering mechanism using `dvips` and `convert`
then you should add the following line to your ikiwiki setup:

    	teximg_dvipng => 0,

The LaTeX postfix is unchanged, but is also now configurable using `teximg_postfix`.
Happy TeXing.

>> I think that about covers it.  -- [[Will]]

    diff --git a/IkiWiki/Plugin/teximg.pm b/IkiWiki/Plugin/teximg.pm
    index 369c108..8c3379f 100644
    --- a/IkiWiki/Plugin/teximg.pm
    +++ b/IkiWiki/Plugin/teximg.pm
    @@ -10,6 +10,18 @@ use File::Temp qw(tempdir);
     use HTML::Entities;
     use IkiWiki 2.00;
     
    +my $default_prefix = <<EOPREFIX
    +\\documentclass{article}
    +\\usepackage{amsmath}
    +\\usepackage{amsfonts}
    +\\usepackage{amssymb}
    +\\pagestyle{empty}
    +\\begin{document}
    +EOPREFIX
    +;
    +
    +my $default_postfix = '\\end{document}';
    +
     sub import {
     	hook(type => "getsetup", id => "teximg", call => \&getsetup);
     	hook(type => "preprocess", id => "teximg", call => \&preprocess);
    @@ -21,6 +33,26 @@ sub getsetup () {
     			safe => 1,
     			rebuild => undef,
     		},
    +		teximg_dvipng => {
    +			type => "boolean",
    +			description => "Should teximg use dvipng to render, or dvips and convert?",
    +			safe => 0,
    +			rebuild => 0,
    +		},
    +		teximg_prefix => {
    +			type => "string",
    +			example => $default_prefix,
    +			description => "LaTeX prefix for teximg plugin",
    +			safe => 0, # Not sure how secure LaTeX is...
    +			rebuild => 1,
    +		},
    +		teximg_postfix => {
    +			type => "string",
    +			example => $default_postfix,
    +			description => "LaTeX postfix for teximg plugin",
    +			safe => 0, # Not sure how secure LaTeX is...
    +			rebuild => 1,
    +		},
     }
     
     sub preprocess (@) {
    @@ -105,25 +137,35 @@ sub gen_image ($$$$) {
     	my $digest = shift;
     	my $imagedir = shift;
     
    -	#TODO This should move into the setup file.
    -	my $tex = '\documentclass['.$height.'pt]{scrartcl}';
    -	$tex .= '\usepackage[version=3]{mhchem}';
    -	$tex .= '\usepackage{amsmath}';
    -	$tex .= '\usepackage{amsfonts}';
    -	$tex .= '\usepackage{amssymb}';
    -	$tex .= '\pagestyle{empty}';
    -	$tex .= '\begin{document}';
    +	if (!defined $config{teximg_prefix}) {
    +		$config{teximg_prefix} = $default_prefix;
    +	}
    +	if (!defined $config{teximg_postfix}) {
    +		$config{teximg_postfix} = $default_postfix;
    +	}
    +	if (!defined $config{teximg_dvipng}) {
    +		# TODO: Can we detect whether dvipng or convert is in the path?
    +		$config{teximg_dvipng} = 1;
    +	}
    +	
    +	my $tex = $config{teximg_prefix};
     	$tex .= '$$'.$code.'$$';
    -	$tex .= '\end{document}';
    +	$tex .= $config{teximg_postfix};
    +	$tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
    +	$tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
     
     	my $tmp = eval { create_tmp_dir($digest) };
     	if (! $@ &&
    -	    writefile("$digest.tex", $tmp, $tex) &&
    -	    system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
    -	    system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
    -	    # ensure destination directory exists
    -	    writefile("$imagedir/$digest.png", $config{destdir}, "") &&
    -	    system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
    +		writefile("$digest.tex", $tmp, $tex) &&
    +		system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
    +		# ensure destination directory exists
    +		writefile("$imagedir/$digest.png", $config{destdir}, "") &&
    +		(($config{teximg_dvipng} &&
    +			system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
    +			) || 
    +		(!$config{teximg_dvipng} &&
    +			system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
    +			system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0))) {
     		return 1;
     	}
     	else {

[[done]]