-
Notifications
You must be signed in to change notification settings - Fork 0
/
pptex.bas
110 lines (94 loc) · 2.89 KB
/
pptex.bas
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
'pptex for Mac Microsoft Powerpoint 2016
'https://github.com/v-joe/pptex
'License: GPLv3
Sub pptex()
Dim tex As String
Dim LF As String
LF = Chr(10)
tex = "\documentclass[varwidth=true,border=2pt]{standalone}" & LF & _
"\usepackage{color}" & LF & _
"\begin{document}" & LF & _
"\huge" & LF & _
LF & LF & LF & LF & LF & LF & _
"\end{document}"
Dim tmpdir As String
Dim file As String
Dim dtex As String
Dim ntex As String
Dim wscale As Single
tmpdir = ""
On Error Resume Next
' MacScript is deprecated in PowerPoint 2016, but running mktemp with it
' will create a sub directory in PowerPoint's temporary space it can read from
' and write to
tmpdir = MacScript("do shell script " & Chr(34) & "/usr/bin/mktemp -d 2>/dev/null" & Chr(34))
On Error GoTo 0
If tmpdir = "" Then
MsgBox "Cannot create temporary directory", Title:="pptex error"
Exit Sub
End If
file = tmpdir & "/input.tex"
pdf = tmpdir & "/input.pdf"
' Check if there is previous LaTeX code as alternative text in the
' selected shape
dtex = ""
On Error Resume Next
dtex = ActiveWindow.Selection.ShapeRange.Item(1).AlternativeText
On Error GoTo 0
If InStr(dtex, "\documentclass") > 0 Then
tex = dtex
End If
' Write LaTeX input
Open file For Output As #1
Print #1, tex;
Close #1
' Launch editor and pdfLaTeX
On Error GoTo pdfLaTeXerror
pdfLaTeXOutput = AppleScriptTask("pptexLaunch.applescript", "ppLauncher", tmpdir)
On Error GoTo 0
' If input wasn't changed, quit here
Open file For Input As #1
ntex = Input(LOF(1), 1)
Close #1
If tex = ntex Then GoTo texcontinue
' Insert generated pdf
If dtex = "" Then
On Error GoTo texerror
Set x = ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
fileName:=pdf, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, Left:=200, Top:=100)
Else
' In case of selected pptex-generated shape, replace it with new one
' at same position and same relative scale factor
Set s = ActiveWindow.Selection.ShapeRange.Item(1)
On Error GoTo texerror
Set x = ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
fileName:=pdf, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, Left:=s.Left, Top:=s.Top)
wscale = s.Width
s.ScaleWidth 1, msoTrue
wscale = wscale / s.Width
x.ScaleWidth wscale, msoTrue
x.ScaleHeight wscale, msoTrue
s.Delete
End If
x.Select
' Store LaTeX source code as alternative text of pdf figure
x.AlternativeText = ntex
' Remove temporary sub directory
texcontinue:
On Error Resume Next
MacScript "do shell script " & Chr(34) & "rm -r " & tmpdir & " 2>/dev/null" & Chr(34)
On Error GoTo 0
Exit Sub
texerror:
MsgBox "Error in LaTeX input", vbCritical, "pptex"
GoTo texcontinue
' create message box with pdfLaTeX log in case of error
pdfLaTeXerror:
On Error Resume Next
MsgBox "pdfLaTeX error:" & LF & MacScript("do shell script " & Chr(34) & "tail -25 " & tmpdir & "/input.log" & Chr(34))
GoTo texcontinue
End Sub