编织到PDF时,knitr中代码输出块的中心对齐存在问题。
例如,如果我运行以下命令:
---
title: "TRIAL"
output: pdf_document
header-includes:
- \usepackage{sectsty}
- \allsectionsfont{\color{cyan}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\setlength{\leftskip}{7cm}
## R Markdown
\setlength{\leftskip}{0cm}
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
```{r cars}
options(width = 80)
summary(cars)
```
我希望摘要(cars)的所有输出都居中对齐。
由于您的输出属于table
类,因此您可以使用以下解决方法(注意安装两个库kableExtra
和knitr
):
library(kableExtra)
library(knitr)
kable_styling(kable(summary(cars)), position = "center")
此外,我强烈建议以下PDF:https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
如果要居中文本:
\begin{center}
centered text
\end{center}