Search code examples
latexr-markdownpandocquarto

Code block inside a list gets badly formated


Consider the following quarto document:

---
execute:
  cache: false
jupyter: python3
format: 
  pdf:
   number-sections: true
   fontsize: 12 pt
   papersize: A4
   fig-pos: 'H'
   geometry: "left=2.54cm,right=2.54cm,top=2.54cm,bottom=2.54cm"
   classoption: abstract, table
   include-in-header:
     text: |
       \usepackage{fvextra}
       \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
       \usepackage[font=small]{caption}
       \usepackage{float}
       \usepackage{makecell}
       \usepackage{graphicx} 
       \geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=1cm,rmargin=1cm}
---
1. This is an item.

    a) Something

        ```{python}
         #|echo: false
         #|result: asis

         import pandas as pd

         data = {'A': [15, 30],
                'b': [8, 5]}

         df = pd.DataFrame(data)
         print(df)
        ```

The output is badly formatted, as can be seen below. Could someone please help me?

enter image description here


Solution

  • From the Pandoc docs,

    A list item may contain multiple paragraphs and other block-level content. However, subsequent paragraphs must be preceded by a blank line and indented to line up with the first non-space content after the list marker.

    So if you want to indent both a) Something and Code output within the 1. This is an item., you need to indent both of them so that they line up with the T character from 1. This is an item..

    ---
    [Used the Same YAML options as the OP. Removed them just to show the relevant part]
    ---
    
    1. This is an item.
    
       a) Something
       
       ```{python}
       import pandas as pd
        
       data = {'A': [15, 30], 'b': [8, 5]}
        
       df = pd.DataFrame(data)
       print(df)
       ```
    

    code block inside the list


    And if you want to indent the code block within the second level list a) Something, the code block must be indented to line up with the S character of a) Something.

    ---
    [Used the Same YAML options as the OP. Removed them just to show the relevant part]
    ---
    
    1. This is an item.
    
       a) Something
       
          ```{python}
          import pandas as pd
        
          data = {'A': [15, 30], 'b': [8, 5]}
        
          df = pd.DataFrame(data)
          print(df)
          ```
    
    

    code block indented within the sublist


    Also note the preceding blank line before the code chunk in both cases.