## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
1 + 1
```

\newpage
You can add options to executable code like this (the option `#| echo:false` is used at the top of the code chunk). 

The `echo: false` option disables the printing of code (only output is displayed), so the chunk is not visible.


```{r}
#| echo: false
2 * 2
```

## Packages

Snippets of code using different languages can be included. Here we make a set of R packages available for usage in R code chunks:

```{r}
#| label: load-pkgs
#| code-summary: "Packages"
#| code-fold: show
#| message: false
#| echo: true

pkgs <- "
DT bslib leaflet plotly htmltools dplyr purrr
"

import <- function(x)
 x |> trimws() |> strsplit("\\s+") |> unlist() |>
  lapply(function(x) library(x, character.only = TRUE)) |>
  invisible()

pkgs |> import()

```

## Plots

We can include static or dynamic plots, for example using `ggplot`.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(ktheme)
library(dplyr)
library(purrr)
library(knitr)
library(kableExtra)
library(RColorBrewer)
library(scales)
```

This is an example of a static plot using KTH graphical profile colors from [ktheme](https://github.com/KTH-Library/ktheme).

```{r}
#| echo: false
#| fig-width: 9

library(dplyr)
library(ggplot2)

p <- 
  count(mpg, class) |> 
  ggplot(aes(class, n)) +
  geom_col(aes(fill = class)) +
  geom_text(aes(label = paste0("n=", n)), nudge_y = 3) +
  labs(
    x = "Vehicle category", y="Observations (n)",
    title = "Seminal ggplot2 bar chart example",
    subtitle = "A plot that is only useful for demonstration purposes",
    caption = "A demo of theme_kth_neo") +
  theme_kth_neo() +
  scale_fill_kth() +
  theme(axis.text.y=element_blank())

p
```

This is an example of a interactive variant of the same plot.

```{r}

plotly::ggplotly(p)
```

## Embeddable Style Assets HTML/SVG

Logo, favicon, webfonts for KTH are available through the `ktheme` package. This section shows usage.

### Logos

Scalable vector graphics files for the KTH logo can be embedded using `ktheme::kthb_b64_logo_svg()`. 

Increasing the zoom, the logos will still look good (SVG means no raster image format).

::: {layout-ncol=3}

```{r}
#| results: asis

library(ktheme)

h <- 120

kth_b64_logo_svg(variant = "blue", height = h)

kth_b64_logo_svg(variant = "black", height = h)

# styling the white logo with custom CSS to provide padding and blue background

h <- 144

logo_css <- 
"background-size: %spx;
background-position: center;
background-position-y: center;
background-repeat: no-repeat;
padding-left: %spx;
padding-right: %spx;
padding-top: %spx;
padding-bottom: %spx;
background-color:%s;" |> 
  sprintf(h, h/10, h/10, h/10, h/10, ktheme::kth_colors("darkblue")) |> 
  gsub(pattern = "\\s+", replacement = "")

kth_b64_logo_svg(variant = "white", 
  height = h, 
  style = logo_css
)

```
:::

Logos are also available in raster formats, also "spaced" variants which allow for a safe zone around the logo.

### Banner CSS

A chunk used here includes a KTH logo by generating a custom `kth-banner.css` for the top banner, which then can be included in the kth-quarto template.

```{r}
#| eval: false

# install the "ktheme" package which provides fonts and styling assets for KTH
# for example for use in static and dynamic plots

if (!"ktheme" %in% installed.packages())
  devtools::install_github("KTH-Library/ktheme")

kth_banner_css <- 
".quarto-title-block .quarto-title-banner {
  background-image: url('%s');
  background-origin: content-box;
  background-size: 120px;
  background-position: right;
  background-position-y: bottom;
  background-repeat: no-repeat;
  padding-left: 10px;
  padding-right: 30px;
  padding-top: 30px;
  padding-bottom: 30 px;
}" |> sprintf(ktheme::kth_b64_logo_svg("white", as = "uri"))

readr::write_file(kth_banner_css, "_extensions/kth-quarto/kth-banner.css")
message("Wrote banner styles")

```
