texreg icon indicating copy to clipboard operation
texreg copied to clipboard

Add support for threeparttable

Open avsaase opened this issue 4 years ago • 4 comments

Is it possible to add support for threeparttable, with options to set the tablenotes? I think this provides a nicer solution than adding the notes in a \multicolumn.

avsaase avatar Apr 23 '20 08:04 avsaase

Thanks for the suggestion! I wasn't aware of the threeparttable package. I have now implemented it in the latest commit. When longtable = TRUE is set, then the threeparttablex is used instead of threeparttable. Here are some examples:

Basic example (note that the \usepackage line can be omitted using use.packages = FALSE):

library("texreg")
data("iris")
model1 <- lm(Sepal.Width ~ Petal.Width, data = iris)
texreg(list(model1, model1), threeparttable = TRUE)

Result:

\usepackage{threeparttable}

\begin{table}
\begin{center}
\begin{threeparttable}
\begin{tabular}{l c c}
\hline
 & Model 1 & Model 2 \\
\hline
(Intercept) & $3.31^{***}$  & $3.31^{***}$  \\
            & $(0.06)$      & $(0.06)$      \\
Petal.Width & $-0.21^{***}$ & $-0.21^{***}$ \\
            & $(0.04)$      & $(0.04)$      \\
\hline
R$^2$       & $0.13$        & $0.13$        \\
Adj. R$^2$  & $0.13$        & $0.13$        \\
Num. obs.   & $150$         & $150$         \\
\hline
\end{tabular}
\begin{tablenotes}[flushleft]
\scriptsize{\item $^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}
\end{tablenotes}
\end{threeparttable}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

With a custom note over two lines (can also be done in a single line):

texreg(list(model1, model1), threeparttable = TRUE,
       custom.note = "\n\\item %stars.\\\\\n\\item Second line.\n")
\usepackage{threeparttable}

\begin{table}
\begin{center}
\begin{threeparttable}
\begin{tabular}{l c c}
\hline
 & Model 1 & Model 2 \\
\hline
(Intercept) & $3.31^{***}$  & $3.31^{***}$  \\
            & $(0.06)$      & $(0.06)$      \\
Petal.Width & $-0.21^{***}$ & $-0.21^{***}$ \\
            & $(0.04)$      & $(0.04)$      \\
\hline
R$^2$       & $0.13$        & $0.13$        \\
Adj. R$^2$  & $0.13$        & $0.13$        \\
Num. obs.   & $150$         & $150$         \\
\hline
\end{tabular}
\begin{tablenotes}[flushleft]
\scriptsize{
\item $^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$.\\
\item Second line.
}
\end{tablenotes}
\end{threeparttable}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

With longtable, caption.above, and dcolumn:

texreg(list(model1, model1), threeparttable = TRUE,
       longtable = TRUE, caption.above = TRUE, dcolumn = TRUE)
\begin{center}
\begin{ThreePartTable}
\begin{TableNotes}[flushleft]
\scriptsize{\item $^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}
\end{TableNotes}
\begin{longtable}{l D{.}{.}{3.5} D{.}{.}{3.5}}
\caption{Statistical models}
\label{table:coefficients}\\
\hline
 & \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
\hline
\endfirsthead
\hline
 & \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
\hline
\endhead
\hline
\endfoot
\hline
\insertTableNotes\\
\endlastfoot
(Intercept) & 3.31^{***}  & 3.31^{***}  \\
            & (0.06)      & (0.06)      \\
Petal.Width & -0.21^{***} & -0.21^{***} \\
            & (0.04)      & (0.04)      \\
\hline
R$^2$       & 0.13        & 0.13        \\
Adj. R$^2$  & 0.13        & 0.13        \\
Num. obs.   & 150         & 150         \\
\end{longtable}
\end{ThreePartTable}
\end{center}

leifeld avatar May 08 '20 18:05 leifeld

Thanks for implementing this! I am a bit late with responding but I just now got to testing it out.

I noticed two small problems:

  1. The table caption in placed outside of the threeparttable environment, which adds additional space between the table and the caption. The threeparttable documentation puts the caption within the threeparttable environment. In fact, I think the caption is the first "part" of the threeparttable..
  2. Combining threeparttable = TRUE and siunitx= TRUE throws an error that these packages cannot be used together. Why is that? I am pretty sure they are compatible on the LaTeX side since I have used them together in the part.

avsaase avatar Sep 09 '20 11:09 avsaase

Just encountered the same issue as @avsaase:

Combining threeparttable = TRUE and siunitx= TRUE throws an error that these packages cannot be used together. Why is that? I am pretty sure they are compatible on the LaTeX side since I have used them together in the part.

I'm always using threeparttable and siunitx together so I believe they are compatible.

geoffreycastillo avatar Mar 10 '22 14:03 geoffreycastillo

I'll reopen this for now. If anyone wants to work on this and start a pull request, please feel free to do so.

leifeld avatar Mar 10 '22 15:03 leifeld