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

时间:2018-11-05 10:38:02

标签: r r-markdown bookdown

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

一切正常,但必要的徽标未出现在标题中。以普通的pdf_document格式使用相同的代码会很好。

我想这与book-option有关-有人在这里了解更多吗?我查阅了https://bookdown.org/yihui/bookdown/,它们的github-repo以及StackOverflow以及 fancyhdr 上的文档。

我的sessioninfo(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 

谢谢您的任何建议。

2 个答案:

答案 0 :(得分:3)

您确定图像没有被推离页面顶部吗?如果没有完整的目录,我将无法重现您的问题,但似乎您没有在geometry参数中指定足够大的top值。查看geometry LaTeX函数的文档,您会发现它指定了主体顶部和纸张边缘之间的距离,而页眉没有任何空间。

enter image description here

这是我可复制的示例。它会生成一个伪图解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}

enter image description here

答案 1 :(得分:1)

编辑: 尽管重新加载了所有内容,但我似乎在系统中存在某种错误。我遵循了Michael Harper的建议,但是在一个全新的项目/文件夹中打开了代码,然后我手动设置了documentclass: article插入标题页。现在还剩下什么:使页眉在目录中起作用,并删除页码(页脚居中)。

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