You should be OK if only [p]
is specified as the positioning argument, because the contents of page of floats is always centered vertically.
If it's not possible to set up Stata so that it outputs \begin{figure}[p]
for this particular document, then adding the following code to the preamble should force it anyway:
\let\latextable\table
\renewcommand\table[1][]{\latextable[p]}
Note: if you can convince Stata to say
\def\sym#1{\ensuremath{^{#1}}
your document will be more robust. This is one of the few cases when \ensuremath
is really useful.
You were on the right track, but you did not follow the Jörg's instructions completely.
First make sure that you add Jörg's preamble into your document preamble (in LyX: Document > Settings > Latex-Preamble):
% Packages for tables
\usepackage{booktabs}% Pretty tables
\usepackage{threeparttablex}% For Notes below table
% *****************************************************************
% siunitx
% *****************************************************************
\newcommand{\sym}[1]{\rlap{#1}} % Thanks to Joseph Wright & David Carlisle
\usepackage{siunitx}
\sisetup{
detect-mode,
group-digits = false,
input-symbols = ( ) [ ] - +,
table-align-text-post = false,
input-signs = ,
}
% Character substitution that prints brackets and the minus symbol in text mode. Thanks to David Carlisle
\def\yyy{%
\bgroup\uccode`\~\expandafter`\string-%
\uppercase{\egroup\edef~{\noexpand\text{\llap{\textendash}\relax}}}%
\mathcode\expandafter`\string-"8000 }
\def\xxxl#1{%
\bgroup\uccode`\~\expandafter`\string#1%
\uppercase{\egroup\edef~{\noexpand\text{\noexpand\llap{\string#1}}}}%
\mathcode\expandafter`\string#1"8000 }
\def\xxxr#1{%
\bgroup\uccode`\~\expandafter`\string#1%
\uppercase{\egroup\edef~{\noexpand\text{\noexpand\rlap{\string#1}}}}%
\mathcode\expandafter`\string#1"8000 }
\def\textsymbols{\xxxl[\xxxr]\xxxl(\xxxr)\yyy}
% *****************************************************************
% Estout related things
% *****************************************************************
\let\estinput=\input % define a new input command so that we can still flatten the document
\newcommand{\estwide}[3]{
\vspace{.75ex}{
\textsymbols% Note the added command here
\begin{tabular*}
{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular*}
}
}
\newcommand{\estauto}[3]{
\vspace{.75ex}{
\textsymbols% Note the added command here
\begin{tabular}{l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular}
}
}
% Allow line breaks with \\ in specialcells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}
}
% *****************************************************************
% Custom subcaptions
% *****************************************************************
% Note/Source/Text after Tables
% The new approach using threeparttables to generate notes that are the exact width of the table.
\newcommand{\Figtext}[1]{%
\begin{tablenotes}[para,flushleft]
\hspace{6pt}
\hangindent=1.75em
#1
\end{tablenotes}
}
\newcommand{\Fignote}[1]{\Figtext{\emph{Note:~}~#1}}
\newcommand{\Figsource}[1]{\Figtext{\emph{Source:~}~#1}}
\newcommand{\Starnote}{\Figtext{* p < 0.1, ** p < 0.05, *** p < 0.01. Standard errors in parentheses.}}% Add significance note with \starnote
You said you did that but Jörg made some refinements in his follow-up posts. I took this from his sample document. Note that I included the threeparttablex
package instead of threeparttable
, since the former also supports longtable
(for tables spanning more than one page, should you need that). For this example, however, this does not matter.
e-birk is right that not everyone has access to Stata, but for those that do, it helps to add a MWE that makes use of the example datasets installed with Stata (help sysuse
). So I adjusted your example like this:
sysuse auto
regress mpg weight length foreign, robust // specification with 'foreign'
eststo C1
regress mpg weight length, robust // specification without 'foreign'
eststo C2
regress price weight length foreign, robust // specification with 'foreign'
eststo C3
regress price weight length, robust // specification without 'foreign'
eststo C4
Now export the table from Stata as a fragment and without the table notes (we add these later into the LaTeX/LyX document). The right esttab
command should be something like this:
esttab C1 C2 C3 C4 using test_tex, booktabs fragment replace ///
se(%3.2f) b(3) label indicate(Controls=foreign) ///
star(* 0.1 ** 0.05 *** 0.01) nonotes nomtitles drop(_cons) ///
mgroups("mpg" "weight", pattern(1 0 1 0) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
Note that I added the fragment option. I also reformated the command a little. This way you can just add , ///
before using
in the first line to test your table in Stata first (e.g. to see whether labels are correct, etc.).
Finally, include the table in LyX with this (adjust accordingly):
\begin{table}\centering
\begin{threeparttable}
\caption{Table with Better Notes and Better Symbols}
\estauto{test_tex}{4}{c}
\Figtext{Some basic text about the table.}
\Fignote{Standard errors corrected for heteroskedasticity in brackets. \\
\textit{* p < 0.10, ** p < 0.5, *** p < 0.01}}
\Figsource{auto.dta (Stata example dataset)}
% \Starnote
\end{threeparttable}
\end{table}
You can insert this into your LyX document with Evil Red Text (Insert > Tex-Code) or use a .tex document and insert that as a file into your document.
I added the p-value note directly, but you could also use the \Starnote
command (useful if you have multiple tables with the same p-symbols). Adjust the p-values on the last line of the preamble accordingly.
I also used the center alignment {c}
in \estauto{test_tex}{4}{c}
instead of the decimal alignment {S}
, since the latter seems to choke on the Controls row. I am not very familiar with the siunitx
package, so I might be wrong here or there might be an easy fix.
Obviously, this is mostly Jörg's code, he deserves the credit.
Best Answer
This answer is meant to address the follow-up question in the comments section on "is there a method to scale the table down without touching the code in the /input section? 'tabular*' is generated automatically by the Stata export command". It may or may not be compatible with what the OP is trying to accomplish.
First, let me reiterate the disclaimers:
1) The table is poorly conceptualized, because of the excessive width of the column headers;
2) if one wishes to scale a table, one should use
tabular
, rather thantabular*
because thetabular*
has no idea, in advance, if the table is oversized, and will not provide an acceptable output is scaling is required;3) Any scaling can only be done on the inner
tabular
part of the table, and not on thetable
itself, becausetable
is a float and cannot be scaled as an object.That said, what I do here is wrap the tabular in a macro that I call
\totextwidth
which will scale thetabular
(either scale down or leave alone) to fit\textwidth
. I use the floating pointfp
package to calculate the scale factor, and use\scalebox
of thegraphicx
package to resize thetabular
. It uses an egreg trick to place lengths into count registers.I have verified that this approach works if you stick the part between
\begin{table}
and\end{table}
into a separate file and use\input
. EDITED to not scale narrow tables up to\textwidth
, but only to scale wide tables down to\textwidth
.