latexquarto

Not able to create blue caption for my figure


I'm trying to create blue caption for my figure, but I get the error message

ERROR: 
compilation failed- missing packages (automatic installed disabled)
Undefined control sequence.
<recently read> \DeclareCaptionFont 
                    
l.97 \DeclareCaptionFont
                      {blue}{\color{blue}}

Then I try to render my code. Any ideas why this does not work?

---
title: "test"
format: 
   pdf: 
     include-in-header:
      - text: | 
           \usepackage{geometry}
           \usepackage{color}
           \DeclareCaptionFont{blue}{\color{blue}}
           \usepackage[font=Large,labelfont = bf,textfont = {blue,sf}]{caption}
editor: visual
pdf-engine: lualatex
fig-cap-location: top
lang: nb
---

```{r, figure, fig.height= 12, fig.width= 10,fig.cap=paste("Blue Caption")}
#| echo: false
library(tidyverse)


 data.frame(Year= 2000:2002, 
            length=c(4.2, 10, 29.5)) %>% 
  ggplot() + geom_line(mapping = aes(x = Year, y = length))

```

Solution

  • You need to load the caption package if you want to use macros from this package like \DeclareCaptionFont:

    ---
    title: "test"
    format: 
       pdf: 
         include-in-header:
          - text: | 
               \newcommand{\pandocbounded}[1]{#1}
               \usepackage{caption}
               \usepackage{geometry}
               \usepackage{xcolor}
               \DeclareCaptionFont{blue}{\color{blue}}
               \usepackage[font=Large,labelfont = bf,textfont = {blue,sf}]{caption}
    editor: visual
    pdf-engine: lualatex
    fig-cap-location: top
    lang: nb
    ---
    
    ```{r, figure, fig.height= 12, fig.width= 10,fig.cap=paste("Blue Caption")}
    #| echo: false
    library(tidyverse)
    
    
     data.frame(Year= 2000:2002, 
                length=c(4.2, 10, 29.5)) %>% 
      ggplot() + geom_line(mapping = aes(x = Year, y = length))
    
    ```
    

    enter image description here