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

tob*_*lab 5 r r-markdown bookdown

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

索引.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
---
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

除了必要的徽标没有出现在标题中之外,一切正常。以纯格式使用相同的代码pdf_document效果很好。

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

我的会话信息(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 
Run Code Online (Sandbox Code Playgroud)

感谢您的任何建议。

Mic*_*per 3

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

在此输入图像描述

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

索引.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
Run Code Online (Sandbox Code Playgroud)

在同一目录中是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}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述