在 header 中插入图片时出现问题

Issue inserting image in header

我正在使用多个 R Markdown 文件编写报告,为了便于组合它们,我决定使用 bookdown 包。我在使用 fancyhdr LaTeX 包插入徽标作为 header 时遇到问题,在常规 .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}

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

我想这与 book-option 有关 - 这里有人知道更多吗?我查阅了 https://bookdown.org/yihui/bookdown/、他们的 github-repo 以及 Whosebug 和 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 

感谢您的任何建议。

您确定图片没有被推离页面顶部吗?没有你的完整目录,我无法重现你的问题,但看起来你没有在 geometry 参数中指定足够大的 top 值。查看 geometry LaTeX 函数的文档,您可以看到这指定了 body 顶部和纸张边缘之间的距离,没有为您的 header.

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

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}

编辑: 尽管重新加载了所有内容,但我似乎在系统中遇到了某种错误。我遵循了 Michael Harper 的建议,但在一个全新的 project/folder 中打开了代码,然后我设置 documentclass: article 手动插入了一个标题页。现在剩下的是:让 headers 在目录中工作并删除页码(页脚,居中)。

Index.Rmd:

---
documentclass: article
classoption: openany
site: bookdown::bookdown_site
fontsize: 12pt
linestretch: 1.25
link-citations: yes
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
      before_body: before_body.tex
    latex_engine: xelatex
    toc: yes
    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()
```

preamble.tex:

% loading necessary LateX-packages
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{fontspec}
\setmainfont{Arial}

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

% set page geometry
\geometry{a4paper, total={170mm,257mm}, left=20mm, top=40mm}

% make the word "Chapter" disappear - only display the chapters name
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

% Following below
% turns maketitle off, then you can define your own titlepage in before_body.tex
\let\oldmaketitle\maketitle 
\AtBeginDocument{\let\maketitle\relax}

before_body.tex:

% titlepage
\thispagestyle{empty}
\begin{center}
{\Huge A BOOK}
\linebreak
\includegraphics{cover.png}
\linebreak
{\huge by Me}
\end{center}

\let\maketitle\oldmaketitle
\maketitle