【问题标题】:Bookdown/thesisdown/huskydown: change page margins for a single page within a documentBookdown/thesisdown/huskydown:更改文档中单页的页边距
【发布时间】:2020-06-30 11:31:20
【问题描述】:

我有一个用 Rmarkdown 编写的 huskydown 文档(基于 thesisdown/bookdown),它被编入 PDF/LaTex,但后来也编入 HTML。

对于文档中的单个页面,我想更改页边距(例如,包括一个非常大的表格/图形/外部 PDF)。 在此页面之后,页边距应恢复为默认值。

我尝试了什么:

---
author: 'Your R. Name'
date: 'May 20xx'
institution: 'Reed College'
division: 'Mathematics and Natural Sciences'
advisor: 'Advisor F. Name'
# If you have more two advisors, un-silence line 7
#altadvisor: 'Your Other Advisor'
department: 'Mathematics'
degree: 'Bachelor of Arts'
title: 'My Final College Paper'
knit: "bookdown::render_book"
site: bookdown::bookdown_site
output: 
 thesisdown::thesis_pdf: default
---

### Normal page margins again
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

\newpage
### Reduced page margins
```{r, echo = FALSE, results = "asis"}
cat("\\newgeometry{left=1cm,right=1cm,top=1cm,bottom=1cm}")
```

```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

```{r, echo = FALSE, results = "asis"}
cat("\\restoregeometry")
```

\clearpage
Restore the page margins

### Normal page margins again
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

# ! Undefined control sequence.
# l.190 \newgeometry
#                   {left=1cm,right=1cm,top=1cm,bottom=1cm} 

我也试过

\newgeometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
<!-- material for this page -->
\clearpage
\restoregeometry

正如@bretauv 所建议的那样。它适用于一个简单的 Rmd 文件,但不适用于我的 huskydown 用例。我最终得到了同样的错误消息。

【问题讨论】:

  • 嗨,你检查here了吗?
  • @bretauv 感谢您的链接和您的建议!对我来说,修复只适用于一个简单的 Rmd 文件,但不适用于 thesisdown/bookdown/huskydown。将相应地更新我的 Q。

标签: r r-markdown bookdown


【解决方案1】:

正如@bretauv 在对您问题的评论中所暗示的那样,这个问题并不是真正来自 rmarkdown 或 bookdown。这是一个 LaTeX 错误,很容易解决。

背景

您似乎知道,几何包中的命令可用于调整 LaTeX 文档中的布局(即尤其是页边距)。基本上,可以在 rmarkdown 文档中使用这样的 LaTeX 命令。但是,先决条件是加载了相应的 LaTeX 包。这不是这里的情况,因此错误

! Undefined control sequence.
l.190 \newgeometry

表示找不到命令\newgeometry

解决方案

LaTeX 中的包在标题中使用命令\usepackage{&lt;pkg_name&gt;} 加载。此命令不能简单地在 rmakdown 文档中使用,它必须在一个单独的模板中设置,告诉pandoc 如何从 .Rmd 文件中创建 PDF结束。对于huskydown / thesisdown,这是项目目录中的文件 template.tex

打开文件并通过在之前 \begin{document}添加以下行来确保已加载包,然后保存文件:

\usepackage[left=6cm,right=6cm,top=6cm,bottom=6cm]{geometry}

这将设置一些默认的边距和间距。我选择了一些(相当引人注目的)值,您当然可以根据自己的喜好进行调整。

编织你的 .Rmd 应该可以工作,你现在应该看到\newgeometry{}\restoregeometry 的效果。另外,请注意,没有必要用代码块来隔离这些。

示例

(使用thesisdown

### Default (reduced) page margins as defined by geometry    
```{r, echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point()
```
 
\newpage
 
\newgeometry{left=3cm,top=6cm,right=3cm,bottom=6cm}
 
### Larger margins
 
```{r, echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point()
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多