简体   繁体   English

如何在 R markdown 生成的 pdf 文档中更改标题的位置并修改交叉引用的颜色?

[英]How can I change the placement of the caption and modify the color of cross-referencing in the R markdown generated pdf document?

---
title: "Annual Report"
author: "Xyz"
date: "`r format(Sys.time(),'%d %B, %Y')`"
output: 
  bookdown::pdf_document2:
    extra_dependencies: ["float"]
    number_sections: false
    toc: false

---


```{r, echo = FALSE}
library(ggplot2)
data(mtcars)
names(mtcars)
```
### Heading 1
```{r figure-1,echo=FALSE, fig.cap = "Sample Graph 1"}
ggplot(mtcars,aes(x=mpg,y=hp))+
  geom_point()+
  theme_classic()

```


To see another graph, please see figure \@ref(fig:figure-2)

\newpage
### Heading 2
```{r figure-2,echo=FALSE, fig.cap = "Sample Graph 2"}
ggplot(mtcars,aes(x=mpg,y=carb))+
  geom_point()+
  theme_classic()

```


To see another graph, please see figure \@ref(fig:figure-1)

I have tried to enclose @ref in the \textcolor{}{}, but Latex removes the cross-referencing and only shows @ref(fig:figure-1) in the knitted pdf document.我曾尝试将 @ref 包含在 \textcolor{}{} 中,但 Latex 删除了交叉引用并且仅在编制的 pdf 文档中显示@ref(fig:figure-1)

Moreover, I want to put the caption at the top of the figure instead of being at the bottom.此外,我想将标题放在图的顶部而不是底部。 I looked over Stackoverflow and users are recommending to use floatrow package to control the placement of caption, but I can't use floatrow with float package. I am using float package in my document to control for extra spaces in my document by using the following code written in Latex:我查看了 Stackoverflow,用户建议使用floatrow package 来控制标题的位置,但我不能将floatrowfloat package 一起使用。我在我的文档中使用 float package 通过使用以下命令来控制文档中的额外空间代码写在Latex:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

Please help me out in sorting out these issues:)请帮我解决这些问题:)

I was able to figure out the solution to both of the problems mentioned in my question.我能够找出问题中提到的两个问题的解决方案。 I am sharing my solution to both problems for the other learners below:我正在为下面的其他学习者分享我对这两个问题的解决方案:

Change the Cross-Reference Color更改交叉引用颜色

We cannot use Latex \textcolor{}{} to change the color of the cross-referencing in R Markdown. My thinking is R Markdown confuses multiple Latex commands with simple words while knitting the document.我们不能使用 Latex \textcolor{}{}来更改 R Markdown 中交叉引用的颜色。我的想法是 R Markdown 在编织简单文档时混淆了多个 Latex 命令。

Xie et al. 谢等。 (2020) has addressed this problem by creating a lua filter. (2020)通过创建 lua 过滤器解决了这个问题。 I opened "The Notepad" and copied the code given in the book.我打开“记事本”,把书上给的代码复制过来。 And I saved the file as color-text.lua .我将文件保存为color-text.lua The code is given below for the reader's convenience:为了方便读者,代码如下:

Span = function(el)
  color = el.attributes['color']
  -- if no color attribute, return unchange
  if color == nil then return el end
  
  -- transform to <span style="color: red;"></span>
  if FORMAT:match 'html' then
    -- remove color attributes
    el.attributes['color'] = nil
    -- use style attribute instead
    el.attributes['style'] = 'color: ' .. color .. ';'
    -- return full span element
    return el
  elseif FORMAT:match 'latex' then
    -- remove color attributes
    el.attributes['color'] = nil
    -- encapsulate in latex code
    table.insert(
      el.content, 1,
      pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
    )
    table.insert(
      el.content,
      pandoc.RawInline('latex', '}')
    )
    -- returns only span content
    return el.content
  else
    -- for other format return unchanged
    return el
  end
end

Once the file is saved, we need to incorporate color-text.lua in the YAML header of our required document.保存文件后,我们需要将color-text.lua合并到我们所需文件的 YAML header 中。 We will incorporate the lua file by modifying YAML header:我们将 lua 文件通过修改 YAML header 合并:

---
title: "Annual Report"
author: "Xyz"
date: "`r format(Sys.time(),'%d %B, %Y')`"
output: 
  bookdown::pdf_document2:
    pandoc_args: ["--lua-filter=color-text.lua"]
    number_sections: false
    toc: false

---

After modifying the YAML header, we can change the font color of cross-referenced text by doing it the following way:修改 YAML header 后,我们可以通过以下方式更改交叉引用文本的字体颜色:

To see another graph, please see figure [\@ref(fig:figure-2)]{color="blue"}

We need to write the cross-referenced text within square brackets followed by color argument within {}.我们需要在方括号内编写交叉引用文本,然后在 {} 内编写颜色参数。

Changing the placement of the caption and controlling for extra whitespaces更改标题的位置并控制额外的空格

If we want to change the placement of the caption and control for extra white space in our pdf document, we need to include the following lines in our.tex file如果我们想在我们的 pdf 文档中更改标题和控件的位置以获得额外的空白,我们需要在我们的 .tex 文件中包含以下行

\usepackage{floatrow}
\usepackage[onehalfspacing]{setspace}
\floatsetup[figure]{capposition=top}
\floatplacement{figure}{H}

After saving the above tex file as caption_control.tex, we will include it in our YAML header:将上面的tex文件保存为caption_control.tex后,我们将其包含在我们的YAML header中:


---
title: "Annual Report"
author: "Xyz"
date: "`r format(Sys.time(),'%d %B, %Y')`"
output: 
  bookdown::pdf_document2:
    pandoc_args: ["--lua-filter=color-text.lua"]
    number_sections: false
    toc: false
    includes:
       in_header: caption.control.tex

---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM