我们先新建一个 do 文件,命名为 putdocx_example2。注意: 我们也可以不新建 do 文件,直接在已有 do 文件中输入并执行以下命令。
*--------------------------begin putdocx_example2.do------------------------
webuse nhanes2, clear
putdocx begin // 声明使用 putdocx 命令,新建 Word 文档
// 我们可这样理解:一个 .docx 文件包含很多段落,每一次写入一段必须声明。
// 添加段落标题,样式为 "标题 (Title)"
putdocx paragraph, style(Title)
putdocx text ("Blood pressure report")
// 查看相关样式的可选项: help putdocx_appendix
// 添加文本块
putdocx textblock begin
We use data from the Second National Health and Nutrition Examination Survey
to study the incidence of high blood pressure.
putdocx textblock end
// 添加段落标题,样式为 "标题1 (Heading1)"
putdocx paragraph, style(Heading1)
putdocx text ("Logistic regression results")
// 添加文本块
putdocx textblock begin
We fit a logistic regression model of high blood pressure on
weight, age group, and the interaction between age group and sex.
putdocx textblock end
logistic highbp weight agegrp##sex, nopvalues vsquish
// 添加上述命令生成的估计表格
putdocx table results = etable
// 添加段落标题,样式为 "Heading1"
putdocx paragraph, style(Heading1)
putdocx text ("Interaction plot")
// 进行边际效应分析,并画图
margins agegrp#sex
marginsplot, title(Age Group and Sex Interaction) ///
ytitle(Expected Probability of High Blood Pressure) ///
legend(ring(0) bplacement(seast) col(1))
graph export interaction.png, replace
// 另起一段,插入上述命令生成的图
putdocx paragraph, halign(center)
putdocx image interaction.png
// 保存 Word 文档
putdocx save report1, replace
// 若出现错误:document already open in memory
// 可能是执行 putdocx begin 后遇到错误中断后,再重新执行。
// 解决该错误可尝试该命令:putdocx clear
// 再重新执行上述命令。
*--------------------------end putdocx_example2.do---------------------------
我们对范例 2 的输出结果进行进一步的美化,比如设置页眉、页脚、页码,美化图表等。我们新建一个 do 文件,命名为 putdocx_example3。
**注意:**我们也可以不新建 do 文件,直接在已有 do 文件中输入并执行以下命令。
*--------------------------begin putdocx_example3.do------------------------
webuse nhanes2, clear
// 声明新建带页眉的 Word 文档
putdocx begin, header(head)
// 自定义页眉的内容,并加上页码
putdocx paragraph, toheader(head) font(,14)
putdocx text ("Blood pressure report: ")
putdocx pagenumber
// 添加段落标题,样式为 "Heading1"
putdocx paragraph, style(Heading1)
putdocx text ("Data")
// 添加文本块
putdocx textblock begin
We use data from the Second National Health and Nutrition Examination Survey
to study the incidence of high blood pressure.
putdocx textblock end
// 添加段落标题,样式为 "Heading1"
putdocx paragraph, style(Heading1)
putdocx text ("Logistic regression results")
// 添加文本块
putdocx textblock begin
We fit logistic regression models of high blood pressure on
weight, age group, and the interaction between age group and sex.
putdocx textblock end
// 添加估计表格
logistic highbp weight agegrp##sex, nopvalues vsquish
putdocx table results = etable
// 对表格的某几行,添加背景色
putdocx table results(3 5 7 9 11 13 15 17,.), shading(lightgray)
// 设置表格中数字的显示格式,为:共占5个空格,小数位占三个空格
putdocx table results(2/17,2/5), nformat(%5.3f)
// 改变单元格中的内容
putdocx table results(1,1) = ("High BP")
// 添加段落标题,样式为 "Heading1"
putdocx paragraph, style(Heading1)
putdocx text ("Interaction plot")
// 进行边际效应分析,并画图
margins agegrp#sex
marginsplot, title(Age Group and Sex Interaction) ///
ytitle(Expected Probability of High Blood Pressure) ///
legend(ring(0) bplacement(seast) col(1))
graph export interaction.png, replace
// 另起一段,插入上述命令生成的图,并设置图高度
putdocx paragraph, halign(center)
putdocx image interaction.png, height(5 in)
// 保存 Word 文档
putdocx save report2, replace
*--------------------------end putdocx_example3.do---------------------------
将论文拆分成三部分:
My_Paper.docx,即论文主文档,包含引言、文献综述、理论分析、参考文献等;
My_Table.docx,即表格文档,包含统计和回归表格;
My_Fig.docx,即图形文档,包含各种图。
每部分都可以用 putdocx 或 dyndoc 命令进行编写 do 文件,生成相应的 Word 文档。然后把这些 do 文件放到 Prog 子文件夹。如果某部分不想用命令,也可以直接在 Word 文档上码字,贴上图表。
我们先新建一个 do 文件,命名为 putpdf_example1。**注意:**我们也可以不新建 do 文件,直接在已有 do 文件中输入并执行以下命令。
*--------------------------begin putpdf_example1.do------------------------
webuse nhanes2, clear
putpdf begin // 声明使用 putpdf 命令,新建 PDF 文档
// 我们可这样理解:一个 .pdf 文件包含很多段落,每一次写入一段必须声明。
// 添加段落标题,段落内容居中,并设置字体大小
putpdf paragraph, font(,20) halign(center)
putpdf text ("Blood pressure report")
// 另起一段,添加文本
putpdf paragraph
putpdf text ("We use data from the Second National Health and Nutrition ")
putpdf text ("Examination Survey to study the incidence of high blood pressure.")
// 另起一段,添加文本
putpdf paragraph, font(,16)
putpdf text ("Logistic regression results")
// 另起一段,添加文本
putpdf paragraph
putpdf text ("We fit a logistic regression model of high blood pressure on ")
putpdf text ("weight, age group, and the interaction between age group and sex.")
logistic highbp weight agegrp##sex, nopvalues vsquish
// 添加上述命令生成的估计表格
putpdf table results = etable
// 另起一页,生成新的段落
putpdf pagebreak
putpdf paragraph, font(,16)
putpdf text ("Interaction plot")
// 进行边际效应分析,并画图
margins agegrp#sex
marginsplot, title(Age Group and Sex Interaction) ///
ytitle(Expected Probability of High Blood Pressure) ///
legend(ring(0) bplacement(seast) col(1))
graph export interaction.png, replace
// 另起一段,插入上述命令生成的图
putpdf paragraph, halign(center)
putpdf image interaction.png
// 保存 PDF 文档
putpdf save report-pdf, replace
*--------------------------end putpdf_example1.do---------------------------