[英]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 來控制標題的位置,但我不能將floatrow與float 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.