-
DescriptionHi! I am currently working on a document using Quarto and have encountered an issue with the formatting of table captions. Specifically, the captions for tables generated using Python code blocks include the block label within curly braces, and I'm looking to format them differently. Here's an example.
Is there a way to have only the tbl-cap text in the caption? Any guidance or insights from experienced Quarto users would be greatly appreciated. Your assistance will be invaluable in helping me resolve this matter. Thank you in advance for your help and support. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
Can you edit your question and provide your code in markdown format inside the post ? This way we can copy paste. It is easier for us with a reproducible example. Thanks ! |
Beta Was this translation helpful? Give feedback.
-
This is the code of tabellaM
|
Beta Was this translation helpful? Give feedback.
-
It is a very long document. I attach a zip file with the qmd and data files. I'm using the v1.110.1 of the Quarto plugin on VS code. |
Beta Was this translation helpful? Give feedback.
-
---
title: "Indagine EPPI 2023"
subtitle: \today
author: "Mefop"
lang: it
format:
html:
toc: true
theme: journal
pdf:
toc: false
number-sections: true
number-depth: 2
colorlinks: true
documentclass: memoir
papersize: A4
geometry:
- top=30mm
- left=20mm
- heightrounded
execute:
echo: false
warning: false
tbl-cap-location: top
---
###
```{python}
#| label: Caricamento moduli
import pandas as pd
import xlwings as xw
from IPython.display import display, Markdown, Latex
import locale
import json
# Impostazione del locale per utilizzare la virgola come separatore decimale
locale.setlocale(locale.LC_ALL, 'it_IT.UTF-8')
pd.options.display.float_format = '{:,.2f}'.format
```
```{python}
#| label: DEF tabella risposte multiple
def tabellaM(df, variabili, pesi=True, ordinamento=True, n_valori=None, decimali=1, var_gruppo=None):
"""
Elabora una tabella riassuntiva basata su una lista di variabili, suddividendo opzionalmente per un'altra variabile.
Parametri:
- df: DataFrame di origine.
- variabili: Lista delle variabili da elaborare.
- labels: Dizionario con le etichette descrittive per le variabili.
- pesi: Se True, aggrega 'peso' per somma, altrimenti conta i valori non nulli e > 0.
- ordinamento: Se True, ordina i risultati dal più alto al più basso.
- n_valori: Numero di valori da visualizzare. Se None, mostra tutti i valori.
- decimali: Numero di decimali per la colonna '%'.
- var_gruppo: Variabile su cui basare la suddivisione del DataFrame.
"""
risultati_globali = []
if var_gruppo and var_gruppo in df.columns:
if pd.api.types.is_categorical_dtype(df[var_gruppo]):
gruppi = df[var_gruppo].cat.categories
else:
gruppi = sorted(df[var_gruppo].dropna().unique())
else:
gruppi = [None]
for gruppo in gruppi:
df_gruppo = df if gruppo is None else df[df[var_gruppo] == gruppo]
base = df_gruppo['peso'].sum() if pesi else df_gruppo[df_gruppo['peso'] > 0]['peso'].count()
for var in variabili:
valore = df_gruppo[var].sum() if pesi else df_gruppo[df_gruppo[var] > 0][var].count()
percentuale = (valore / base) * 100
risultati_globali.append({
'Gruppo': gruppo if gruppo is not None else "Totale",
'Risposte': labels[var],
'Soggetti': round(valore),
'%': round(percentuale, decimali)
})
risultati_df = pd.DataFrame(risultati_globali)
if ordinamento:
risultati_df.sort_values(by=['Gruppo', 'Soggetti'], ascending=[True, False], inplace=True)
if n_valori is not None:
risultati_df = risultati_df.groupby('Gruppo').head(n_valori).reset_index(drop=True)
for gruppo in gruppi:
# display(Markdown(gruppo if gruppo is not None else 'Totale'))
gruppo_df = risultati_df[risultati_df['Gruppo'] == gruppo] if gruppo is not None else risultati_df
if not gruppo_df.empty:
markdown_result = gruppo_df.drop('Gruppo', axis=1).to_markdown(index=False, tablefmt="pipe", stralign="right")
display(Markdown(markdown_result))
display(Markdown(f"Soggetti - {gruppo if gruppo is not None else 'Totale'}: {int(base.round(0))}"))
else:
display(Markdown('Nessun dato disponibile per questo gruppo.'))
display(Markdown("*Nota: la somma delle percentuali potrebbe non essere 100 a causa della possibilità di fornire più risposte.*"))
```
```{python}
#| label: Caricamento dati
dati = pd.read_pickle('dati.pkl')
labels = {}
with open('labels.json', 'r') as f:
labels = json.load(f)
```
```{python}
#| label: test
#| tbl-cap:
#| - I problemi economici e di salute affrontati dagli iscritti negli ultimi due anni (fino a 34 anni)
#| - I problemi economici e di salute affrontati dagli iscritti negli ultimi due anni (35-65 anni)
#| - I problemi economici e di salute affrontati dagli iscritti negli ultimi due anni (oltre 65 anni)
# I problemi economici e di salute affrontati dagli iscritti negli ultimi due anni
var = [col for col in dati.columns if col.startswith('problemi_')]
tabellaM(dati, var, var_gruppo='cl_eta')
``` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
tbl-cap
requires a label starting withtbl-
.