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.
Looking at the table code you posted in your previous question, I'll give some explanation of it, and how it relates to LyX.
\begin{table}[ht]
\caption{Nonlinear Model Results}
\centering
\begin{tabular}{c c c c}
\hline\hline
Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]
\hline
1 & 50& 837 & 970 \\
2 & 47 & 877 & 230 \\
3 & 31 & 25 & 415 \\
4 & 35 & 144 & 2356 \\
5 & 45 & 300 & 556 \\ [1ex]
\hline
\end{tabular}
\label{table:nonlin}
\end{table}
The table
environment, i.e.
\begin{table}[ht]
...
\end{table}
This is a float, and it is what you get when you do Insert --> Float --> Table in LyX. The optional argument, i.e. [ht]
is the placement specifier, and equivalent to choosing Here if possible and Top of page in the float settings.
The caption, i.e. \caption{...}
. This is the same as the caption-box that is placed in floats in LyX by default. You can also get it in LyX with Insert --> Caption.
Centering of the content of the float, i.e.
\centering
This is (almost) the same as changing the paragraph settings inside the table float to centered alignment, via right-click --> Paragraph settings.
The difference is that LyX inserts a centering
environment, instead of the \centering
switch.
The tabular
enviroment, i.e.
\begin{tabular}{c c c c}
...
\end{tabular}
This is what actually produces your table, and is what you get when doing Insert --> Table in LyX.
The label, i.e. \label{table:nonlin}
. This is the same as labels in LyX, so you can insert this with Insert --> Label.
The point
If you would like to edit your tables such as this with code rather than the LyX GUI, you really don't need anything more than the tabular
environment in an ERT, the rest can just as well be added via the GUI. That could also make cross-referencing easier, as the label will be listed in the GUI. For example as seen in the screenshot below:
You don't have to use the GUI though, you could just have the complete code as above in an ERT. Just don't place it inside a second float, which won't work, and doesn't make sense.
Finally, note that floating environments have nothing to do with the scaling of their contents, so having your table in a float won't help you change its height.
Best Answer
There are several ways to import tabular data into SWP. There is an easy and fast technique for the code you posted. Enter a 4x3 floating table fragment in the SWP editor. (Move the caption and marker fields to the front of the 4x3 tabular fields if you want the captions to typeset before the table.) Copy the entire tabular (not the table) environment. Delete the tabular 4x3 tabular input fields in the SWP editor and paste the code you copied into a tex field located in the position of the deleted tabular fields. Do not encapsulate this field. Close the document and then re-open it. Use copy and past to update the caption. Add the \def command to the B tex field on a new line after the begin{figure} line. While in this field you can change [tbp] to [htbp].