Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lines containing, but not starting, with "#| echo: fenced" are omitted #10821

Closed
debruine opened this issue Sep 17, 2024 · 12 comments · Fixed by #10876
Closed

Lines containing, but not starting, with "#| echo: fenced" are omitted #10821

debruine opened this issue Sep 17, 2024 · 12 comments · Fixed by #10876
Assignees
Labels
bug Something isn't working
Milestone

Comments

@debruine
Copy link

Bug description

This is a tiny minor bug, but if you try to include the text #| echo: fenced in a fenced figure caption, that doesn't show up! (I found this while writing the quarto chapter of my MSc methods textbook).

I assume you just need to add regex to only omit lines that start with #| echo: fenced

Steps to reproduce

```{r}
#| echo: fenced
#| label: fig-demo
#| fig-cap: "fig-cap is invisible if it contains `#| echo: fenced`"

hist(rnorm(100000))
```

Expected behavior

The figure caption line should show in the code block

Actual behavior

The fig-cap line does not show

Your environment

  • IDE: RStudio 2023.06.0+421
  • quarto: 1.5.57
  • R version 4.2.1 (2022-06-23)

Quarto check output

Quarto 1.5.57
[✓] Checking versions of quarto binary dependencies...
Pandoc version 3.2.0: OK
Dart Sass version 1.70.0: OK
Deno version 1.41.0: OK
Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
Version: 1.5.57
Path: /Applications/quarto/bin

[✓] Checking tools....................OK
TinyTeX: (external install)
Chromium: (not installed)

[✓] Checking LaTeX....................OK
Using: TinyTex
Path: /Users/lisad/Library/TinyTeX/bin/universal-darwin
Version: 2021

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
Version: 3.9.6
Path: /Users/lisad/.virtualenvs/r-reticulate/bin/python3
Jupyter: (None)

  Jupyter is not available in this Python installation.
  Install with python3 -m pip install jupyter

[✓] Checking R installation...........OK
Version: 4.2.1
Path: /Library/Frameworks/R.framework/Resources
LibPaths:
- /Library/Frameworks/R.framework/Versions/4.2/Resources/library
knitr: 1.44
rmarkdown: 2.25

[✓] Checking Knitr engine render......OK

@debruine debruine added the bug Something isn't working label Sep 17, 2024
@mcanouil
Copy link
Collaborator

I cannot reproduce on the current development version.

image

@debruine
Copy link
Author

debruine commented Sep 17, 2024

@mcanouil You did reproduce it. The code chunk in the rendered doc has the label line, but not the fig-cap line

@mcanouil
Copy link
Collaborator

How you meant, it's not in the echoed code cell.

@debruine
Copy link
Author

Super-niche bug, I know

@mcanouil
Copy link
Collaborator

mcanouil commented Sep 17, 2024

This happens for both knitr and Python engines.

It seems to be on purpose (not only here) but maybe the "hard way" is way too hard:

  • for (const contentLine of contentLines) {
    const commentMatch = contentLine.value.match(commentPattern);
    if (commentMatch) {
    if (contentLine.value.indexOf("echo: fenced") === -1) {
    frontMatterLines.push(contentLine);
    }
    ++inputIndex;
    } else {
    break;
    }
    }
    const inputLines = contentLines.slice(inputIndex);
  • // hack for "echo: fenced": remove all "//| echo: fenced" lines the hard way, but keep
    // the right line numbers around.
    Array.from(document.querySelectorAll("span.co"))
    .filter((n) => n.textContent === "//| echo: fenced")
    .forEach((n) => {
    const lineSpan = n.parentElement;
    const lineBreak = lineSpan.nextSibling;
    if (lineBreak) {
    const nextLineSpan = lineBreak.nextSibling;
    if (nextLineSpan) {
    const lineNumber = Number(nextLineSpan.id.split("-")[1]);
    nextLineSpan.style = `counter-reset: source-line ${lineNumber - 1}`;
    }
    }
    // update the source offset variable with the new right amount
    const sourceDiv = lineSpan.parentElement.parentElement.parentElement;
    const oldOffset = Number(sourceDiv.dataset.sourceOffset);
    sourceDiv.dataset.sourceOffset = oldOffset - "//| echo: fenced\n".length;
    lineSpan.remove();
    lineBreak.remove();
    });

@mcanouil
Copy link
Collaborator

In fact, you only need echo: fenced to be in a line starting with the special comment symbol.

InputOutput
---
title: "Quarto Playground"
format: html
---

```{r}
#| echo: fenced
#| whatever: "echo: fenced"
#| something: "echo: true"
print("Hello, World!")
```
image

@debruine
Copy link
Author

I hacked a fix like I describe here: #3554 (reply in thread)

@cscheid cscheid added this to the v1.6 milestone Sep 17, 2024
@cscheid cscheid self-assigned this Sep 17, 2024
@cscheid
Copy link
Collaborator

cscheid commented Sep 17, 2024

The echo: fenced removal is indeed super hacky on our side, and it'll be a long time before we can make it perfect. Nevertheless, we should only be a little more defensive and only pluck the lines that have no other content.

@cscheid
Copy link
Collaborator

cscheid commented Sep 24, 2024

FYI the specific line at play is this one:

yamlCode <- Filter(function(line) !grepl("echo:\\s+fenced", line), yamlCode)

@cscheid
Copy link
Collaborator

cscheid commented Sep 24, 2024

In Jupyter, it seems that we're removing more than just the echo: fenced lines:

---
title: issue-10821
format: html
_quarto:
  tests:
    html:
      ensureHtmlElements:
        - []
        - []
---

```{python}
#| echo: fenced
#| warning: true
#| whatever: "echo: fenced"
#| something: "echo: true"
print("Hello, World!")
```

The output:

image

I believe what's happening is that in Jupyter, unrecognized options are not forwarded to the engine and so the engine can't print them back to us.

@cscheid
Copy link
Collaborator

cscheid commented Sep 24, 2024

In addition, we also seem to be stripping echo: fenced from Jupyter cells too aggressively:

---
title: issue-10821
format: html
_quarto:
  tests:
    html:
      ensureHtmlElements:
        - []
        - []
---

```{python}
#| echo: fenced
#| label: fig-1
#| fig-cap: "This figure will have 'echo: fenced' in its caption."
import matplotlib.pyplot as plt
plt.plot([1,2,3])
```
image

@cscheid
Copy link
Collaborator

cscheid commented Sep 24, 2024

In contrast,

---
title: issue-10821
format: html
_quarto:
  tests:
    html:
      ensureHtmlElements:
        - []
        - []
---

```{python}
#| echo: fenced
#| label: fig-1
#| fig-cap: "A regular caption."
import matplotlib.pyplot as plt
plt.plot([1,2,3])
```
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants