R 在标题中插入图像的问题

R 在标题中插入图像的问题,r,r-markdown,bookdown,R,R Markdown,Bookdown,我正在使用多个R标记文件编写报告,为了便于合并它们,我决定使用bookdown包。我在使用fancyhdrLaTeX包插入徽标作为标题时遇到了一个问题,该包在常规.Rmd文件中运行良好 index.Rmd文件: --- documentclass: book classoption: openany site: bookdown::bookdown_site subparagraph: true output: bookdown::pdf_book: includes:

我正在使用多个R标记文件编写报告,为了便于合并它们,我决定使用bookdown包。我在使用fancyhdrLaTeX包插入徽标作为标题时遇到了一个问题,该包在常规
.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
---
序言.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\u文档
格式中使用相同的代码可以很好地工作

我想这与书的选择有关——这里有人知道更多吗?我查阅了他们的github回购协议、StackOverflow以及fancyhdr上的文档

我的会话信息(R Studio 1.2.1070):


谢谢您的建议。

您确定没有将图像从页面顶部推下吗?如果没有完整的目录,我无法重现您的问题,但您似乎没有在geometry参数中指定足够大的
top
值。查看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
同一目录中有序言.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的建议,但在一个全新的项目/文件夹中打开了代码,然后我设置了
documentclass:article
手动插入了一个标题页。现在剩下的是:让页眉在目录中工作并删除页码(页脚,居中)

指数.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()
```
序言.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}
在_body.tex之前:

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

\let\maketitle\oldmaketitle
\maketitle

您正在“px”中指定图像尺寸,如前所述,该尺寸只能由pdfTeX和LauTeX使用。我不认为这是你的问题的主要原因。我可以重建你的reprex,但只要我插入site:bookdown::bookdown_site(包括所有章节),fancyhdr定义的所有东西都会消失。“没有我的完整目录”是什么意思?我很乐意提供更多信息。我只是尝试添加
site:bookdown::bookdown\u site
,页眉显示在第3页和第4页,而不是第1页和第2页。这是你正在经历的行为吗?更改为
documentclass:article
将使其显示在所有页面上。是!但是,当我添加第二个只包含#titletest(表示章节)的文件.Rmd时,fancyhdr页眉/页脚规范消失了。编辑:更改为
documentclass:article
在reprex中工作,但不在原始文件中。是否调整了行
\geometry{a4paper,total={170mm,257mm},left=20mm,top=50mm}
?这是我复制你的错误的唯一方法,当我把它保留为你原来的样子。是的-我已经调整了一切-见我上面的编辑。
% titlepage
\thispagestyle{empty}
\begin{center}
{\Huge A BOOK}
\linebreak
\includegraphics{cover.png}
\linebreak
{\huge by Me}
\end{center}

\let\maketitle\oldmaketitle
\maketitle