寻求Rmd导出Beamer时coeftest输出格式化的简便knitr方法
Hey there! Let's break down how to format your coeftest() output in Beamer presentations using knitr, without the hassle of manual LaTeX environments.
1. Simplify Red Box Formatting with Knitr Hooks
Instead of manually wrapping coeftest() output in Verbatim environments, you can create a custom knitr hook to automate this. Here's how:
First, add the required LaTeX packages to your presentation's preamble. Create a preamble.tex file with:
\usepackage{fancyvrb} \usepackage{xcolor} \usepackage{graphicx} % For scaling later
Then, in your Rmd file's setup chunk, define the custom output hook:
library(knitr) library(lmtest) # Save the default output hook to fall back on default_output_hook <- knit_hooks$get("output") # Define custom hook for coeftest boxes knit_hooks$set(output = function(x, options) { # Check if the chunk has the `coeftest_box` flag set to TRUE if (!is.null(options$coeftest_box) && options$coeftest_box) { # Wrap output in red-framed Verbatim paste0( "\\begin{Verbatim}[frame=single,framerule=0.5pt,rulecolor=\\color{red}]\n", x, "\n\\end{Verbatim}" ) } else { # Use default output handling for other chunks default_output_hook(x, options) } })
Now, whenever you want to box a coeftest() output, just add coeftest_box=TRUE to your chunk options:
# Fit a sample model model <- lm(mpg ~ wt + hp, data = mtcars) # This output will get a red box automatically coeftest(model)
(Chunk options: {r, coeftest_box=TRUE})
2. Fixing Kable Errors with coeftest()
The error you see with kable() happens because coeftest() returns a specialized coeftest class object, not a standard data frame. The fix is simple: convert the result to a data frame first.
Here's a working example with kableExtra for nicer formatting in Beamer:
library(kableExtra) # Convert coeftest output to data frame coef_df <- as.data.frame(coeftest(model)) # Format with kable kable(coef_df, "latex", booktabs = TRUE, caption = "Coefficient Tests") %>% kable_styling(latex_options = c("striped", "hold_position"), full_width = FALSE) %>% column_spec(1, bold = TRUE) %>% add_header_above(c("Coefficient Estimates" = 4))
(Don't forget to add \usepackage{kableExtra} to your preamble.tex!)
3. Scaling Output Inside Boxes (with knitr::render() Context)
knitr::render() itself doesn't handle scaling directly, but you can extend the custom hook we made earlier to include LaTeX's \scalebox command. This will shrink the output while keeping it inside the red box.
Update the hook to include scaling:
knit_hooks$set(output = function(x, options) { if (!is.null(options$coeftest_box) && options$coeftest_box) { # Set a default scale if none is provided (e.g., 0.8 = 80% size) scale <- ifelse(is.null(options$scale), 0.8, options$scale) # Scale the output and wrap in red box scaled_content <- paste0("\\scalebox{", scale, "}{\n", x, "\n}") paste0( "\\begin{Verbatim}[frame=single,framerule=0.5pt,rulecolor=\\color{red}]\n", scaled_content, "\n\\end{Verbatim}" ) } else { default_output_hook(x, options) } })
Now you can control scaling via the chunk's scale option:
coeftest(model)
(Chunk options: {r, coeftest_box=TRUE, scale=0.75} for 75% size)
If you're using render() to generate the presentation programmatically, you can pass chunk options via params or set them globally in your setup chunk.
内容的提问来源于stack exchange,提问作者Michael

