Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for threeparttable #146

Open
avsaase opened this issue Apr 23, 2020 · 4 comments
Open

Add support for threeparttable #146

avsaase opened this issue Apr 23, 2020 · 4 comments

Comments

@avsaase
Copy link

avsaase commented Apr 23, 2020

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.

leifeld added a commit that referenced this issue May 8, 2020
@leifeld
Copy link
Owner

leifeld commented May 8, 2020

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}

@avsaase
Copy link
Author

avsaase commented Sep 9, 2020

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.

@geoffreycastillo
Copy link

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.

@leifeld
Copy link
Owner

leifeld commented Mar 10, 2022

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 leifeld reopened this Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants