-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex.sh
executable file
·138 lines (114 loc) · 2.51 KB
/
latex.sh
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
#!/usr/bin/env bash
# latex-inline
# Author: https://github.com/lewisjb
LATEX_ENDINGS=("tex" "aux" "log" "pdf" "png")
W3MIMAGEDISPLAY="/usr/lib/w3m/w3mimgdisplay"
FILENAME="tmp"
DEBUG=false
DPI=100
ANCHOR=""
# Output basic usage explanation
usage() {
echo "Usage: $0 \"<equation>\""
}
# Output full usage explanation
full_usage() {
usage
cat << EOF
Flags:
-h | --help Shows this full help message.
-d | --debug Enables debugging mode. This puts it in verbose mode,
and doesn't automatically remove the temp files.
--imgdisplay <path> Uses path for w3mimgdisplay.
--filename <name> Uses filename for the temporary files.
--dpi <dpi> Sets the DPI of the LaTeX equation (def=100).
EOF
}
overflow_warning() {
echo -e "Warning: Using latex-inline after a whole 'page' of terminal \
history won't work as intended."
}
# Handle flags
while test $# -gt 0 ; do
case "$1" in
-h|--help)
full_usage
exit 0
;;
-d|--debug)
DEBUG=true
;;
--imgdisplay)
W3MIMAGEDISPLAY="$2"
shift
;;
--filename)
FILENAME="$2"
shift
;;
--dpi)
DPI="$2"
shift
;;
*)
break
;;
esac
shift
done
if [ -z "$1" ]; then
usage
exit 1
fi
# Create tmp - LaTeX file to be converted to PNG
cat << EOF > "$FILENAME.tex"
\\documentclass[convert={density=$DPI}]{standalone}
\\usepackage{color}
\\begin{document}
\\textcolor{white}{\$$1\$}
\\end{document}
EOF
# Create the files
if [ "$DEBUG" = true ]; then
pdflatex -shell-escape $FILENAME
else
pdflatex -shell-escape $FILENAME > /dev/null
fi
# Terminal rows
ROWS=`tput lines`
# Get the current cursor position
echo -en "\E[6n"
read -sdR CURPOS
CURPOS=${CURPOS#*[}
# CURPOS = row;col
IFS=';' read -ra CURPOS <<< "$CURPOS"
# CURPOS = (row col)
CURLINE="${CURPOS[0]}"
if [ "$CURLINE" -eq "$ROWS" ]; then
overflow_warning
fi
# Get the terminal height (px)
stty -echo
printf "%b%s" "\033[14t\033[c"
read -t 1 -d c -s -r SIZE; stty echo
HEIGHT=`echo "$SIZE" | awk -F ';' '{print $2}'`
# Font height = px/rows
FONTH=$(($HEIGHT / $ROWS))
# Y-offset = Y cursor row * font height
yoff=$((($CURLINE)*$FONTH))
yoff=$(($yoff-$FONTH))
# Get dimensions of image
read width height <<< `echo -e "5;$FILENAME.png" | $W3MIMAGEDISPLAY`
# Move cursor down
tput cup $((($height/$FONTH)+${CURPOS[0]})) 0
# TODO: Find better way, but a delay is needed for tput to take effect
sleep 0.1
# Display it
echo -e "0;1;0;$yoff;$width;$height;;;;;$FILENAME.png\n4;\n3;" | $W3MIMAGEDISPLAY
# Cleanup
if [ "$DEBUG" = false ]; then
for i in "${LATEX_ENDINGS[@]}"
do
rm "$FILENAME.$i"
done
fi