r - 在标题中插入图像时出现问题

标签 r r-markdown bookdown

我正在使用多个 R Markdown 文件编写报告,为了便于组合它们,我决定使用 bookdown 包。我在使用 fancyhdr LaTeX 包插入 Logo 作为标题时遇到问题,该包在常规 .Rmd 文件中运行良好。

index.Rmd 文件:

---
documentclass: book
classoption: openany
site: bookdown::bookdown_site
subparagraph: true
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
    toc_depth: 4
    latex_engine: xelatex # to receive Arial as font --> install.packages("xelatex") works for R users
link-citations: yes
fontsize: 12pt
linestretch: 1.25
---

preamble.tex 是:

\usepackage[german]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{fancyhdr}

\usepackage{fontspec}
\setmainfont{Arial}

\pagestyle{fancy}\setlength\headheight{100pt}
\fancyhead[L]{\includegraphics[width=456px]{INS_logo.png}}
\fancyhead[R]{\includegraphics[width=4.1cm]{shurp-2018-logo-blue-transparent.png}}
\renewcommand{\headrulewidth}{0pt}
\rfoot{Seite \thepage \hspace{1pt} von \pageref{LastPage}}

\geometry{a4paper, total={170mm,257mm}, left=20mm, top=10mm, }

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

一切正常,只是必要的 Logo 没有出现在标题中。在纯 pdf_document 格式中使用相同的代码效果很好。

我想这与书籍选项有关 - 有人知道更多吗?我查阅了 https://bookdown.org/yihui/bookdown/ 、他们的 github-repo 以及 StackOverflow 和 fancyhdr 上的文档。

我的 session 信息(R Studio 1.2.1070):

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.5.1  backports_1.1.2 bookdown_0.7    rprojroot_1.3-2 htmltools_0.3.6 tools_3.5.1     yaml_2.2.0     
 [8] Rcpp_0.12.19.3  rmarkdown_1.10  knitr_1.20      xfun_0.4        digest_0.6.18   evaluate_0.12 

感谢您的任何建议。

最佳答案

您确定图像没有被推离页面顶部吗?如果没有完整的目录,我无法重现您的问题,但看起来您没有在几何参数中指定足够大的 top 值。查看 geometry 的文档LaTeX 函数,您可以看到这指定了正文顶部与纸张边缘之间的距离,不为页眉留出空间。

enter image description here

这是我的可重现的示例。它生成一个虚拟图 plot.png,用于 Logo 占位符

index.Rmd:

---
documentclass: book
classoption: openany
fontsize: 12pt
linestretch: 1.25
link-citations: yes
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
    latex_engine: xelatex
    toc_depth: 4
subparagraph: yes
---

```{r setup, include=FALSE}
# Create an image to attach
png(filename = "logo.png", width = 200, height = 150, units = "px")
plot(cars)
box('figure', col = 'red')
dev.off()
```

# Title 1

Text

\newpage

## Section

Text

\newpage

同一目录中有preamble.tex。注意调整后的几何参数top=50mm:

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{fontspec}
\setmainfont{Arial}

\pagestyle{fancy}\setlength\headheight{100pt}
\fancyhead[L]{\includegraphics[width=4cm]{logo.png}}
\fancyhead[R]{\includegraphics[width=4.1cm]{logo.png}}
\renewcommand{\headrulewidth}{0pt}
\rfoot{Seite \thepage \hspace{1pt} von \pageref{LastPage}}

\geometry{a4paper, total={170mm,257mm}, left=20mm, top=50mm}

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

enter image description here

关于r - 在标题中插入图像时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53152698/

相关文章:

javascript - 在 slidify 中为第二级元素符号点设置动画

knitr - RMarkdown 与 Knit to HTML : How to increase section number?

r - 使用 gitbook 格式和样式创建单个独立的 html bookdown 文件

r - bookdown:自定义输出文件名

r - 使用 ggfortify 作为叠加层绘制多元时间序列时选择颜色

r - Lyx/knitr 错误

r - 检查具有不同功能的数据框(dplyr)

r - 使用 R 包 RefManageR 调整文本引用

r - 设置 R bookdown 输入目录

带 lapply 的稳健线性回归