-
Notifications
You must be signed in to change notification settings - Fork 0
/
takahashi.pl
521 lines (453 loc) · 15.3 KB
/
takahashi.pl
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/perl
=pod
Perl application to turn a simple text definition file into a latex beamer presentation.
As this is intended for the so-called Takahashi style of presentation which consists of
a few or even only one word (or perhaps an image or an equation) per slide,
the text definition file is also very simple in structure.
An example:
##################
Presentation
Takahashi method
perl script
Test
.image
osskilogo.pdf
.
Gnuplot test
.gnuplot
set logs
plot x
.
# Comment
Equation test
For example $5+5=10$
\[\sin(2x)=2\sin x \cos x\]
\TeX test
.tex
\begin{tabular}{ l | c || r | }
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
.
Test end
#####################
Comment lines begin with '#', they will be ignored.
Normal text in a line will be typeset in a line of their own in the presentation as well.
Font sizes are chosen so that the text fills up the viewing area.
Check the output and reword if you don't like the default layout.
Alternatively, manual font sizes are possible with the character '@'.
If a normal line is terminated by the '@' char and an integer,
then that integer will be substituted for the font size (in points) for that line.
If the line is terminated with '@-' then the line will be typeset with the font size of the preceding line.
An empty line signifies a new slide.
The dot character (.) denotes special mode, which can be one of the following:
image, gnuplot and tex.
This is easily the worst piece of code I've ever written - and that is saying something, mind you.
=cut
use strict;
use warnings;
use utf8;
use Data::Dumper;
use File::Basename;
$|++;
my @slides;
my @current_slide;
my @slide_options;
my %layout;
$layout{scale_a} = 16;
$layout{scale_b} = 40;
$layout{scale_text} = 0.9;
$layout{plus_margin} = 0;
$layout{idiocy_factor} = 1.7;
$layout{default_fontsize} = 30;
my $delete_junk = 1;
my $delete_even_more_junk = 0;
my $redo_graphs = 1;
my $gnuplot_path = "/home/kikuchiyo/gnuplot43";
if (not -e $gnuplot_path) {
$gnuplot_path = "gnuplot";
}
my %modes = (image => 'image', 'gnuplot*' => 'script', gnuplot => 'script', tex => 'line');
my @extlist = qw/ .txt .lst .dat .src /;
my $globalheader = <<'HEADER';
\documentclass[utf8x]{beamer}
\makeatletter
\insc@unt=60000
\makeatother
\usepackage{lmodern}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\def\magyarOptions{defaults=hu-min}
\usepackage[magyar]{babel}
%\usefonttheme[onlymath]{serif}
\usepackage{tikz}
%\usetikzlibrary{arrows,snakes,shapes}
%\usepackage[usenames,dvipsnames,x11names]{xcolor}
\setbeamertemplate{navigation symbols}{}
\setbeamercolor{whitetext}{fg=white}
\begin{document}
\setbeamerfont{text}{family=\sffamily}
HEADER
binmode STDOUT, ":encoding(UTF-8)";
binmode STDIN, ":encoding(UTF-8)";
die "Usage: [perl] takahashi.pl [options] file\n" unless @ARGV;
my $infn_orig = $ARGV[0];
open(my $infh, '<:encoding(UTF-8)', $infn_orig) || die "Error opening input\n";
while (<$infh>) {
chomp;
if (/^\./) { # start special mode
my ($type_s) = /^\.(.*)/;
my $type;
my $script;
if ($type_s =~ /^gnuplot\*/) {
$type = 'gnuplot*';
} elsif ($type_s =~ /^gn?u?p?l?o?t?/) {
$type = 'gnuplot';
} elsif ($type_s =~ /^im?a?g?e?/) {
$type = 'image';
} elsif ($type_s =~ /^te?x?/) {
$type = 'tex';
} elsif ($type_s =~ /^se?t?/) {
$type = 'none';
my (undef, $opt, @optval) = split /\s+/, $type_s;
if ($opt =~ /background/) {
$slide_options[@slides]{background} = join " ", @optval;
}
} else {
$type = 'none';
}
if ($type eq 'none') {
# do nothing, exit special mode (no terminating .)
}
elsif ($type eq 'image') {
my $ifn = <$infh>;
chomp $ifn;
my $scale = <$infh>;
chomp $scale;
if ($scale =~ /^\.$/) {
push @current_slide, {type => $type, $modes{$type} => $ifn};
} else {
push @current_slide, {type => $type, $modes{$type} => $ifn, scale => $scale};
$scale = <$infh>;
}
} else {
while (<$infh>) {
if (/^\./) {
last;
} else {
$script .= $_;
}
}
push @current_slide, {type => $type, $modes{$type} => $script} unless $type eq 'none';
}
} elsif (/^#/) { # comment line
# ignore it
} elsif (!$_) { # empty line: new slide
push @slides, [ @current_slide ] if @current_slide;
$slides[-1][-1]{type} = 'tex' if $slides[-1][-1]{type} eq 'normal';
#print Dumper \@current_slide;
@current_slide = ();
} elsif (/\\\[|\\\]|\$/) { # line containing TeX markup
my $type = 'normal';
my $dollarcnt =()= /\$/sg;
$type = 'normal' unless $dollarcnt % 2;
$type = 'tex' if /\\begin|\\end/;
my $brace_open =()= /\\\[\{/sg; my $brace_close =()= /\\\]/sg;
$type = 'tex' if $brace_open != $brace_close;
$brace_open =()= /\{/sg; $brace_close =()= /\}/sg;
$type = 'tex' if $brace_open != $brace_close;
if (/\s\@(?:\d+|-)\s*$/) {
#print;
my ($forcedsize) = /\s\@(\d+|-)\s*$/;
s/\s\@(?:\d+|-)\s*$//;
push @current_slide, {type => 'normal', line => $_, forcedsize => $forcedsize};
} else {
push @current_slide, {type => $type, line => $_};
}
#push @current_slide, {type => $type, line => $_};
} else { # normal line: append it to current slide
if (/\s\@(?:\d+|-)\s*$/) {
#print;
my ($forcedsize) = /\s\@(\d+|-)\s*$/;
s/\s\@(?:\d+|-)\s*$//;
push @current_slide, {type => 'normal', line => $_, forcedsize => $forcedsize};
} else {
push @current_slide, {type => 'normal', line => $_};
}
}
}
close $infh;
#print Dumper \@slides, \@slide_options;
# get output filename from ARGV, change to tex, open file
my ($infn, $path, $ext) = fileparse($infn_orig, @extlist);
my $tempfnbase = $infn."!temp";
my $tempfn = $tempfnbase.".tex";
my ($ifh, $ofh);
my $sr = latex(\@slides, 1);
open($ofh, '>:encoding(UTF-8)', $tempfn) || die "Error opening temporary file\n";
print $ofh $$sr;
close $ofh;
system('pdflatex', "-interaction=batchmode", $tempfn);
open($ifh, '<:encoding(UTF-8)', $tempfnbase.".log") || die "Error reading log\n";
my $logfile = do { local $/; <$ifh> };
close $ifh;
#print Dumper \@slides;
#print '!';
parse_log(\@slides, \$logfile);
#print Dumper \@slides;
$sr = latex(\@slides, 2);
my $ofn = $infn.".tex";
open($ofh, '>:encoding(UTF-8)', $ofn) || die "Error opening temporary file\n";
print $ofh $$sr;
close $ofh;
system('pdflatex', "-interaction=batchmode", $ofn);
system('pdflatex', "-interaction=batchmode", $ofn);
if ($delete_junk) {
unlink <*.toc>; unlink <*.aux>; unlink <*.nav>; unlink <*.snm>; unlink <*.out>;
}
if ($delete_even_more_junk) {
unlink <*.plt>; unlink <*.log>; unlink <*!temp.*>; unlink <*!*.pdf>;
}
####################################################x
sub parse_log {
my $slides = shift;
my $logfile = shift;
my $counter = 0;
#print $$logfile;
my ($textwidth) = $$logfile =~ /^\> (\d+\.\d+)pt\.\nl\.\d+ \\showthe\s*\\textwidth/ms;
my ($textheight) = $$logfile =~ /^\> (\d+\.\d+)pt\.\nl\.\d+ \\showthe\s*\\textheight/ms;
#print "$textwidth, $textheight\n";
foreach my $slide (@$slides) {
my $maxwidth = 0;
my $totalheight = 0;
my ($xscale, $yscale, $scale);
foreach my $line ( @$slide ){
my $code = $line->{numeral};
#print $line->{numeral};
($line->{width}) = ($$logfile =~ /^\> (\d+\.\d+)pt.{30,40}?\\showthe\s*\\wz$code/ms);
($line->{height}) = ($$logfile =~ /^\> (\d+\.\d+)pt.{30,40}?\\showthe\s*\\hz$code/ms);
#print "$code, $line->{width}, $line->{height}, $-[0] \n";
$maxwidth = $line->{width} if $line->{width} > $maxwidth;
$totalheight += stupid_scaling_func($line->{height});
}
if ($maxwidth == 0 or $totalheight == 0) {
$scale = 1;
my $errortext = substr($slide->[0]{ $modes{ $slide->[0]{type} } }, 0, 30);
$errortext =~ s/\n/ /msg;
print STDERR "!!! PDFLaTeX shat itself at code $slide->[0]{numeral}: \"$errortext...\"\n";
} else {
$xscale = ($textwidth - 2 * $layout{plus_margin}) / $maxwidth;
$yscale = ($textheight - 2 * $layout{plus_margin}) / ($totalheight * $layout{idiocy_factor});
$scale = ($xscale<$yscale) ? $xscale : $yscale;
}
unshift @$slide, {fontsize => int($layout{default_fontsize}*$scale)};
}
}
sub latex {
my $slides = shift;
my $pass = shift || 2;
my $latex = $globalheader;
if ($pass == 1) {
$latex .= '\showthe\textwidth'."\n";
$latex .= '\showthe\textheight'."\n";
#$latex .= '\newlength{\tw}'."\n";
#$latex .= '\newlength{\thei}'."\n";
#$latex .= '\newsavebox{\mybox}'."\n";
#$latex .= '\newcommand{\mybox}{}'."\n";
}
my $counter = 0;
my $saved_fontsize;
foreach my $i (0..$#$slides) {
my $slide = $slides->[$i];
if (exists $slide_options[$i]{background}) {
$latex .= '\setbeamercolor{normal text}{bg=' . $slide_options[$i]{background} . "}\n";
}
$latex .= '\begin{frame}'."\n";
$latex .= '\begin{center}'."\n";
my $fontsize;
if (exists $slide->[0]{fontsize}) {
$fontsize = $slide->[0]{fontsize};
shift @$slide;
} else {
$fontsize = $layout{default_fontsize};
}
$latex .= '\fontsize{'.$fontsize.'}{'.stupid_scaling_func($fontsize).'}\selectfont ' ;#."\n";
#$latex .= '\sbox{\mybox}{%'."\n" if $pass == 1;
#$latex .= '\newcommand{\mybox'.numeral($counter).'}{%'."\n" if $pass == 1;
#print Dumper $slide;
foreach my $line ( @$slide ){
#print $line->{forcedsize} if exists $line->{forcedsize};
if ($line->{type} eq "normal") {
if ($pass == 2) {
if (exists $line->{forcedsize}) {
if ($line->{forcedsize} eq '-') {
$latex .= '\fontsize{'.$saved_fontsize.'}{'.
stupid_scaling_func($saved_fontsize).'}\selectfont ' ;#."\n";
$latex .= $line->{line}."\\\\\n";
$latex .= '\fontsize{'.$fontsize.'}{'.
stupid_scaling_func($fontsize).'}\selectfont'."\n" unless $line == $slide->[-1];
} else {
$latex .= '\fontsize{'.$line->{forcedsize}.'}{'.
stupid_scaling_func($line->{forcedsize}).'}\selectfont ' ;#."\n";
$latex .= $line->{line}."\\\\\n";
$latex .= '\fontsize{'.$fontsize.'}{'.
stupid_scaling_func($fontsize).'}\selectfont'."\n" unless $line == $slide->[-1];
$saved_fontsize = $line->{forcedsize};
}
} else {
$latex .= $line->{line}."\\\\\n";
$saved_fontsize = $fontsize;
}
} else {
$line->{numeral} = numeral($counter);
$latex .= '\newlength{\wz'.numeral($counter)."}\n";
$latex .= '\newlength{\hz'.numeral($counter)."}\n";
$latex .= '\settowidth{\wz'.numeral($counter).'}{'.accent2tex($line->{line})."\\\\}\n";
$latex .= '\showthe\wz'.numeral($counter)."\n";
$latex .= '\settoheight{\hz'.numeral($counter).'}{'.accent2tex($line->{line})."\\\\}\n";
$latex .= '\showthe\hz'.numeral($counter)."\n";
$counter++;
$latex .= accent2tex($line->{line})."\\\\\n";
}
} elsif ($line->{type} eq "tex") {
if ($pass == 2) {
if (exists $line->{forcedsize}) {
if ($line->{forcedsize} eq '-') {
$latex .= '\fontsize{'.$saved_fontsize.'}{'.
stupid_scaling_func($saved_fontsize).'}\selectfont ' ;#."\n";
$latex .= $line->{line}."\n";
$latex .= '\fontsize{'.$fontsize.'}{'.
stupid_scaling_func($fontsize).'}\selectfont'."\n" unless $line == $slide->[-1];
} else {
$latex .= '\fontsize{'.$line->{forcedsize}.'}{'.
stupid_scaling_func($line->{forcedsize}).'}\selectfont ' ;#."\n";
$latex .= $line->{line}."\n";
$latex .= '\fontsize{'.$fontsize.'}{'.
stupid_scaling_func($fontsize).'}\selectfont'."\n" unless $line == $slide->[-1];
$saved_fontsize = $line->{forcedsize};
}
} else {
$latex .= $line->{line}."\n";
$saved_fontsize = $fontsize;
}
} else {
$line->{numeral} = numeral($counter);
$latex .= '\newlength{\wz'.numeral($counter)."}\n";
$latex .= '\newlength{\hz'.numeral($counter)."}\n";
$latex .= '\settowidth{\wz'.numeral($counter).'}{'.accent2tex($line->{line})."}\n";
$latex .= '\showthe\wz'.numeral($counter)."\n";
$latex .= '\settoheight{\hz'.numeral($counter).'}{'.accent2tex($line->{line})."}\n";
$latex .= '\showthe\hz'.numeral($counter)."\n";
$counter++;
$latex .= accent2tex($line->{line})."\n";
}
} elsif ($line->{type} eq "image") {
if ($pass == 2) {
$latex .= image($line->{image}, $line->{scale}) if -e $line->{image};
} elsif (-e $line->{image}) {
$line->{numeral} = numeral($counter);
$latex .= '\newlength{\wz'.numeral($counter)."}\n";
$latex .= '\newlength{\hz'.numeral($counter)."}\n";
$latex .= '\settowidth{\wz'.numeral($counter).'}{'.image($line->{image}, $line->{scale})."}\n";
$latex .= '\showthe\wz'.numeral($counter)."\n";
$latex .= '\settoheight{\hz'.numeral($counter).'}{'.image($line->{image}, $line->{scale})."}\n";
$latex .= '\showthe\hz'.numeral($counter)."\n";
$counter++;
$latex .= image($line->{image}, $line->{scale})."\n";
}
} elsif ($line->{type} =~ /gnuplot/) {
my $gnuplot_image;
if ($pass == 1) {
$gnuplot_image = gnuplot($line->{script}, ($line->{type} =~ /\*/)?1:0, numeral($counter));
$line->{image} = $gnuplot_image;
$line->{numeral} = numeral($counter);
$line->{scale} = 1.0;
$latex .= '\newlength{\wz'.numeral($counter)."}\n";
$latex .= '\newlength{\hz'.numeral($counter)."}\n";
$latex .= '\settowidth{\wz'.numeral($counter).'}{'.image($line->{image}, $line->{scale})."}\n";
$latex .= '\showthe\wz'.numeral($counter)."\n";
$latex .= '\settoheight{\hz'.numeral($counter).'}{'.image($line->{image}, $line->{scale})."}\n";
$latex .= '\showthe\hz'.numeral($counter)."\n";
$counter++;
$latex .= image($line->{image}, $line->{scale});
} else {
$latex .= image($line->{image}, $line->{scale}) if -e $line->{image};
}
}
}
$latex .= '\end{center}'."\n";
$latex .= '\end{frame}'."\n\n";
#$counter++;
}
$latex .= '\end{document}'."\n";
return \$latex;
}
sub gnuplot {
my $script = shift;
my $noheader = shift || 0;
my $code = shift || 'xxx';
my ($gpfh, $gpfn, $gpimfn);
#print "$noheader, $code\n";
$gpfn = $infn.'!'.$code.'.plt';
$gpimfn = $infn.'!'.$code.'.pdf';
unless (!$redo_graphs and -e $gpimfn) {
my $gnuplot_header1 = <<"GNUPLOTHEADER1";
set term pdfcairo enh font "LM Sans 10, 12" fontscale 1 lw 2
set out '$gpimfn';
GNUPLOTHEADER1
my $gnuplot_header2 = <<"GNUPLOTHEADER2";
set out
GNUPLOTHEADER2
open($gpfh, '>:encoding(UTF-8)', $gpfn) || die "Error opening temporary file\n";
if ($noheader) {
$script =~ s/set out \*/set out '$gpimfn'/;
}
print $gpfh $gnuplot_header1 unless $noheader;
print $gpfh $script;
print $gpfh $gnuplot_header2 unless $noheader;
close $gpfh;
system($gnuplot_path, $gpfn);
}
return $gpimfn;
}
sub image {
my $ifn = shift;
my $scale = shift || 0.7;
return '\includegraphics[width='.$scale.'\textwidth]{'.$ifn."}\n";
}
sub numeral {
#my @digits = qw|zero one two three four five six seven eight nine|;
my @digits = qw|a b c d e f g h i j|;
return join 'x', map $digits[$_], split(//, $_[0]);
}
sub stupid_scaling_func {
return int($_[0]+$layout{scale_a}*(1-2/(1+exp(2*$_[0]/$layout{scale_b}))));
}
# This shit is needed because pdflatex is a buggy piece of crap
# that prints malformed utf-8 into its log file.
sub accent2tex {
#shift;
local $_=$_[0];
s/á/\\'a/g;
s/é/\\'e/g;
s/í/\\'\{\\i\}/g;
s/ó/\\'o/g;
s/ú/\\'u/g;
s/ö/\\"o/g;
s/ü/\\"u/g;
s/ő/\\H\{o\}/g;
s/ű/\\H\{u\}/g;
s/Á/\\'A/g;
s/É/\\'E/g;
s/Í/\\'I/g;
s/Ó/\\'O/g;
s/Ú/\\'U/g;
s/Ö/\\"O/g;
s/Ü/\\"U/g;
s/Ő/\\H\{O\}/g;
s/Ű/\\H\{U\}/g;
return $_;
}