-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimg.ado
418 lines (323 loc) · 12.6 KB
/
img.ado
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
/*** DO NOT EDIT THIS LINE -----------------------------------------------------
Version:
Title: img
Description: captures and imports images and graphs into the dynamic document.
This command belongs to {bf:{help Weaver}} package but it also supports the
{bf:{help MarkDoc}} package. This document only describes __txt__ in Weaver package.
For using the command in MarkDoc package,
[read the MarkDoc manual](https://github.com/haghish/MarkDoc/wiki/img/).
----------------------------------------------------- DO NOT EDIT THIS LINE ***/
/***
Syntax
======
Import graphical files in the dynamic document
{cmdab:img} [using {it:{help filename}}] [{cmd:,} {opt tit:le(str)} {opt w:idth(int)} {opt h:eight(int)} {opt left} {opt center} ]
Automatically include the {it:current graph} from Stata in the dynamic document
{cmdab:img} [{cmd:,} {opt tit:le(str)} {opt w:idth(int)} {opt h:eight(int)} {opt left} {opt center} ]
Description
===========
The __img__ command imports images and graphs into the dynamic document.
Any graphical file that is compatible with a web-browser can be inserted in the
html log. This command belongs to {help Weaver} package but it also supports the
{help MarkDoc} package. The syntax for both packages is the same but
the __img__ command behave differently based on which of the packages is in use.
If Weaver html log and smcl log are open at the same time, the command
only functions for Weaver and not for MarkDoc. In contrast, when Weaver html log is
not open and scml log is on, it will function for MarkDoc package.
Options
=======
{phang}{cmdab:tit:tle(}{it:str}{cmd:)} specify a header string (title) for the figure {p_end}
{phang}{opt w:idth(int)} define the width of the figure. This option must be used
with {opt h:ight(int)} option. Otherwise, it will keep the actual hight of the
figure and only changes the width. {p_end}
{phang}{opt h:ight(int)} define the hight of the figure. This option must be used
with {opt w:idth(int)} option for the same reason mentioned above. {p_end}
{phang}{cmdab::left} this option is the default and it aligns the figure to the
left-side of the dynamic document. {p_end}
{phang}{cmdab::center} aligns the figure to the center of the dynamic document. {p_end}
Examples
=================
You have created a graph in Stata. Before importing in the HTML log, you should
export it in a format that can be interpreted in html. Such as PNG which is recommended
because it is lossless format and the same file can be used for publication.
. sysuse auto
. histogram price
. graph export price.png, replace
. img using price.png
. img using price.png, title("Histogram of the Price variable")
. img using price.png, w(300) h(200) center
Alternatively, the image can be obtained from Stata automatically
. histogram mpg
. img, title("Histogram of the MPG variable")
Author
======
__E. F. Haghish__
Center for Medical Biometry and Medical Informatics
University of Freiburg, Germany
_and_
Department of Mathematics and Computer Science
University of Southern Denmark
haghish@imbi.uni-freiburg.de
[Weaver Homepage](www.haghish.com/weaver)
Package Updates on [Twitter](http://www.twitter.com/Haghish)
- - -
_This help file was dynamically produced by[MarkDoc Literate Programming package](http://www.haghish.com/markdoc/)_
***/
*cap prog drop img
program define img
version 11
syntax [using/], [Width(numlist max=1 >0 <=14000)] ///
[Height(numlist max=1 int >0 <=14000)] [TITle(str)] [left|center] ///
[Markup(str)]
************************************************************************
* GENERAL SYNTAX PROCESSING
*
* - load global values
* - make sure "`using'" is a filename
* - if `using' is missing, automaticaly export the image and define it
* - check that only one of the align options is selected
* - defining the default image alignment
* - check the markup option (FOR MARKDOC ONLY)
************************************************************************
if !missing("`using'") {
confirm file "`using'"
}
else {
local autoimg 1
// WF : Weaver-figure directory or Log-file directory for MarkDoc
if "$weaver" != "" {
if "`c(os)'"=="Windows" local WF = "$weaverDir" + "\Weaver-figure\"
if "`c(os)'"!="Windows" local WF = "$weaverDir" + "/Weaver-figure/"
}
if "$weaver" == "" {
quietly log query
if "`r(status)'" != "on" {
quietly log query rundoc
}
*if "`r(status)'" == "on" {
local path = r(filename)
if "`c(os)'" == "Windows" {
local path : subinstr local path "/" "\", all
tokenize "`path'", parse("\")
}
else tokenize "`path'", parse("/")
while !missing("`1'") {
if !missing("`3'") local WF = "`WF'" +"`1'" //avoid the last 2
macro shift
}
// define the path to Weaver-figure
if "`c(os)'" == "Windows" local WF = "`WF'" + "\Weaver-figure\"
if "`c(os)'" != "Windows" local WF = "`WF'" + "/Weaver-figure/"
*}
}
// try to navigate to Weaver-figure, to check if it already exists
local d `"`c(pwd)'`c(dirsep)'"'
capture cd "`WF'"
if _rc != 0 {
mkdir "`WF'", public
}
else {
quietly cd "`d'"
}
// the first time img is called, erase the previous figures, if the
// weaver log IS NOT APPENDED or if Weaver is not in use
if missing("$currentFigure") {
global currentFigure 1
if !missing("$weaver") & missing("$weaverAppend") | ///
missing("$weaver") {
local files : dir "`WF'" files "*.png"
foreach lname in `files' {
capture erase "`WF'`lname'"
}
}
}
else global currentFigure = $currentFigure + 1
// EXPORTING THE GRAPH for Weaver & MarkDoc
local exp = "`WF'" + "figure_$currentFigure" + ".png"
if !missing("$weaver") {
qui graph export "`exp'", replace
}
if missing("$weaver") {
if missing("`width'") local w 400
else local w `width'
qui graph export "`exp'", replace width(`w')
}
// define using for the rest of the program
local using = "Weaver-figure/figure_$currentFigure" + ".png"
}
if "`left'" == "left" & "`center'" == "center" {
di as err `"{p}The {bf:left} and "' ///
`"{bf:center} options cannot be used together"'
exit 198
}
// make center the default location of the figure for HTML and LaTeX
if missing("`left'") & missing("`center'") {
local center center
}
if "`center'" == "center" {
local centerClass class="center"
local centerStyle style="text-align:center;"
}
if "$weaver" == "" {
if !missing("`markup'") & "`markup'" != "markdown" & ///
"`markup'" != "html" & "`markup'" != "latex" {
di as err `"{p}{bf:`markup'} is not recognized"'
exit 198
}
}
************************************************************************
* FOR WEAVER PACKAGE
*
* - setup the default image size for HTML log
* - write the image title in html
* - print the LaTeX code
************************************************************************
if "$weaver" != "" {
tempname canvas
file open `canvas' using `"$weaverFullPath"', write text append
if "$weaverMarkup" == "html" {
if "$format" == "normal" & missing("`width'") {
local width 343
}
if "$format" == "normal" & missing("`height'") {
local height 250
}
if "$format" == "landscape" & missing("`width'") {
local width 1020
}
if "$format" == "landscape" & missing("`height'") {
local height 694
}
/*
if "$format" == "normal" & "`width'" > "694" {
display as error "{p}image width cannot be more than {bf:694} " ///
"pixles, unless you choose the {help landscape} option " ///
"from the {help weave} command"
exit 198
}
if "$format" == "normal" & "`height'" > "1000" {
display as error "{p}image height cannot be more than {bf:1000} pixles"
exit 198
}
if "$format" == "landscape" & "`width'" > "1020" {
display as error "{p}image width cannot be more than {bf:1000} pixles"
exit 198
}
if "$format" == "landscape" & "`height'" > "694" {
display as error "image height cannot be more than 694 " ///
"pixles, unless you {bf:remove} the {help landscape} " ///
"option from the {help weave} command"
exit 198
}
*/
file write `canvas' `"<img rel="zoom" src="`using'" "' ///
`"`centerClass' width="`width'" height="`height'" >"'
if "`title'" ~= "" {
file write `canvas' _n `"<span class="tble" "' ///
`"`centerStyle'>`title'</span>"' ///
_n "<br />" _n
}
}
if "$weaverMarkup" == "latex" {
if missing("`width'") & missing("`height'") {
local width 225
local height 150
}
if !missing("`width'") & missing("`height'") {
local miss h
}
if missing("`width'") & !missing("`height'") {
local miss w
}
file write `canvas' _n "\begin{figure}[h]" _n
if !missing("`center'") file write `canvas' " \centering" _n
else file write `canvas' " \captionsetup{justification=" ///
"raggedright,singlelinecheck=false}" _n
if "`miss'" == "w" file write `canvas' ///
" \includegraphics[height=`height'px]{`using'}" _n
else if "`miss'" == "h" file write `canvas' ///
" \includegraphics[width=`width'px]{`using'}" _n
else file write `canvas' " \includegraphics[width=`width'px, " ///
"height=`height'px]{`using'}" _n
if "`title'" ~= "" file write `canvas' " \caption{`title'}" _n
file write `canvas' "\end{figure}" _n(2)
}
}
************************************************************************
* FOR MarkDoc PACKAGES
************************************************************************
if "$weaver" == "" | "$noisyWeaver" == "yes" {
//If the smcl log file is on
quietly log query
if missing("`width'") & missing("`height'") {
local width 350
local height 250
}
// -----------------------------------------------------------------
// Define the Markup language and print the output
// =================================================================
if missing("`markup'") & !missing("$markdocDefault") {
local markup "$markdocDefault"
}
if missing("`markup'") & missing("$markdocDefault") {
local markup "markdown"
}
// -----------------------------------------------------------------
// Markdown syntax
// -----------------------------------------------------------------
if "`markup'" == "markdown" {
noisily display as txt _n ">" _n
}
// -----------------------------------------------------------------
// HTML syntax
// -----------------------------------------------------------------
if "`left'" == "left" & "`markup'" == "html" {
if "`title'" ~= "" {
noisily display as txt ///
`"><p style="text-align:left; margin-bottom:0;">`title'</p>"' _n
}
noisily display as txt `"><img src="`using'" "' ///
`"width="`width'" height="`height'" >"' _newline
}
if "`center'" == "center" & "`markup'" == "html" {
if "`title'" ~= "" {
noisily display as txt ///
`"><p style="text-align:center; margin-bottom:0;">`title'</p>"' _n
}
noisily display `"><img src="`using'" "' ///
`"class="center" width="`width'" height="`height'" >"' _n
}
// -----------------------------------------------------------------
// LaTeX syntax
// =================================================================
if "`markup'" == "latex" {
if missing("`width'") & missing("`height'") {
local width 225
local height 150
}
if !missing("`width'") & missing("`height'") {
local miss h
}
if missing("`width'") & !missing("`height'") {
local miss w
}
//if !missing("`autoimg'") noisily display _n "\end{verbatim}" _n
noisily display _n ">\begin{figure}[h]"
if !missing("`center'") noisily display ">\centering"
else noisily display ">\captionsetup{justification=" ///
"raggedright,singlelinecheck=false}"
if "`miss'" == "w" noisily display ///
">\includegraphics[height=`height'px]{`using'}"
else if "`miss'" == "h" noisily display ///
">\includegraphics[width=`width'px]{`using'}"
else noisily display ">\includegraphics[width=`width'px, " ///
"height=`height'px]{`using'}"
if "`title'" ~= "" noisily display ">\caption{`title'}"
noisily display ">\end{figure}" _n
//if !missing("`autoimg'") noisily display _n "\begin{verbatim}" _n
}
}
end
// DYNAMIC HELP FILE
// ===================================
*markdoc img.ado, export(sthlp) replace