Search code examples
pandaslatexr-markdownquartobeamer

How to align to center a table created inside a code chunk in Quarto?


I am using quarto with Python to produce some Beamer slides. I would like to generate a Pandas dataframe and have it a

## Test

```{python}
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["pandas",20000, "40days"], 
               ]
column_names=["Courses","Fee","Duration"]
row_label=["a","b"]
df=pd.DataFrame(technologies,columns=column_names,index=row_label)

df
```

This will generate the following output:

enter image description here

I'd like to have that table centered. What is the chunk option I am missing?


Solution

  • Wrap the code chunk that generates the table with pandoc divs :::, use the div-class .center and attribute data-latex="".

    ---
    title: "Center Align Code Output"
    format: beamer
    jupyter: python3
    ---
    
    ## Test
    
    ::: {.center  data-latex=""}
    
    ```{python}
    import pandas as pd
    technologies = [ ["Spark",20000, "30days"], 
                     ["pandas",20000, "40days"], 
                   ]
    column_names=["Courses","Fee","Duration"]
    row_label=["a","b"]
    df=pd.DataFrame(technologies,columns=column_names,index=row_label)
    
    df
    ```
    
    :::
    

    pandas table center aligned