admin管理员组文章数量:1431702
My work flow is R + Overleaf. So often times I need to generate regression table in R and print it out in overleaf. I recently came accross the the modelsummary
package which is super handy and powerful in doing so. But I stumble upon an issue and it took me an entire day without finding a proper solution, so here I am.
I generated a very large (too wide and too long) tex table using modelsummary directly and it can not be fit into one page when I compile it use overleaf. The common LaTeX fix is to use longtable
but it seems like .tex file generated by modelsummary
does not compatiable to the longtable
command. Then I turn to the kableExtra option to scale down the table and make the font size small. Which, if works, is the best to do it since I have hundreds of such .tex files and it would extremely inefficient if I need to change them manually one by one. But I can not get it work since the saved .tex file is either in html format (when output=kableExtra
is specified in the modelsummary
function) or is empty (when output=latex
is specified in the modelsummary
function).
Any help will be deeply appreciated and I think this will benefit those who share the same or similar workflow with me in great deal. Thanks so much in advance. Below is an minimum example to play around with.
# update.packages()
rm(list = ls())
library(kableExtra)
library(modelsummary)
library(tidyr)
options(knitr.table.format = "latex")
options("modelsummary_format_numeric_latex" = "plain")
url <- '.csv'
dat <- read.csv(url)
models <- list(
"OLS 1" = lm(Donations ~ Literacy + Clergy, data = dat),
"Poisson" = glm(Donations ~ Literacy + Commerce, family = poisson, data = dat),
"OLS 2" = lm(Crime_pers ~ Literacy + Clergy, data = dat)
)
# Generate modelsummary output as a kable object (for compatibility)
raw_output <- modelsummary(models = models, output = "kableExtra")
# Apply kableExtra styling to the kable object
styled_output <- raw_output %>%
kable_styling(font_size = 7, latex_options = c('scale_down', 'HOLD_position'))
# Save the styled LaTeX output to a file
kableExtra::save_kable(
styled_output,
file = "test.tex"
)
--------------------------EDIT----------------------- Based on Vincent's comment, I changed the code to the following:
raw_output <- modelsummary(models = models, output = "tinytable") |>
theme_tt("resize", width=0.8) |>
save_tt('test.tex', overwrite = TRUE)
And tried to compile the tex.file using the following environment in Overleaf:
\documentclass{article}
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage{codehigh}
\usepackage[normalem]{ulem}
\usepackage{tcolorbox}
\usepackage{adjustbox}
\usepackage{pdflscape}
\usepackage{subcaption}
\usepackage[skip=1ex,
font={small, sf},
labelfont=bf
]{caption} % new
\usepackage{tabularray}
\usepackage{tblr-extras}
\UseTblrLibrary{amsmath, booktabs,
caption, siunitx} % new
%\usepackage{amsmath} % Loaded by TblrLibrary
\usepackage{acronym}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}
\graphicspath{{./images}}
\begin{landscape}
\input{tables/test.tex}
\end{landscape}
Below is the first error:
Missing $ inserted.
You need to enclose all mathematical expressions and symbols with special markers. These special markers create a ‘math mode’.
Use $...$ for inline math mode, and \[...\]or one of the mathematical environments (e.g. equation) for display math mode.
This applies to symbols such as subscripts ( _ ), integrals ( \int ), Greek letters ( \alpha, \beta, \delta ) and modifiers (\vec{x}, \tilde{x}).
Learn more
This is usually a longtable
error.
My work flow is R + Overleaf. So often times I need to generate regression table in R and print it out in overleaf. I recently came accross the the modelsummary
package which is super handy and powerful in doing so. But I stumble upon an issue and it took me an entire day without finding a proper solution, so here I am.
I generated a very large (too wide and too long) tex table using modelsummary directly and it can not be fit into one page when I compile it use overleaf. The common LaTeX fix is to use longtable
but it seems like .tex file generated by modelsummary
does not compatiable to the longtable
command. Then I turn to the kableExtra option to scale down the table and make the font size small. Which, if works, is the best to do it since I have hundreds of such .tex files and it would extremely inefficient if I need to change them manually one by one. But I can not get it work since the saved .tex file is either in html format (when output=kableExtra
is specified in the modelsummary
function) or is empty (when output=latex
is specified in the modelsummary
function).
Any help will be deeply appreciated and I think this will benefit those who share the same or similar workflow with me in great deal. Thanks so much in advance. Below is an minimum example to play around with.
# update.packages()
rm(list = ls())
library(kableExtra)
library(modelsummary)
library(tidyr)
options(knitr.table.format = "latex")
options("modelsummary_format_numeric_latex" = "plain")
url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)
models <- list(
"OLS 1" = lm(Donations ~ Literacy + Clergy, data = dat),
"Poisson" = glm(Donations ~ Literacy + Commerce, family = poisson, data = dat),
"OLS 2" = lm(Crime_pers ~ Literacy + Clergy, data = dat)
)
# Generate modelsummary output as a kable object (for compatibility)
raw_output <- modelsummary(models = models, output = "kableExtra")
# Apply kableExtra styling to the kable object
styled_output <- raw_output %>%
kable_styling(font_size = 7, latex_options = c('scale_down', 'HOLD_position'))
# Save the styled LaTeX output to a file
kableExtra::save_kable(
styled_output,
file = "test.tex"
)
--------------------------EDIT----------------------- Based on Vincent's comment, I changed the code to the following:
raw_output <- modelsummary(models = models, output = "tinytable") |>
theme_tt("resize", width=0.8) |>
save_tt('test.tex', overwrite = TRUE)
And tried to compile the tex.file using the following environment in Overleaf:
\documentclass{article}
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage{codehigh}
\usepackage[normalem]{ulem}
\usepackage{tcolorbox}
\usepackage{adjustbox}
\usepackage{pdflscape}
\usepackage{subcaption}
\usepackage[skip=1ex,
font={small, sf},
labelfont=bf
]{caption} % new
\usepackage{tabularray}
\usepackage{tblr-extras}
\UseTblrLibrary{amsmath, booktabs,
caption, siunitx} % new
%\usepackage{amsmath} % Loaded by TblrLibrary
\usepackage{acronym}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}
\graphicspath{{./images}}
\begin{landscape}
\input{tables/test.tex}
\end{landscape}
Below is the first error:
Missing $ inserted.
You need to enclose all mathematical expressions and symbols with special markers. These special markers create a ‘math mode’.
Use $...$ for inline math mode, and \[...\]or one of the mathematical environments (e.g. equation) for display math mode.
This applies to symbols such as subscripts ( _ ), integrals ( \int ), Greek letters ( \alpha, \beta, \delta ) and modifiers (\vec{x}, \tilde{x}).
Learn more
This is usually a longtable
error.
1 Answer
Reset to default 0I don't use Overleaf, so I can't tell you if this will work there, but I don't see why not. This is how to modify your code to produce longtable
output. Note that it can't be resized by kableExtra
, so I've left out that option.
First you output a dataframe from modelsummary()
, then you format it in kableExtra
.
# Generate modelsummary output as a data.frame
raw_output <- modelsummary(models = models, output = "data.frame")
# Apply kableExtra styling to the kable object
styled_output <- raw_output %>%
kbl(format = "latex", longtable = TRUE, booktabs=TRUE) %>%
kable_styling(font_size = 7, latex_options = c('HOLD_position'))
本文标签: rUse kableExtra to manipulate and save tex file after modelsummaryStack Overflow
版权声明:本文标题:r - Use kableExtra to manipulate and save tex file after modelsummary - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745585319a2664866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
modelsummary
is atinytable
object. This package also has the ability to split tables across multiple pages and scale them. Look for "multipage" and "scale" in the "Themes" section of the PDF documentation: vincentarelbundock.github.io/tinytable/vignettes/… – Vincent Commented Nov 19, 2024 at 14:32longtable
directly in overleaf. @Vincent. I'm really looking forward a workaround here as this is kind of a dealbreaker for me andmodelsummary
is pretty amazing otherwise. – Jia Gao Commented Nov 22, 2024 at 5:27