Skip to content

Commit

Permalink
Merge pull request #34 from PumasAI/jk/footnotes-with-linebreaks
Browse files Browse the repository at this point in the history
Footnotes with linebreaks
  • Loading branch information
jkrumbiegel authored Sep 23, 2024
2 parents 97958e1 + c485034 commit cff3325
Show file tree
Hide file tree
Showing 40 changed files with 865 additions and 90 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- **Breaking** Footnotes are by default separated with linebreaks now. This can be changed by setting the new `Table` option `linebreak_footnotes = false` [#34](https://github.com/PumasAI/SummaryTables.jl/pull/34).
- **Breaking** Changed `show_overall` keyword of `table_one` to `show_total`. The name of all total columns was changed from `"Overall"` to `"Total"` as well but this can be changed using the new `total_name` keyword.
- Added ability to show "Total" statistics for subgroups in `table_one` [#30](https://github.com/PumasAI/SummaryTables.jl/pull/30).
- Fixed tagging of header rows in docx output, such that the header section is now repeated across pages as expected [#32](https://github.com/PumasAI/SummaryTables.jl/pull/32).
Expand Down
38 changes: 17 additions & 21 deletions README_files/figure-commonmark/cell-5-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions src/cells.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct Table
round_digits::Int
round_mode::Union{Nothing,Symbol}
trailing_zeros::Bool
linebreak_footnotes::Bool
end

function Table(cells, header, footer;
Expand All @@ -121,8 +122,9 @@ function Table(cells, header, footer;
postprocess = [],
rowgaps = Pair{Int,Float64}[],
colgaps = Pair{Int,Float64}[],
linebreak_footnotes::Bool = true,
)
Table(cells, header, footer, footnotes, rowgaps, colgaps, postprocess, round_digits, round_mode, trailing_zeros)
Table(cells, header, footer, footnotes, rowgaps, colgaps, postprocess, round_digits, round_mode, trailing_zeros, linebreak_footnotes)
end

"""
Expand All @@ -136,6 +138,7 @@ end
postprocess = [],
rowgaps = Pair{Int,Float64}[],
colgaps = Pair{Int,Float64}[],
linebreak_footnotes = true,
)
Create a `Table` which can be rendered in multiple formats, such as HTML or LaTeX.
Expand All @@ -160,6 +163,7 @@ Create a `Table` which can be rendered in multiple formats, such as HTML or LaTe
the size of `gap_pt` is added between the rows `index` and `index+1`.
- `colgaps = Pair{Int,Float64}[]`: A list of pairs `index => gap_pt`. For each pair, a visual gap
the size of `gap_pt` is added between the columns `index` and `index+1`.
- `linebreak_footnotes = true`: If `true`, each footnote and annotation starts on a separate line.
## Round mode
Expand Down Expand Up @@ -465,7 +469,7 @@ function postprocess_table(ct::Table, any)
end
return new_cell
end
Table(new_cl, ct.header, ct.footer, ct.footnotes, ct.rowgaps, ct.colgaps, [], ct.round_digits, ct.round_mode, ct.trailing_zeros)
Table(new_cl, ct.header, ct.footer, ct.footnotes, ct.rowgaps, ct.colgaps, [], ct.round_digits, ct.round_mode, ct.trailing_zeros, ct.linebreak_footnotes)
end

function postprocess_table(ct::Table, v::AbstractVector)
Expand Down
6 changes: 4 additions & 2 deletions src/docx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ function to_docx(ct::Table)

push!(tablerows, full_width_border_row(DOCX_OUTER_RULE_SIZE))

separator_element = ct.linebreak_footnotes ? WriteDocx.Break() : WriteDocx.Text(" ")

if !isempty(annotations) || !isempty(ct.footnotes)
elements = []
for (i, (annotation, label)) in enumerate(annotations)
i > 1 && push!(elements, WriteDocx.Run([WriteDocx.Text(" ")]))
i > 1 && push!(elements, WriteDocx.Run([separator_element]))
if label !== NoLabel()
push!(elements, WriteDocx.Run([WriteDocx.Text(docx_sprint(label)), WriteDocx.Text(" ")],
WriteDocx.RunProperties(valign = WriteDocx.VerticalAlignment.superscript)))
Expand All @@ -92,7 +94,7 @@ function to_docx(ct::Table)
WriteDocx.RunProperties(size = DOCX_ANNOTATION_FONTSIZE)))
end
for (i, footnote) in enumerate(ct.footnotes)
(!isempty(annotations) || i > 1) && push!(elements, WriteDocx.Run([WriteDocx.Text(" ")]))
(!isempty(annotations) || i > 1) && push!(elements, WriteDocx.Run([separator_element]))
push!(elements, WriteDocx.Run([WriteDocx.Text(docx_sprint(footnote))],
WriteDocx.RunProperties(size = DOCX_ANNOTATION_FONTSIZE)))
end
Expand Down
16 changes: 14 additions & 2 deletions src/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ function Base.show(io::IO, ::MIME"text/html", ct::Table)
if !isempty(annotations) || !isempty(ct.footnotes)
print(_io, " <tr><td colspan=\"$(size(matrix, 2))\" style=\"font-size: 0.8em;\">")
for (i, (annotation, label)) in enumerate(annotations)
i > 1 && print(_io, "&nbsp;&nbsp;&nbsp;&nbsp;")
if i > 1
if ct.linebreak_footnotes
print(_io, "<br/>")
else
print(_io, "&nbsp;&nbsp;&nbsp;&nbsp;")
end
end
if label !== NoLabel()
print(_io, "<sup>")
_showas(_io, MIME"text/html"(), label)
Expand All @@ -99,7 +105,13 @@ function Base.show(io::IO, ::MIME"text/html", ct::Table)
_showas(_io, MIME"text/html"(), annotation)
end
for (i, footnote) in enumerate(ct.footnotes)
(!isempty(annotations) || i > 1) && print(_io, "&nbsp;&nbsp;&nbsp;&nbsp;")
if !isempty(annotations) || i > 1
if ct.linebreak_footnotes
print(_io, "<br/>")
else
print(_io, "&nbsp;&nbsp;&nbsp;&nbsp;")
end
end
_showas(_io, MIME"text/html"(), footnote)
end
println(_io, "</td></tr>")
Expand Down
4 changes: 3 additions & 1 deletion src/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ function Base.show(io::IO, ::MIME"text/latex", ct::Table)
\end{tabular}
""")
if !isempty(annotations) || !isempty(ct.footnotes)
println(io, raw"\begin{tablenotes}[flushleft,para]")
println(io, "\\begin{tablenotes}[flushleft$(ct.linebreak_footnotes ? "" : ",para")]")
println(io, raw"\footnotesize")
for (annotation, label) in annotations
if label !== NoLabel()
print(io, raw"\item[")
_showas(io, MIME"text/latex"(), label)
print(io, "]")
else
print(io, raw"\item[]")
end
_showas(io, MIME"text/latex"(), annotation)
println(io)
Expand Down
18 changes: 12 additions & 6 deletions src/typst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,32 @@ function Base.show(io::IO, M::MIME"text/typst", ct::Table)
align = _align(CellStyle(halign = :left), 1)
colspan = "colspan: $(size(matrix, 2))"
options = join(filter(!isempty, [align, colspan]), ", ")
print(io, " table.cell($options)[")
print(io, " table.cell($options)[#text(size: 0.8em)[")

if (!isempty(annotations) || !isempty(ct.footnotes)) && ct.linebreak_footnotes
print(io, "\n ")
end

for (i, (annotation, label)) in enumerate(annotations)
i > 1 && print(io, "#h(1.5em, weak: true)")
i > 1 && print(io, ct.linebreak_footnotes ? "\\\n " : "#h(1.5em, weak: true)")
if label !== NoLabel()
print(io, "#super[")
_showas(io, MIME"text/typst"(), label)
print(io, "]")
end
print(io, "#text(size: 0.8em)[")
_showas(io, MIME"text/typst"(), annotation)
print(io, "]")
end

for (i, footnote) in enumerate(ct.footnotes)
(!isempty(annotations) || i > 1) && print(io, "#h(1.5em, weak: true)")
(!isempty(annotations) || i > 1) && print(io, ct.linebreak_footnotes ? "\\\n " : "#h(1.5em, weak: true)")
_showas(io, MIME"text/typst"(), footnote)
end

println(io, "],") # table.cell()[
if (!isempty(annotations) || !isempty(ct.footnotes)) && ct.linebreak_footnotes
print(io, "\n ")
end

println(io, "]],") # table.cell()[#text(..)[
end

println(io, ")") # table()
Expand Down
2 changes: 1 addition & 1 deletion test/references/annotations/annotated_float.latex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
0.124\tnote{A} \\
\bottomrule
\end{tabular}
\begin{tablenotes}[flushleft,para]
\begin{tablenotes}[flushleft]
\footnotesize
\item[A]Note 1
\end{tablenotes}
Expand Down
4 changes: 3 additions & 1 deletion test/references/annotations/annotated_float.typ.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
table.hline(y: 0, stroke: 1pt),
[0.124#super[A]],
table.hline(y: 1, stroke: 1pt),
table.cell(align: left, colspan: 1)[#super[A]#text(size: 0.8em)[Note 1]],
table.cell(align: left, colspan: 1)[#text(size: 0.8em)[
#super[A]Note 1
]],
)
4 changes: 2 additions & 2 deletions test/references/annotations/automatic_annotations.docx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
</w:r>
<w:r>
<w:rPr/>
<w:t xml:space="preserve"> </w:t>
<w:br w:type="textWrapping"/>
</w:r>
<w:r>
<w:rPr>
Expand All @@ -343,7 +343,7 @@
</w:r>
<w:r>
<w:rPr/>
<w:t xml:space="preserve"> </w:t>
<w:br w:type="textWrapping"/>
</w:r>
<w:r>
<w:rPr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A\tnote{1} & B\tnote{2} \\
C\tnote{3} & D\tnote{1} \\
\bottomrule
\end{tabular}
\begin{tablenotes}[flushleft,para]
\begin{tablenotes}[flushleft]
\footnotesize
\item[1]Note 1
\item[2]Note 2
Expand Down
14 changes: 7 additions & 7 deletions test/references/annotations/automatic_annotations.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<table class="st-8670b714">
<table class="st-11d3bcb5">
<style>
.st-8670b714 {
.st-11d3bcb5 {
border: none;
margin: 0 auto;
padding: 0.25rem;
Expand All @@ -9,22 +9,22 @@
line-height: 1.2em;
}

.st-8670b714 tr td {
.st-11d3bcb5 tr td {
vertical-align: top;
padding: 0;
border: none;
}

.st-8670b714 br {
.st-11d3bcb5 br {
line-height: 0em;
margin: 0;
}

.st-8670b714 sub {
.st-11d3bcb5 sub {
line-height: 0;
}

.st-8670b714 sup {
.st-11d3bcb5 sup {
line-height: 0;
}
</style>
Expand All @@ -38,5 +38,5 @@
<td style="text-align:center;">D<sup>1</sup></td>
</tr>
<tr><td colspan="2" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
<tr><td colspan="2" style="font-size: 0.8em;"><sup>1</sup> Note 1&nbsp;&nbsp;&nbsp;&nbsp;<sup>2</sup> Note 2&nbsp;&nbsp;&nbsp;&nbsp;<sup>3</sup> Note 3</td></tr>
<tr><td colspan="2" style="font-size: 0.8em;"><sup>1</sup> Note 1<br/><sup>2</sup> Note 2<br/><sup>3</sup> Note 3</td></tr>
</table>
6 changes: 5 additions & 1 deletion test/references/annotations/automatic_annotations.typ.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
[C#super[3]],
[D#super[1]],
table.hline(y: 2, stroke: 1pt),
table.cell(align: left, colspan: 2)[#super[1]#text(size: 0.8em)[Note 1]#h(1.5em, weak: true)#super[2]#text(size: 0.8em)[Note 2]#h(1.5em, weak: true)#super[3]#text(size: 0.8em)[Note 3]],
table.cell(align: left, colspan: 2)[#text(size: 0.8em)[
#super[1]Note 1\
#super[2]Note 2\
#super[3]Note 3
]],
)
6 changes: 3 additions & 3 deletions test/references/annotations/manual_annotations.docx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
</w:r>
<w:r>
<w:rPr/>
<w:t xml:space="preserve"> </w:t>
<w:br w:type="textWrapping"/>
</w:r>
<w:r>
<w:rPr>
Expand All @@ -343,7 +343,7 @@
</w:r>
<w:r>
<w:rPr/>
<w:t xml:space="preserve"> </w:t>
<w:br w:type="textWrapping"/>
</w:r>
<w:r>
<w:rPr>
Expand All @@ -360,7 +360,7 @@
</w:r>
<w:r>
<w:rPr/>
<w:t xml:space="preserve"> </w:t>
<w:br w:type="textWrapping"/>
</w:r>
<w:r>
<w:rPr>
Expand Down
2 changes: 1 addition & 1 deletion test/references/annotations/manual_annotations.latex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A\tnote{X} & B\tnote{Y} \\
C\tnote{1} & D\tnote{2} \\
\bottomrule
\end{tabular}
\begin{tablenotes}[flushleft,para]
\begin{tablenotes}[flushleft]
\footnotesize
\item[1]Note 3
\item[2]Note 4
Expand Down
Loading

0 comments on commit cff3325

Please sign in to comment.