You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加align=TRUE后Stargazer生成PDF报错的技术求助

Fixing LaTeX Compile Error When Adding align=TRUE to Stargazer

Problem Overview

You're trying to generate a LaTeX-formatted logistic regression table with Stargazer, and your original code works perfectly for PDF output. But once you add the align=TRUE parameter to stargazer(), you hit this error:

! LaTeX Error: Illegal character in array arg.

Here's your working original code for reference:

---
title: "Untitled"
author: "Kenta Okuyama"
date: "2018年5月10日"
output:
  pdf_document:
    latex_engine: xelatex
---

## Table2

```{r,results='asis', echo=FALSE, message=FALSE, warning=FALSE}
library(stargazer)
load("C:/Users/kenta/Documents/R_Pojects/HealthPlace/.RData")
MultilogitSlowCon<- glm(hbp_treat~E2FCASlowPHC_m+gender+age+BMI+smoke_bi+drink_bi+incomeMean,data=dflogisticLW,subset=age>=30,family = "binomial"(link="logit"))
stargazer(MultilogitSlowCon,title="E2FCA Slow decay (Continuous)",dep.var.labels=c("Uncontrolled hypertension"),ci=TRUE,ci.level=0.95,single.row=TRUE)
## Root Cause
The conflict comes from combining `align=TRUE` with `single.row=TRUE` in Stargazer. When both are enabled, Stargazer generates LaTeX table code with invalid characters in the column alignment specification (like malformed array arguments), which XeLaTeX can't parse. The Chinese language environment with XeLaTeX might also exacerbate this compatibility issue.

## Solutions
Try these fixes in order of simplicity:

### 1. Remove `single.row=TRUE`
The most straightforward fix is to drop the `single.row=TRUE` parameter, as it's the main source of the formatting conflict with `align=TRUE`:
```r
stargazer(MultilogitSlowCon,
          title="E2FCA Slow decay (Continuous)",
          dep.var.labels=c("Uncontrolled hypertension"),
          ci=TRUE, ci.level=0.95,
          align=TRUE)

If you need the single-row layout for coefficients and confidence intervals, you can manually tweak the generated LaTeX code later, or try the next option.

2. Manually Correct the Generated LaTeX Code

If you must keep single.row=TRUE, first adjust your R chunk to output the raw LaTeX code instead of rendering it directly: change results='asis' to results='markup'. Run the chunk, copy the LaTeX table code, then fix the column alignment line (it'll look something like \begin{tabular}{l*{x}{c}} — replace it with a valid format like \begin{tabular}{lccccccc} where the number of c matches your column count). Then paste this corrected code into your R Markdown document as raw LaTeX.

3. Switch to xtable as an Alternative

If Stargazer's parameter conflicts are too stubborn, the xtable package is a reliable alternative that plays nicer with XeLaTeX and Chinese environments. Here's how to replicate your table with it:

library(xtable)
# Fit your model as before
MultilogitSlowCon<- glm(hbp_treat~E2FCASlowPHC_m+gender+age+BMI+smoke_bi+drink_bi+incomeMean,data=dflogisticLW,subset=age>=30,family = binomial(link="logit"))
# Generate and print the table
print(xtable(MultilogitSlowCon, caption="E2FCA Slow decay (Continuous)"),
      caption.placement = "top",
      include.rownames = TRUE,
      align = "lccccccc") # Adjust the number of 'c' to match your columns

4. Ensure Required LaTeX Packages Are Installed

Stargazer relies on LaTeX packages like booktabs and array for proper formatting. If these are missing, you might get unexpected errors. If you're using TinyTeX, install them with:

tinytex::tlmgr_install(c("booktabs", "array"))

Verification

After applying any of these fixes, recompile your PDF. If the Illegal character in array arg error is gone and your table renders correctly, you're good to go. Tweak the formatting as needed to match your original desired layout.

内容的提问来源于stack exchange,提问作者Kenta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:50:14