英文:
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:
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>
答案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('')
[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 = [ ["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.style.set_caption('')
```
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/xK0Ru.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论