Skip to content

Commit 7002636

Browse files
Issue #613 latex tabular caption
1 parent 660fb36 commit 7002636

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: tinytable
22
Type: Package
33
Title: Simple and Configurable Tables in 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', and 'Typst' Formats
44
Description: Create highly customized tables with this simple and dependency-free package. Data frames can be converted to 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', or 'Typst' tables. The user interface is minimalist and easy to learn. The syntax is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package.
5-
Version: 0.15.1.2
5+
Version: 0.15.1.3
66
Imports:
77
methods
88
Depends:

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Bugs:
66

77
* Tabulator HTML output now preserves special characters in column names like question marks (Issue #611, thanks to @etiennebacher).
88
* Document `rbind2()` limitations around `format_tt()`/`style_tt()` and string coercion when stacking tables (Issue #612, thanks to @alexploner).
9+
* `theme_latex(environment = "tabular")` now preserves captions (Issue #613, thanks to @brueckmann).
910

1011
## 0.15.1
1112

R/theme_latex.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ handle_latex_environment <- function(x, environment, environment_table) {
3131
regex_end = "tabularray inner close"
3232
)
3333

34+
# reinsert caption for plain tabular tables
35+
table_env <- "\\\\begin\\{table\\}"
36+
if (length(table@caption) > 0 && grepl(table_env, table_string, perl = TRUE)) {
37+
cap <- sprintf("\\caption{%s}", table@caption[1])
38+
table_string <- lines_insert(
39+
table_string,
40+
new = cap,
41+
regex = table_env,
42+
position = "after",
43+
perl = TRUE
44+
)
45+
}
46+
3447
# Convert tabularray syntax to tabular
3548
table_string <- gsub("cmidrule\\[(.*?)\\]", "cmidrule(\\1)", table_string)
3649
table_string <- gsub("\\\\toprule|\\\\midrule|\\\\bottomrule", "\\\\hline", table_string)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
\begin{table}
2+
\caption{Tabular caption example.}
3+
\centering
4+
\begin{tabular}{lllll}
5+
mpg & cyl & disp & hp & drat \\
6+
21.0 & 6 & 160 & 110 & 3.90 \\
7+
21.0 & 6 & 160 & 110 & 3.90 \\
8+
22.8 & 4 & 108 & 93 & 3.85 \\
9+
21.4 & 6 & 258 & 110 & 3.08 \\
10+
\end{tabular}
11+
\end{table}

inst/tinytest/test-latex.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ expect_snapshot_print(
2020
label = "latex-nohead.tex"
2121
)
2222

23+
# Issue #613: caption with tabular latex theme
24+
expect_snapshot_print(
25+
tt(x, caption = "Tabular caption example.") |>
26+
theme_latex(environment = "tabular"),
27+
label = "latex-tabular_caption.tex"
28+
)
29+
2330
# Align
2431
expect_snapshot_print(
2532
tt(x) |> style_tt(j = 1:5, align = "ccllr"),

0 commit comments

Comments
 (0)