summarylogtreecommitdiffstats
path: root/latextogrob
blob: de98d1514d65c2b4b190b7bb9acd5b09571566dc (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
#!/bin/bash
#
# latextogrob: A simple script that converts a file with LaTeX fomulae into a grob image.
#
# Copyright (c) 2010-2011 Adria ARRUFAT <swiftscythe@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

usage () {
	echo "$0 [input file]"
	echo ""
	echo " This is a simple script that converts a file with LaTeX fomulae into a grob image."
	echo " The input file must contain one formula per line and end with an empty line."
	echo ""
	exit 1
}

[ "$1" = "" ]       && usage
[ "$1" = "--help" ] && usage
[ "$1" = "-h" ]     && usage

# Customised mathTeX options for better results in HP 50g display
linetogif()
{
  mathtex -m 0 "\tiny\dpi{140}\gammacorrection{3}\mathtt{$2}" -o $1
}

# Timer start
START="$(date +%s)"

# Parse the input file: 
# change '\' -> '\\'  
# change ' ' -> '\\;' 
# etc
# You can add or remove more rules: 
sed -e "s/\\\/\\\\\\\/g" \
  -e "s/ /\\\\\\\;/g" \
  -e "s/·/\\\\\\\cdot{}/g" \
  -e "s/(/\\\\\\\left(/g" \
  -e "s/)/\\\\\\\right)/g" \
  -e "s/\[/\\\\\\\left\[/g" \
  -e "s/\]/\\\\\\\right\]/g" \
  -e "s/à/\\\\\\\grave{a}/g" \
  -e "s/è/\\\\\\\grave{e}/g" \
  -e "s/ì/\\\\\\\grave{i}/g" \
  -e "s/ò/\\\\\\\grave{o}/g" \
  -e "s/á/\\\\\\\acute{a}/g" \
  -e "s/é/\\\\\\\acute{e}/g" \
  -e "s/í/\\\\\\\acute{i}/g" \
  -e "s/ó/\\\\\\\acute{o}/g" \
  -e "s/ú/\\\\\\\acute{u}/g" \
  $1 > $1.tmp

let "i = 1"
TEN=10
HUNDRED=100
LINES=$(wc -l $1 | awk '{print $1}')
NAME=$(echo $1 | sed -e "s/.tex//g")

# Convert each line of the input file into a gif.
cat $1.tmp | while read line;
do
  if [ $i -lt $TEN ]; then
    linetogif $NAME-00$i $(echo $line)
    convert -border 2 -bordercolor transparent $NAME-00$i.gif $NAME-00$i.gif
  elif [ $i -lt $HUNDRED ]; then
    linetogif $NAME-0$i $(echo $line)
    convert -border 2 -bordercolor transparent $NAME-0$i.gif $NAME-0$i.gif
  else
    linetogif $NAME-$i $(echo $line)
    convert -border 2 -bordercolor transparent $NAME-$i.gif $NAME-$i.gif
  fi
  echo -en "Progress $i/$LINES \r"
  let "i += 1"
done

# Remove the temporary file
rm $1.tmp

# If a previous formula image file exists, remove it
if [ -f $NAME.gif ]; then
  rm $NAME.gif
fi

# Append all gif images beginning with the same name into a larger gif
convert $NAME-*.gif -append $NAME.gif

# Convert the resulting image into grb
giftopnm $NAME.gif | ppmtopgm | pgmtogrob > $NAME.grb

# Timer end
END="$(date +%s)"
ELAPSED="$(expr $END - $START)"

echo -e "\nDone! All $LINES images correctly converted in $ELAPSED seconds!\a\v"