Skip to content

Commit 7e4a6ab

Browse files
author
Meiting-Wang
committed
Initial commit
0 parents  commit 7e4a6ab

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Created by https://www.gitignore.io/api/stata
2+
# Edit at https://www.gitignore.io/?templates=stata
3+
4+
### Stata ###
5+
# .gitignore file for git projects containing Stata files
6+
# Commercial statistical software: http://www.stata.com
7+
8+
# Stata dataset and output files
9+
*.dta
10+
*.gph
11+
*.log
12+
*.smcl
13+
*.stpr
14+
*.stsem
15+
16+
# Graphic export files from Stata
17+
# Stata command graph export: http://www.stata.com/manuals14/g-2graphexport.pdf
18+
#
19+
# You may add graphic export files to your .gitignore. However you should be
20+
# aware that this will exclude all image files from this main directory
21+
# and subdirectories.
22+
# *.ps
23+
# *.eps
24+
# *.wmf
25+
# *.emf
26+
# *.pdf
27+
# *.png
28+
# *.tif
29+
30+
# End of https://www.gitignore.io/api/stata

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Meiting-Wang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

stata.toc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
v 1.0.0
2+
d Materials by Meiting-Wang
3+
d School of Economics, South-Central University for Nationalities
4+
d 2017110097@mail.scuec.edu.cn
5+
6+
d 'wmtmat': output a matrix to Word, LaTeX, Stata interface.
7+
8+
p wmtmat

wmtmat.ado

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
* Feather: output matrix to Stata interface, Word and LaTeX
2+
* Author: Meiting Wang, Master, School of Economics, South-Central University for Nationalities
3+
* Email: 2017110097@mail.scuec.edu.cn
4+
* Created on Oct 26th, 2019
5+
6+
7+
program define wmtmat
8+
version 15.1
9+
10+
syntax name(id="a matrix name") [using/] [, ///
11+
replace append FMT(string) ROWSFMT(string) COLSFMT(string) ///
12+
TItle(string) Alignment(string) PAGE(string)]
13+
/*
14+
optional illustration:
15+
1. name: only a matrix name permitted.
16+
2. fmt(): set the overall format for the reported matrix.
17+
3. rowsfmt(): set the format of each row of the matrix separately.
18+
4. colsfmt(): set the format of each column of the matrix separately.
19+
5. title(): set the title for the reported table, matrix name as the default.
20+
6. alignment(): only used in the LaTeX output, set the column format of the LaTeX
21+
table, but it will not impact the column format in the Stata output table, dot as
22+
the default.
23+
7. page(): only used in the LaTeX output,set the extra package for the LaTeX code.
24+
please don't need to add the package of booktabs array dcolumn, because the code
25+
will automatic process these with the option of alignment().
26+
*/
27+
28+
29+
*--------设置默认格式------------
30+
local default_fmt "%11.3f"
31+
local default_la_fmt "%11.3fc"
32+
33+
34+
*---------程序不合规时的报错-----------
35+
if ("`replace'`append'"!="")&("`using'"=="") {
36+
dis "{error:replace or append can't appear when you don't need to output result to a file.}"
37+
exit
38+
}
39+
40+
if ("`replace'"!="")&("`append'"!="") {
41+
dis "{error:replace and append cannot appear at the same time.}"
42+
exit
43+
}
44+
45+
if ("`fmt'"!="")&("`rowsfmt'"!="") {
46+
dis "{error:fmt and rowsfmt can't appear at the same time.}"
47+
exit
48+
}
49+
if ("`fmt'"!="")&("`colsfmt'"!="") {
50+
dis "{error:fmt and colsfmt can't appear at the same time.}"
51+
exit
52+
}
53+
if ("`rowsfmt'"!="")&("`colsfmt'"!="") {
54+
dis "{error:rowsfmt and colsfmt can't appear at the same time.}"
55+
exit
56+
}
57+
58+
if "`fmt'" != "" {
59+
local fmt_num: word count `fmt'
60+
if `fmt_num' != 1 {
61+
dis "{error:fmt needs to be a single word.}"
62+
exit
63+
}
64+
}
65+
if "`rowsfmt'" != "" {
66+
local rowsfmt_num: word count `rowsfmt'
67+
if `rowsfmt_num' != rowsof(`namelist') {
68+
dis "{error:the number of words in rowsfmt() can't match the number of rows in the matrix `namelist'.}"
69+
exit
70+
}
71+
}
72+
if "`colsfmt'" != "" {
73+
local colsfmt_num: word count `colsfmt'
74+
if `colsfmt_num' != colsof(`namelist') {
75+
dis "{error:the number of words in colsfmt() can't match the number of cols in the matrix `namelist'.}"
76+
exit
77+
}
78+
}
79+
80+
if (~ustrregexm("`using'",".tex"))&("`alignment'`page'"!="") {
81+
dis "{error:alignment and page can only be used in the LaTeX output.}"
82+
exit
83+
}
84+
85+
86+
*---------前期语句处理----------
87+
*普通选项语句的处理
88+
if "`using'" != "" {
89+
local us_ing "using `using'"
90+
}
91+
if "`title'" == "" {
92+
local title "Matrix `namelist'[`=rowsof(`namelist')',`=colsof(`namelist')']"
93+
}
94+
95+
*fmt系列语句构建
96+
if "`fmt'" != "" {
97+
local st_fmt "fmt(`fmt')"
98+
local st_fmt_la "fmt(`fmt')"
99+
}
100+
else if "`rowsfmt'" != "" {
101+
local st_fmt `"fmt("`rowsfmt'")"'
102+
local st_fmt_la `"fmt("`rowsfmt'")"'
103+
}
104+
else if "`colsfmt'" != "" {
105+
local st_fmt "fmt(`colsfmt')"
106+
local st_fmt_la "fmt(`colsfmt')"
107+
}
108+
else {
109+
local st_fmt "fmt(`default_fmt')"
110+
local st_fmt_la "fmt(`default_la_fmt')"
111+
} //默认值
112+
113+
*构建esttab中alignment()和page()内部的语句(LaTeX输出专属)
114+
if "`page'" != "" {
115+
local page ",`page'"
116+
}
117+
if "`alignment'" == "math" {
118+
local page "array`page'"
119+
local alignment "*{`=colsof(`namelist')'}{>{$}c<{$}}"
120+
121+
}
122+
else {
123+
local page "array,dcolumn`page'"
124+
local alignment "*{`=colsof(`namelist')'}{D{.}{.}{-1}}"
125+
}
126+
//加上array宏包可使得表格线之间的衔接没有空缺
127+
128+
129+
*-----------------主程序---------------
130+
esttab matrix(`namelist', `st_fmt'), compress ///
131+
nomtitles title(`title') //Stata 界面显示
132+
if ustrregexm("`us_ing'",".rtf") {
133+
esttab matrix(`namelist', `st_fmt') `us_ing', compress `replace'`append' ///
134+
nomtitles title(`title') //Word 显示
135+
}
136+
if ustrregexm("`us_ing'",".tex") {
137+
local col_names: colnames `namelist'
138+
tokenize "`col_names'"
139+
local i = 1
140+
local col_new_names ""
141+
while "``i''" != "" {
142+
local col_new_names "`col_new_names'\multicolumn{1}{c}{``i''} "
143+
local `i' "" //置空`i'
144+
local i = `i' + 1
145+
}
146+
mat colnames `namelist' = `col_new_names'
147+
esttab matrix(`namelist', `st_fmt_la') `us_ing', compress `replace'`append' ///
148+
nomtitles title(`title') booktabs width(\hsize) page(`page') ///
149+
alignment(`alignment')
150+
mat colnames `namelist' = `col_names'
151+
} //LaTeX 显示
152+
end

wmtmat.pkg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
v 1.0.0
2+
d 'WMTMAT': wmtmat
3+
d
4+
d output a matrix to Word, LaTeX, Stata interface.
5+
d
6+
d Distribution-Date: 20200427
7+
d License: MIT
8+
d
9+
F wmtmat.ado

0 commit comments

Comments
 (0)