我的顾问要我在汇总统计表中添加美元符号.我生成这个表并使用Stata的esttab命令将其导出到Latex.
我需要1)在一些结果单元格中添加美元符号(不是全部)和2)确保Latex可以处理美元符号.
我认为2可以使用替换选项完成,但我无法弄清楚如何做1.这是我试图用来解决这个问题的一些最小代码.
sysuse auto, clear
estpost summarize price mpg weight length if foreign==0
est store A
estpost summarize price mpg weight length if foreign==1
est store B
esttab A B using $root/Outputs/test.tex, replace /// //a file path on my machine
cells("mean (fmt(%9.0fc %9.2fc %9.0fc))" "sd(par fmt(%9.0fc %9.2fc %9.0fc))") ///
mtitle("Domestic" "Foreign") ///
mgroups("Type", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat( \cmidrule(lr){@span})) ///
nonumber booktabs f label collabels(none)
eststo clear
Run Code Online (Sandbox Code Playgroud)
这会产生:
&\multicolumn{2}{c}{Type} \\\cmidrule(lr){2-3}
&\multicolumn{1}{c}{Domestic}&\multicolumn{1}{c}{Foreign}\\
\midrule
Price & 6,072& 6,385\\
& (3,097)& (2,622)\\
Mileage (mpg) & 19.83& 24.77\\
& (4.74)& (6.61)\\
Weight (lbs.) & 3,317& 2,316\\
& (695)& (433)\\
Length (in.) & 196& 169\\
& (20)& (14)\\
\midrule
Observations & 52& 22\\
Run Code Online (Sandbox Code Playgroud)
我想得到它所以输出将在6,072和6,385之前有\ $
我看到有关Statalist关于图表的变通方法的一些讨论,但没有关于esttab的讨论.有人还提到创建"自定义格式",但我似乎无法在任何地方找到相关文档.
我曾经遇到过类似的问题:我想根据显着性级别对单元格进行不同的着色。最后,我能想到的最简单的自动化解决方案是修改 esttab 代码......事实上,这比听起来更容易。
在 中查找以下代码estout.ado:
if `:length local thevalue'<245 {
local thevalue: di `fmt_m' `"`macval(thevalue)'"'
}
Run Code Online (Sandbox Code Playgroud)
之后您可以插入,例如
local thevalue `"\$`macval(thevalue)'\$"'
Run Code Online (Sandbox Code Playgroud)
这会产生:
&\multicolumn{2}{c}{Type} \\\cmidrule(lr){2-3}
&\multicolumn{1}{c}{Domestic}&\multicolumn{1}{c}{Foreign}\\
\midrule
Price &$ 6,072$&$ 6,385$\\
&$ (3,097)$&$ (2,622)$\\
Mileage (mpg) &$ 19.83$&$ 24.77$\\
&$ (4.74)$&$ (6.61)$\\
Weight (lbs.) &$ 3,317$&$ 2,316$\\
&$ (695)$&$ (433)$\\
Length (in.) &$ 196$&$ 169$\\
&$ (20)$&$ (14)$\\
\midrule
Observations & 52& 22\\
Run Code Online (Sandbox Code Playgroud)
(不要忘记在program drop estout导出之前,以便 .ado 重新加载)
因此,主表中的所有数值都封装在 $ 符号中。如果您只需要特定值,则可以执行简单的正则表达式条件。例如,如果您只关心捕获那些有逗号的值(无论出于何种原因),您可以这样做:
if strpos("`macval(thevalue)'", ",") {
local thevalue `"\$`macval(thevalue)'\$"'
}
Run Code Online (Sandbox Code Playgroud)
你还可以添加你自己的选项(就在estout.ado的开头),这样修改的行为就不会一直被触发。