如何使用Knitr pdf输出将代码输出(文本)居中到页面中间?

问题描述 投票:0回答:1

编织到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)的所有输出都居中对齐。

r alignment r-markdown markdown center
1个回答
0
投票

由于您的输出属于table类,因此您可以使用以下解决方法(注意安装两个库kableExtraknitr):

library(kableExtra)
library(knitr)

kable_styling(kable(summary(cars)), position = "center")

enter image description here

此外,我强烈建议以下PDF:https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

如果要居中文本:

\begin{center}
 centered text
\end{center}
© www.soinside.com 2019 - 2024. All rights reserved.