Spacedman
Spacedman

Reputation: 94202

Referring to an executable code block in Quarto

Is there a way to label and refer to an executable code chunk in Quarto? I have the following .qmd file which when ran through quarto render fails to label the executable code chunk.

---
title: "My document"
format:
  pdf:
    colorlinks: true
---

```{#lst-fail .R lst-cap="Code Listing"}
x = fail}{
y = fail_harder(((
```

The code in @lst-fail doesn't run. But I can refer to it.

The code in this next block runs but can't be referenced:

```{r}
#| lst-label: lst-data
#| lst-cap: "Make data"
x=1:10
y=runif(10)
```

And if I try then I get (@lst-data) because undefined. Figures are okay though.

```{r}
#| label: fig-xy
#| fig-cap: "x and y"
plot(x,y)
```

The plot is in @fig-xy

Output looks like this:

enter image description here

What I'd like is for the executable code to appear like the first chunk here, a listing, and be referenceable in the body text.

I'm wondering if this is not possible because of the possibility that a code chunk could generate referencable tables or figures within itself, and that would mess things up badly. Or something. I really don't know (and also I can't find any docs on the quarto site about the listing format I've used in the first chunk, I guess its pandoc markdown or something...)

Upvotes: 2

Views: 607

Answers (1)

Shafee
Shafee

Reputation: 19917

One very naive approach would be link the code block with manual numbering.

---
title: "My document"
format:
  pdf:
    colorlinks: true
---

:::{#lst-data}

```{r}
x=1:10
y=runif(10)
```

:::

See [Code Block 1](#lst-data).


referring executable code blocks

Upvotes: 0

Related Questions