繁体   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)

我曾尝试将 @ref 包含在 \textcolor{}{} 中,但 Latex 删除了交叉引用并且仅在编制的 pdf 文档中显示@ref(fig:figure-1)

此外,我想将标题放在图的顶部而不是底部。 我查看了 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
}

请帮我解决这些问题:)

我能够找出问题中提到的两个问题的解决方案。 我正在为下面的其他学习者分享我对这两个问题的解决方案:

更改交叉引用颜色

我们不能使用 Latex \textcolor{}{}来更改 R Markdown 中交叉引用的颜色。我的想法是 R Markdown 在编织简单文档时混淆了多个 Latex 命令。

谢等。 (2020)通过创建 lua 过滤器解决了这个问题。 我打开“记事本”,把书上给的代码复制过来。 我将文件保存为color-text.lua 为了方便读者,代码如下:

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

保存文件后,我们需要将color-text.lua合并到我们所需文件的 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

---

修改 YAML header 后,我们可以通过以下方式更改交叉引用文本的字体颜色:

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

我们需要在方括号内编写交叉引用文本,然后在 {} 内编写颜色参数。

更改标题的位置并控制额外的空格

如果我们想在我们的 pdf 文档中更改标题和控件的位置以获得额外的空白,我们需要在我们的 .tex 文件中包含以下行

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

将上面的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