Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Centering column headers using estpost tabulate (twoway) in LaTeX table
From
annoporci <[email protected]>
To
[email protected]
Subject
Re: st: Centering column headers using estpost tabulate (twoway) in LaTeX table
Date
Sun, 20 Jan 2013 06:43:45 +0800
Dear Bert,
It's good that you've got a workaround. In the meantime I was playing
around with some suggestions by Jorg Weber in his blog:
http://www.jwe.cc/2012/03/stata-latex-tables-estout/
I quickly adapted and simplified some of his code to get an example to
work with the auto dataset. It has the sort of alignment you were looking
for, albeit for a different type of Stata commands, and may prove useful
at some future time, for you or for me!
I include it below for the record.
Patrick.
Stata Code Below
------------------------------
clear all
set more off
sysuse auto
qui probit foreign weight mpg headroom trunk
predict pr1, pr
quietly su pr1
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store A
qui probit foreign weight mpg headroom length
predict pr2, pr
quietly su pr2
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store B
qui probit foreign weight mpg headroom weight
predict pr2_disp, pr
quietly su pr2_disp
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store C
esttab A B C using table4.tex, replace f ///
label booktabs b(3) p(3) eqlabels(none) alignment(S S)
collabels("\multicolumn{1}{c}{$\beta$ / SE}" "\multicolumn{1}{c}{Mfx}") ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star) margins_b(star)" "se(fmt(3)par)") ///
stats(N r2_p chi2 p pr, fmt(0 3) layout("\multicolumn{1}{c}{@}"
"\multicolumn{1}{S}{@}") labels(`"Observations"' `"Pseudo \(R^{2}\)"' `"LR
chi2"' `"Prob > chi2"' `"Baseline predicted probability"'))
------------------------------
LaTeX Code Below
------------------------------------------------------------
\documentclass{article}%
\usepackage[centering]{geometry}%
\usepackage{booktabs}%
\usepackage{caption}%
\captionsetup{%
justification={justified}%
, singlelinecheck={false}%
}%
\newcommand{\sym}[1]{\rlap{#1}}%
\newcommand{\esttabinput}[3]{%
\renewcommand{\arraystretch}{1.2}%
\begin{minipage}[b]{\textwidth}%
\centering%
\begin{tabular}{l*{#2}{#3}}%
\toprule%
\input{#1}%
\bottomrule%
\addlinespace[.75ex]%
\end{tabular}%
\end{minipage}%
\renewcommand{\arraystretch}{1}%
}%
% Allow line breaks with \\ in specialcells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}%
% Statistical significance notes
\newcommand{\starnote}{%
$^{*}$ $p < 0.1$, $^{**}$ $p < 0.05$, $^{***}$ $p < 0.01$. %
Standard errors in parentheses. }%
\usepackage{siunitx}%
\sisetup{%
detect-mode%
, tight-spacing = {true}%
, group-digits = {false}%
, input-signs = {}%
, input-symbols ={( ) [ ] - + *}%
, input-open-uncertainty = {}%
, input-close-uncertainty = {}%
, table-align-text-post = {false}%
}%
\begin{document}
\begin{table}
\caption{Probit Model for Something or Other}
\esttabinput{table4}{3}{S[table-format=4.4]S[table-format=4.4]}
\parbox{\linewidth}{\starnote}%
\caption*{Source~:~Authors' calculations. blabla, blebleble, blublublu,
blobloblo.}%
% \caption*, use a star to omit automatic numbering
% use caption package to control alignments
\end{table}
\end{document}
------------------------------------------------------------
On Sun, 20 Jan 2013 06:03:35 +0800, Bert Lloyd <[email protected]>
wrote:
Patrick,
Thank you for your suggestion. I couldn't use it directly, since
layout() appears to be a suboption only for stats(), but it led me to
something that worked (see below).
I am posting the code for others' benefit but should emphasize that I
am in well over my head with LaTeX here, so this may be "working" only
by fluke (e.g. is it correct to use @span twice to refer to different
sets of columns?).
Stata code:
set more off
sysuse auto
local rowvar_label : variable label rep78
local colvar_label : variable label foreign
estpost tabulate rep78 foreign
esttab using esttab-example-corrected.tex, booktabs replace ///
cell("b rowpct(fmt(2))") collabels("N" "Row Pct") ///
unstack nonote noobs nonumber ///
eqlabels(, ///
lhs("`rowvar_label'") ///
span prefix(\multicolumn{@span}{c}{) suffix(}) ///
) ///
nomtitles ///
mgroups("`colvar_label'", pattern(1 0 0) ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cmidrule(lr){@span}))
Yields tex code:
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{3}{cc}}
\toprule
&\multicolumn{6}{c}{Car type}
\\\cmidrule(lr){2-7}
Repair Record
1978&\multicolumn{2}{c}{Domestic}&\multicolumn{2}{c}{Foreign}&\multicolumn{2}{c}{Total}\\
& N& Row Pct& N& Row Pct&
N& Row Pct\\
\midrule
1 & 2& 100.00& 0& 0.00&
2& 100.00\\
2 & 8& 100.00& 0& 0.00&
8& 100.00\\
3 & 27& 90.00& 3& 10.00&
30& 100.00\\
4 & 9& 50.00& 9& 50.00&
18& 100.00\\
5 & 2& 18.18& 9& 81.82&
11& 100.00\\
Total & 48& 69.57& 21& 30.43&
69& 100.00\\
\bottomrule
\end{tabular}
}
As PDF:
https://dl.dropbox.com/u/2937392/esttab-twoway-tabulate-example-corrected.pdf
On Sat, Jan 19, 2013 at 3:53 PM, annoporci <[email protected]> wrote:
I think you want to override the automatic alignment. I think you may be
able to achieve that by adding: layout("\multicolumn{1{c}{@}")
That and much more is discussed here:
http://tex.stackexchange.com/questions/48966/esttab-overriding-decimal-alignment
See also this:
http://www.jwe.cc/2012/03/stata-latex-tables-estout/
---
Patrick Toche.
On Sun, 20 Jan 2013 01:11:28 +0800, Bert Lloyd <[email protected]>
wrote:
Dear Statalist,
I am trying to create a simple twoway tabulation table in LaTeX using
the excellent user-written esttab command (version 2.0.5, part of the
estout package).
I am reporting both observation counts and row percentages, with the
value labels as column headers. Everything looks great using the code
below, but I would like to make one minor modification: center the
column headers between the observation count and row percent columns.
That is, currently the column header ("Domestic", "Foreign", "Total")
appears directly above the observation count column, I would like to
shift these slightly to the right so that the span both the
observation count and row percent columns. An example of what I would
like to do is found in Table 1 of the tabout tutorial, page 13 of
http://www.ianwatson.com.au/stata/tabout_tutorial.pdf.
Any advice?
Best,
BL
Stata code:
set more off
estimates clear
sysuse auto
local rowvar_label : variable label rep78
local colvar_label : variable label foreign
estpost tabulate rep78 foreign
esttab using esttab-example.tex, booktabs replace ///
cell("b rowpct(fmt(2))") collabels("N" "Row Pct") ///
unstack nonote noobs nonumber ///
eqlabels(, lhs("`rowvar_label'")) ///
nomtitles ///
mgroups("`colvar_label'", pattern(1 0 0) ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cmidrule(lr){@span}))
Generated TeX code:
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{3}{cc}}
\toprule
&\multicolumn{6}{c}{Car type}
\\\cmidrule(lr){2-7}
Repair Record 1978& Domestic& & Foreign&
& Total& \\
& N& Row Pct& N& Row Pct&
N& Row Pct\\
\midrule
1 & 2& 100.00& 0& 0.00&
2& 100.00\\
2 & 8& 100.00& 0& 0.00&
8& 100.00\\
3 & 27& 90.00& 3& 10.00&
30& 100.00\\
4 & 9& 50.00& 9& 50.00&
18& 100.00\\
5 & 2& 18.18& 9& 81.82&
11& 100.00\\
Total & 48& 69.57& 21& 30.43&
69& 100.00\\
\bottomrule
\end{tabular}
}
pdf table:
https://dl.dropbox.com/u/2937392/esttab-twoway-tabulate-example.pdf
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
--
Patrick Toche.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
--
Patrick Toche.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/