如何将在Quarto中创建的代码块内的表格居中对齐?

huangapple go评论53阅读模式
英文:

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

问题

我正在使用quarto和Python来制作一些Beamer幻灯片。我想生成一个Pandas数据框并将其设置为居中显示。我缺少什么代码选项?

英文:

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:

如何将在Quarto中创建的代码块内的表格居中对齐?

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

答案1

得分: 2

请使用 pandoc divs ::: 包装生成表格的代码块,使用 div-class .center 并添加属性 data-latex=""

::: {.center data-latex=""}
英文:

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
```

:::

<hr>

如何将在Quarto中创建的代码块内的表格居中对齐?

答案2

得分: 1

Beamer 会自动居中表格。为了实现这一点,它需要在表格周围有一个 table 环境。

为了强制 Quarto 将表格包裹在 table 环境中,您可以添加一个标题:

---
format: beamer
---

## 测试

```python
import pandas as pd
technologies = [ ["Spark", 20000, "30天"], 
                 ["pandas", 20000, "40天"], 
               ]
column_names = ["课程", "费用", "时长"]
row_label = ["a", "b"]
df = pd.DataFrame(technologies, columns=column_names, index=row_label)

df.style.set_caption('')

如何将在Quarto中创建的代码块内的表格居中对齐?


[1]: https://i.stack.imgur.com/xK0Ru.png

<details>
<summary>英文:</summary>

Beamer will automatically centre tables. To be able to do it, it need a `table` environment around the tabular.

To force quarto to warp the tabular in a table environment, you could add a caption:



    ---
    format: beamer
    ---
    
    ## Test
    
    ```{python}
    import pandas as pd
    technologies = [ [&quot;Spark&quot;,20000, &quot;30days&quot;], 
                     [&quot;pandas&quot;,20000, &quot;40days&quot;], 
                   ]
    column_names=[&quot;Courses&quot;,&quot;Fee&quot;,&quot;Duration&quot;]
    row_label=[&quot;a&quot;,&quot;b&quot;]
    df=pd.DataFrame(technologies,columns=column_names,index=row_label)
    
    df.style.set_caption(&#39;&#39;)
    ```

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/xK0Ru.png

</details>



huangapple
  • 本文由 发表于 2023年3月31日 04:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定