vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
912 stars 77 forks source link

Latex: `>` symbol in footnote not correctly displayed #798

Closed etiennebacher closed 3 months ago

etiennebacher commented 3 months ago

Close to #793 but even with the Github version I have this escaping problem.

library(modelsummary)

models <- list("OLS 1" = lm(Sepal.Length ~ Petal.Length, data = iris))

modelsummary(models, output = "latex", stars = TRUE)
\begin{table}
\centering
\begin{talltblr}[         %% tabularray outer open
entry=none,label=none,
note{}={+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001},
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]},
column{1}={halign=l,},
column{2}={halign=c,},
hline{6}={1,2}{solid, 0.05em, black},
}                     %% tabularray inner close
\toprule
& OLS 1 \\ \midrule %% TinyTableHeader
(Intercept)  & \num{4.307}*** \\
& (\num{0.078})  \\
Petal.Length & \num{0.409}*** \\
& (\num{0.019})  \\
Num.Obs.     & \num{150}      \\
R2           & \num{0.760}    \\
R2 Adj.      & \num{0.758}    \\
AIC          & \num{160.0}    \\
BIC          & \num{169.1}    \\
Log.Lik.     & \num{-77.020}  \\
F            & \num{468.550}  \\
RMSE         & \num{0.40}     \\
\bottomrule
\end{talltblr}
\end{table}

But if I put this in a .tex file (with the packages printed in the warning of modelsummary() that I didn't show here):

\documentclass{article}

\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage{codehigh}
\usepackage[normalem]{ulem}
\UseTblrLibrary{booktabs}
\UseTblrLibrary{siunitx}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}

\begin{document}

\begin{table}
\centering
\begin{talltblr}[         %% tabularray outer open
entry=none,label=none,
note{}={+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001},
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]},
column{1}={halign=l,},
column{2}={halign=c,},
hline{6}={1,2}{solid, 0.05em, black},
}                     %% tabularray inner close
\toprule
& OLS 1 \\ \midrule %% TinyTableHeader
(Intercept)  & \num{4.307}*** \\
& (\num{0.078})  \\
Petal.Length & \num{0.409}*** \\
& (\num{0.019})  \\
Num.Obs.     & \num{150}      \\
R2           & \num{0.760}    \\
R2 Adj.      & \num{0.758}    \\
AIC          & \num{160.0}    \\
BIC          & \num{169.1}    \\
Log.Lik.     & \num{-77.020}  \\
F            & \num{468.550}  \\
RMSE         & \num{0.40}     \\
\bottomrule
\end{talltblr}
\end{table} 

\end{document}

image


packageVersion("modelsummary")
#> [1] '2.1.1.5'
packageVersion("tinytable")
#> [1] '0.3.0.33'
etiennebacher commented 3 months ago

It is correctly displayed if I use XeLaTeX as compiler but this is not the default in Overleaf and I think it should work out of the box with the default pdflatex. Alternatively, a warning about that would be nice.

vincentarelbundock commented 3 months ago

Thanks for the report. This is tricky.

To escape < we should probably convert it to \textless or $<$ or \num{<}. First problem is we don't want nested $$<$$ or \num{\num{<}}. Second problem is that escaping in this way will break tables like this one:

model <- lm(Sepal.Width ~ Sepal.Length, data = iris)
modelsummary(model, statistic = "p.value", output = "latex")

because that table includes cells with \num{<0.001}. Note that this doesn't work: \num{\textless 0.001}, and neither does this:\num{\text{\textless} 0.001}

Also need to be mindful of the possibility of:

options(modelsummary_format_numeric_latex = “mathmode”)

Although I guess people will not want to escape there anyway, so will set escape=FALSE explicitly.

Finally, note that the actual escaping is delegated to tinytable::format_tt(x, escape="latex")

etiennebacher commented 3 months ago

Yes, I understand that this is tricky and there are many corner cases. But at the same time, I think many people set stars = TRUE and just want a simple table without having to tweak the output manually.

Can't you escape the text in note{}={+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001} as soon as stars = TRUE (or rather is not FALSE)?

Also need to be mindful of the possibility of: options(modelsummary_format_numeric_latex = "mathmode")

Setting this option and using escape = FALSE doesn't fix the issue:

image

vincentarelbundock commented 3 months ago

Oh yes, I agree. Just trying to list all the corner cases to figure out what the best solution is.

vincentarelbundock commented 3 months ago

Thanks for the report. Should be fixed after installing the latest development versions of both tinytable and modelsummary.

Then, restart your R session completely.

Let me know if it's still broken.

Thanks for the report!

etiennebacher commented 3 months ago

Works for me now, thanks!