quarto

Нow to add a vertical scrollbar to non-executable code?


I have a chunk of code in a language, say Matlab, that has no engine installed in Quarto. The chunk is only shown for illustration purposes, but it is valid code and can be used, highlighted etc.

So I cannot do something like ```{matlab}. For this scenario there are some workarounds with css styles.

However, I can do just ```matlab.

Now, my question is how to control code chunk overflow by height in this case? All I need is a scrollbar by height (by width it works automatically).


Solution

  • If you provide code via ```matlab, then this code gets rendered inside a <code> element which has the classes sourceCode and matlab. Hence, you can apply css on the selector .sourceCode .matlab, e.g. a suitable adjustment of max-height and overflow-y yields a horizontal scroll bar.

    document.qmd

    ---
    title: Code chunk height scrollbar
    format: 
      html:
        css: styles.css
    ---
    
    ## Matlab
    
    ```matlab
    clear;
    
    v = [1, 4, 7, 2, 19];
    
    maximum = v(1);
    
    for i = 1:length(v)
        element = v(i);
        
        if element > maximum
            maximum = element; 
        end
    end
    
    disp('The maximum is: ');
    disp(maximum);
    ```
    

    styles.css

    .sourceCode .matlab {
        max-height: 200px;
        overflow-y: auto;
    }
    

    enter image description here