Conditional Content

May 1, 2026 ยท View on GitHub

Quarto allows content to be shown or hidden based on output format, metadata, or profiles.

Format-Based Conditions

Content Visible

Show content only for specific formats:

::: {.content-visible when-format="html"}
This only appears in HTML output.
:::

Content Hidden

Hide content for specific formats:

::: {.content-hidden when-format="pdf"}
This appears everywhere except PDF.
:::

Unless Format

Show unless a specific format:

::: {.content-visible unless-format="html"}
This appears in PDF, DOCX, etc., but not HTML.
:::

Format Values

Single Formats

FormatValue
HTMLhtml
PDFpdf
Worddocx
LaTeXlatex
RevealJSrevealjs
Beamerbeamer
EPUBepub
GitHub Markdowngfm

Format Aliases

Quarto groups related formats:

AliasIncludes
htmlHTML, EPUB, RevealJS, Dashboard
pdfPDF, LaTeX, Beamer
html:jsHTML formats with JavaScript support

Example with Aliases

::: {.content-visible when-format="pdf"}
This appears in PDF, LaTeX, and Beamer.
:::

Multiple Formats

Either Format

::: {.content-visible when-format="html"}
::: {.content-visible when-format="revealjs"}
This appears in HTML or RevealJS.
:::
:::

Or use aliases:

::: {.content-visible when-format="html"}
Appears in all HTML-based formats.
:::

Inline Conditions

For inline content, use spans:

View the [interactive version]{.content-visible when-format="html"}
[figure]{.content-visible when-format="pdf"}.

Metadata-Based Conditions

When Meta

Show based on metadata values:

::: {.content-visible when-meta="draft"}
DRAFT - Not for distribution.
:::

With YAML:

draft: true

Unless Meta

::: {.content-visible unless-meta="draft"}
Final version content.
:::

Nested Metadata

::: {.content-visible when-meta="params.show-advanced"}
Advanced content here.
:::

YAML:

params:
  show-advanced: true

Profile-Based Conditions

Define Profiles

In _quarto.yml:

profile:
  default: production

  group:
    - [development, production]

Profile-Specific Content

::: {.content-visible when-profile="development"}
Debug information here.
:::

::: {.content-visible when-profile="production"}
Production content only.
:::

Using Profiles

quarto render --profile development

Conditional Code Blocks

Format-Specific Code

::: {.content-visible when-format="html"}

```{language}
# interactive output for HTML
```

:::

::: {.content-visible when-format="pdf"}

```{language}
# static output for PDF
```

:::

With QUARTO_EXECUTE_INFO

Quarto creates a JSON file with execution context information. Read it to conditionally execute code in any language.

R

```{r}
quarto_info <- jsonlite::read_json(
  Sys.getenv("QUARTO_EXECUTE_INFO")
)
if (quarto_info$output$format == "html") {
  interactive_plot()
}
```

Python

```{python}
import os
import json

with open(os.environ["QUARTO_EXECUTE_INFO"]) as f:
    quarto_info = json.load(f)

if quarto_info["output"]["format"] == "html":
    interactive_plot()
```

See QUARTO_EXECUTE_INFO for available fields.

Conditional Includes

Include different files based on format:

::: {.content-visible when-format="html"}

{{< include _interactive-content.qmd >}}

:::

::: {.content-visible when-format="pdf"}

{{< include _static-content.qmd >}}

:::

Conditional YAML

Use conditional logic in YAML:

format:
  html:
    include-in-header:
      - text: |
          <script src="interactive.js"></script>
  pdf:
    include-in-header:
      - text: |
          \usepackage{custom}

Complex Conditions

Combining Conditions

::: {.content-visible when-format="html" when-meta="interactive"}
Interactive HTML content.
:::

Both conditions must be true.

Nested Conditions

::: {.content-visible when-format="html"}

::: {.content-visible when-meta="advanced"}
Advanced HTML content.
:::

Basic HTML content.

:::

Resources