---
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.
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:
\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.
Xie et al. (2020) has addressed this problem by creating a lua filter. I opened "The Notepad" and copied the code given in the book. And I saved the file as 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. We will incorporate the lua file by modifying 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:
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
\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:
---
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
---
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.