quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.98k stars 328 forks source link

Code block options not rendering correctly in beamer #6041

Closed darthlite closed 1 year ago

darthlite commented 1 year ago

Bug description

I think I'm facing a bug, but let me know if I'm not doing things right.

When outputting to a beamer format, I can't get code block formatting to behave as I would like. Specifically, using the options code-block-bg, code-block-border-left, and highlight-style, I do not get any background color or left border at all. This is despite all 3 of these properties being part of the beamer options.

The same combination of settings works perfectly when outputting to a pdf format, however.

Steps to reproduce

Here is a MWE with the issue:

---
title: "Test"
format:
    beamer:
        code-block-bg: "#f8f8f8"
        code-block-border-left: "#245ABE"
        highlight-style: github
---

### Testing code blocks

Code block:
```python
import numpy as np

x = 3   # Defining a variable

def best_function(arg1):
    """
    This is a docstring.
    """
    return arg1

best_function(24.8)

### Expected behavior

This should produce the following colors (taken from pdf output) -- focusing here only on the code block:
![Screenshot 2023-06-27 at 6 26 20 PM](https://github.com/quarto-dev/quarto-cli/assets/116678368/64dae35c-fc00-4ba7-8919-6e5758a590a9)

### Actual behavior

When outputting to beamer, I get the following instead:
![Screenshot 2023-06-27 at 6 28 13 PM](https://github.com/quarto-dev/quarto-cli/assets/116678368/2a9d7033-1030-4afc-8f58-28679dce2f56)

Also, if I comment out `highlight-style: github`, I get even stranger behavior. The whole block takes on the color of the left border:
![Screenshot 2023-06-27 at 6 29 10 PM](https://github.com/quarto-dev/quarto-cli/assets/116678368/249e5e27-be93-4be2-a408-0d0c551ba934)

### Your environment

- OS: MacOS 13.4.1
- IDE: VSCodium 1.79.2

### Quarto check output

```bash
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.1: OK
      Dart Sass version 1.55.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.3.427
      Path: /Applications/quarto/bin

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

[✓] Checking Python 3 installation....OK
      Version: 3.10.12 (Conda)
      Path: /opt/homebrew/Caskroom/miniforge/base/bin/python
      Jupyter: 5.3.1
      Kernels: julia-0.4, matlab_kernel, python3

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

[✓] Checking R installation...........OK
      Version: 3.2.4
      Path: /Library/Frameworks/R.framework/Resources
      LibPaths:
        - /Users/me/Library/R/3.2/library
        - /Library/Frameworks/R.framework/Versions/3.2/Resources/library
      knitr: 1.12.3
      NOTE: knitr version 1.12.3 is too old. Please upgrade to 1.30 or later.
      rmarkdown: 0.9.5

      The knitr package is outdated in this R installation.
      Update with update.packages("knitr")
mcanouil commented 1 year ago

Thanks for the report. Meanwhile we are looking into it, could you fix your masked link which is invalid?

darthlite commented 1 year ago

Apologies, link should be fixed now.

cderv commented 1 year ago

github.theme is a custom them we have in Quarto. It seems there needs to be some adjustment either in the .theme file for Pandoc's skylighting library, or maybe with the way we defined the code block environment (i.e Shaded) as it could be conflict with Beamer. @dragonstyle we are tweaking what pandoc does by default right ?

Also, if I comment out highlight-style: github, I get even stranger behavior. The whole block takes on the color of the left border:

Regarding, it could be related or just another issue regarding code-block-border-left

I think we are inserting this

\@ifundefined{shadecolor}{\definecolor{shadecolor}{HTML}{245ABE}}
\makeatother
\makeatletter
\@ifundefined{codebgcolor}{\definecolor{codebgcolor}{HTML}{f8f8f8}}
\makeatother

where code-block-border-left is assigned to shadedcolor, and code-block-bg to codebgcolor

Then we are supposed to assign the codebgcolor to the environment used for codeblock to "overwrite" the main color for Shaded environment

\ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[boxrule=0pt, breakable, borderline west={3pt}{0pt}{shadecolor}, enhanced, colback={codebgcolor}, sharp corners, frame hidden]}{\end{tcolorbox}}\fi

but it seems colback={codebgcolor} does not apply inside beamer. And so the default shadedcolor applies.

Looking at the diff with when we provide highlight-style: github or not, it seems there is this environment present in .tex

\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}

Should be replaced by tcolorbox but maybe that does not work...

Something specifc to handle here IMO... 🤔

Sidenote: The default behavior is already not the same for codeblock with PDF and Beamer.

image

image

cderv commented 1 year ago

Ok - I think I got to the bottom of it

It seems our redefinition of Shaded environment is not taken into account in Beamer. We include it as before-body. https://github.com/quarto-dev/quarto-cli/blob/912f9534a1df39efb8557235577d10c1280c825d/src/resources/filters/layout/meta.lua#L97-L101 Including it in preamble solves this issue.

diff --git a/src/resources/filters/layout/meta.lua b/src/resources/filters/layout/meta.lua
index 611c8b041..c64b4e174 100644
--- a/src/resources/filters/layout/meta.lua
+++ b/src/resources/filters/layout/meta.lua
@@ -96,7 +96,7 @@ function layout_meta_inject_latex_packages()

         -- redefined the 'Shaded' environment that pandoc uses for fenced        
         -- code blocks
-        metaInjectLatexBefore(meta, function(inject)
+        metaInjectLatex(meta, function(inject)
           inject("\\ifdefined\\Shaded\\renewenvironment{Shaded}{\\begin{tcolorbox}[" .. tColorOptions(options) .. "]}{\\end{tcolorbox}}\\fi")
         end)
       end

@dragonstyle Do you remember why doing the redefinition of Shaded with tcolorbox happens after \begin{}, with before-body ? It is like this from the start (https://github.com/quarto-dev/quarto-cli/commit/2d9642435cb26b7a891359bde86cc985fa0048b1)

I see two solutions here:

What are you thoughts ? Happy to discuss live if needed.

cderv commented 1 year ago

Thanks @dragonstyle and Thanks @darthlite for the report !

darthlite commented 1 year ago

Thank you all for your work and for the speedy fix!