使用 knitr 从 xtable 行名称中换行

使用 knitr 从 xtable 行名称中换行

我正在使用knitr来生成包含我用 制作的表格的 PDF xtable。我该如何让这些表格中的文本换行?

这是我迄今为止最大的努力:

\documentclass{article}
\begin{document}

<<example>>=
Count = structure(c("0  (0%)", "1  (9%)", "1  (9%)", "1  (9%)", "2  (18%)", "0  (0%)", "0  (0%)", "0  (0%)", "3  (27%)", "7  (63%)", "4  (36%)", "6  (54%)", "6  (54%)", "3  (27%)", "4  (36%)", "8  (72%)", "6  (54%)", "2  (18%)", "6  (54%)", "2  (18%)", "2  (18%)", "1  (9%)", "3  (27%)", "3  (27%)", "2  (18%)", "1  (9%)", "0  (0%)", "2  (18%)", "1  (9%)", "5  (45%)", "3  (27%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "2  (18%)", "1  (9%)", "0  (0%)"), .Dim = c(8L, 5L), .Dimnames = structure(list(variable = c("Gaining access to information from scientists", "Gaining access to information from public officials/government agencies", "Understanding the scientific research", "Synthesizing complex information for a general audience", "Explaining scientific uncertainty", "Successfully pitching stories to supervisors", "Struggling for placement or prominence of stories", "Obtaining professional development/research skills"), value = c("Very Difficult", "Somewhat Difficult", "Neutral", "Somewhat Easy", "Very Easy")), .Names = c("variable", "value")), class = "table")

row.names(Count) = stringr::str_wrap(row.names(Count), width=50)
print(xtable::xtable(Count, caption='test', label='tab:example', align=c(rep('l|', ncol(Count)), 'l')))
@

\end{document}

string::str_wrap()最终不会影响表中的行。我该如何换行此文本,以便表格适合正常大小的页面,而无需使用scalebox参数print()

答案1

尝试这个:

<<results='asis',echo=FALSE>>=
# row.names(Count) = stringr::str_wrap(row.names(Count), width=50)

print(xtable(Count, caption='test', label='tab:example', align=c('p{1in}',rep('|c', ncol(Count)))),scalebox = 0.75)

@

唯一改变的是参数align。插入p{1in}表示第一列的宽度应为 1 英寸。其余的就不言自明了。

相关内容