bug fix to column_spec
diff --git a/R/add_header_left.R b/R/add_header_left.R
index 4129203..1b4e1fe 100644
--- a/R/add_header_left.R
+++ b/R/add_header_left.R
@@ -19,14 +19,14 @@
#' @param align Column alignment. you can choose from "c", "l" or "r"
#' @param bold A T/F value to control whether the text should be bolded.
#' @param italic A T/F value to control whether the text should to be emphasized.
-#' @param full_midline This option currently only work in LaTeX. It's a
-#' TRUE/FALSE option to control if the mid line needs to be extended to the end
-#' of row.
+#' @param ... Extra options to be passed into HTML or LaTeX. Right now there is
+#' only one for LaTeX. Option full_midline is a TRUE/FALSE option to control
+#' if the mid line needs to be extended to the end of row.
#'
#' @export
add_header_left <- function(kable_input, header = NULL, header_name = "",
align = "c", width = NULL, bold = F, italic = F,
- full_midline) {
+ ...) {
if (is.null(header)) return(kable_input)
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
@@ -39,7 +39,7 @@
}
if (kable_format == "latex") {
return(add_header_left_latex(kable_input, header, header_name, align,
- width, bold, italic, full_midline))
+ width, bold, italic, ...))
}
}
@@ -203,9 +203,9 @@
ifelse(is.null(width), "\\*", width),
"\\}\\{",
switch(align,
- "l" = "\\\\raggedright",
- "c" = "\\\\centering ",
- "r" = "\\\\raggedleft "),
+ "l" = "\\\\raggedright\\\\arraybackslash ",
+ "c" = "\\\\centering\\\\arraybackslash ",
+ "r" = "\\\\raggedleft\\\\arraybackslash "),
header$header[j], "\\} & "
)
new_row_text <- paste0(new_row_pre, contents[header$row_end[j]])
diff --git a/R/column_spec.R b/R/column_spec.R
index d4dd1ac..851861a 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -5,7 +5,9 @@
#' bold text and italic text.
#'
#' @param kable_input Output of `knitr::kable()` with `format` specified
-#' @param column A numeric value indicating which column to be selected
+#' @param column A numeric value indicating which column to be selected. When
+#' you do the counting, ignore the extra header columns you added through
+#' add_header_left.
#' @param width A character string telling HTML & LaTeX how wide the column
#' needs to be, e.g. "10cm", "3in" or "30em".
#' @param bold A T/F value to control whether the text of the selected column
@@ -98,9 +100,9 @@
extra_align <- ""
if (!is.null(width)) {
extra_align <- switch(x,
- "l" = "\\\\raggedright",
- "c" = "\\\\centering",
- "r" = "\\\\raggedleft")
+ "l" = "\\\\raggedright\\\\arraybackslash",
+ "c" = "\\\\centering\\\\arraybackslash",
+ "r" = "\\\\raggedleft\\\\arraybackslash")
x <- paste0("p\\{", width, "\\}")
}
diff --git a/README.md b/README.md
index 490e86a..3ecf948 100644
--- a/README.md
+++ b/README.md
@@ -4,10 +4,6 @@
***
-**If you find using `kableExtra` will "unload" LaTeX packages or settings you defined in the yaml header, you need to install the latest dev version of `rmarkdown` from github. Please see [issue 5](https://github.com/haozhu233/kableExtra/issues/5) & [issue 11](https://github.com/haozhu233/kableExtra/issues/11) for details.**
-
-***
-
When we are talking about table generators in R, knitr::kable wins lots of flavor by its ultimate simplicity. Unlike those powerful table rendering engines such as `xtable`, the philosophy behind kable is to make it easy for programmers to use. Just as it claimed in its function description,
> This is a very simple table generator. It is simple by design. It is not intended to replace any other R packages for making tables. - Yihui
diff --git a/docs/awesome_table_in_html.Rmd b/docs/awesome_table_in_html.Rmd
index c77734a..cfbfddb 100644
--- a/docs/awesome_table_in_html.Rmd
+++ b/docs/awesome_table_in_html.Rmd
@@ -239,3 +239,12 @@
add_header_left(c("Team X" = 5, "Team Y" = 5), "Team") %>%
add_header_left(c("League Alpha" = 10), "League")
```
+
+Also, since `col_spec` won't work on these "added" header rows & columns, if you want to bold, italic or specify width for these appended columns, you can define them here.
+```{r}
+mtcars[1:5, 1:6] %>%
+ kable("html") %>%
+ kable_styling() %>%
+ add_header_above(c(" ", "Data 1" = 3, "Data 2" = 3)) %>%
+ add_header_left(c("Group A" = 3, "Group B" = 2), "Group", bold = T, width = "10cm")
+```
diff --git a/docs/awesome_table_in_html.html b/docs/awesome_table_in_html.html
index a755c5b..28dff27 100644
--- a/docs/awesome_table_in_html.html
+++ b/docs/awesome_table_in_html.html
@@ -11,7 +11,7 @@
<meta name="author" content="Hao Zhu" />
-<meta name="date" content="2017-06-16" />
+<meta name="date" content="2017-06-21" />
<title>Create Awesome HTML Table with knitr::kable and kableExtra</title>
@@ -217,7 +217,7 @@
<h1 class="title toc-ignore">Create Awesome HTML Table with knitr::kable and kableExtra</h1>
<h4 class="author"><em>Hao Zhu</em></h4>
-<h4 class="date"><em>2017-06-16</em></h4>
+<h4 class="date"><em>2017-06-21</em></h4>
</div>
@@ -3747,6 +3747,178 @@
</tr>
</tbody>
</table>
+<p>Also, since <code>col_spec</code> won’t work on these “added” header rows & columns, if you want to bold, italic or specify width for these appended columns, you can define them here.</p>
+<pre class="r"><code>mtcars[1:5, 1:6] %>%
+ kable("html") %>%
+ kable_styling() %>%
+ add_header_above(c(" ", "Data 1" = 3, "Data 2" = 3)) %>%
+ add_header_left(c("Group A" = 3, "Group B" = 2), "Group", bold = T, width = "10cm")</code></pre>
+<?xml version="1.0" encoding="UTF-8" ?>
+<table class="table" style="margin-left: auto; margin-right: auto;">
+<thead>
+<tr>
+<th style="text-align:center; vertical-align: bottom;width: 10cm; font-weight: bold; " rowspan="2">
+Group
+</th>
+<th style="border-bottom:hidden"></th>
+<th style="text-align:center; border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;" colspan="3">
+<div style="border-bottom: 1px solid #ddd;padding-bottom: 5px;">
+Data 1
+</div>
+</th>
+<th style="text-align:center; border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;" colspan="3">
+<div style="border-bottom: 1px solid #ddd;padding-bottom: 5px;">
+Data 2
+</div>
+</th>
+</tr>
+<tr>
+<th style="text-align:left;">
+</th>
+<th style="text-align:right;">
+mpg
+</th>
+<th style="text-align:right;">
+cyl
+</th>
+<th style="text-align:right;">
+disp
+</th>
+<th style="text-align:right;">
+hp
+</th>
+<th style="text-align:right;">
+drat
+</th>
+<th style="text-align:right;">
+wt
+</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td style="text-align:center; vertical-align: middle;width: 10cm; font-weight: bold; " rowspan="3">
+Group A
+</td>
+<td style="text-align:left;">
+Mazda RX4
+</td>
+<td style="text-align:right;">
+21.0
+</td>
+<td style="text-align:right;">
+6
+</td>
+<td style="text-align:right;">
+160
+</td>
+<td style="text-align:right;">
+110
+</td>
+<td style="text-align:right;">
+3.90
+</td>
+<td style="text-align:right;">
+2.620
+</td>
+</tr>
+<tr>
+<td style="text-align:left;">
+Mazda RX4 Wag
+</td>
+<td style="text-align:right;">
+21.0
+</td>
+<td style="text-align:right;">
+6
+</td>
+<td style="text-align:right;">
+160
+</td>
+<td style="text-align:right;">
+110
+</td>
+<td style="text-align:right;">
+3.90
+</td>
+<td style="text-align:right;">
+2.875
+</td>
+</tr>
+<tr>
+<td style="text-align:left;">
+Datsun 710
+</td>
+<td style="text-align:right;">
+22.8
+</td>
+<td style="text-align:right;">
+4
+</td>
+<td style="text-align:right;">
+108
+</td>
+<td style="text-align:right;">
+93
+</td>
+<td style="text-align:right;">
+3.85
+</td>
+<td style="text-align:right;">
+2.320
+</td>
+</tr>
+<tr>
+<td style="text-align:center; vertical-align: middle;width: 10cm; font-weight: bold; " rowspan="2">
+Group B
+</td>
+<td style="text-align:left;">
+Hornet 4 Drive
+</td>
+<td style="text-align:right;">
+21.4
+</td>
+<td style="text-align:right;">
+6
+</td>
+<td style="text-align:right;">
+258
+</td>
+<td style="text-align:right;">
+110
+</td>
+<td style="text-align:right;">
+3.08
+</td>
+<td style="text-align:right;">
+3.215
+</td>
+</tr>
+<tr>
+<td style="text-align:left;">
+Hornet Sportabout
+</td>
+<td style="text-align:right;">
+18.7
+</td>
+<td style="text-align:right;">
+8
+</td>
+<td style="text-align:right;">
+360
+</td>
+<td style="text-align:right;">
+175
+</td>
+<td style="text-align:right;">
+3.15
+</td>
+<td style="text-align:right;">
+3.440
+</td>
+</tr>
+</tbody>
+</table>
</div>
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index cc7cf6c..cdd9be6 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
diff --git a/man/add_header_left.Rd b/man/add_header_left.Rd
index 51b2ae6..aab6b16 100644
--- a/man/add_header_left.Rd
+++ b/man/add_header_left.Rd
@@ -5,7 +5,7 @@
\title{Add a heading column to the left side of the table}
\usage{
add_header_left(kable_input, header = NULL, header_name = "", align = "c",
- width = NULL, bold = F, italic = F, full_midline)
+ width = NULL, bold = F, italic = F, ...)
}
\arguments{
\item{kable_input}{Output of `knitr::kable()` with `format` specified}
@@ -28,9 +28,9 @@
\item{italic}{A T/F value to control whether the text should to be emphasized.}
-\item{full_midline}{This option currently only work in LaTeX. It's a
-TRUE/FALSE option to control if the mid line needs to be extended to the end
-of row.}
+\item{...}{Extra options to be passed into HTML or LaTeX. Right now there is
+only one for LaTeX. Option full_midline is a TRUE/FALSE option to control
+if the mid line needs to be extended to the end of row.}
}
\description{
This function uses the same syntax as add_header_above. It will
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
index a107eb8..cf4b5cf 100644
--- a/man/column_spec.Rd
+++ b/man/column_spec.Rd
@@ -10,7 +10,9 @@
\arguments{
\item{kable_input}{Output of `knitr::kable()` with `format` specified}
-\item{column}{A numeric value indicating which column to be selected}
+\item{column}{A numeric value indicating which column to be selected. When
+you do the counting, ignore the extra header columns you added through
+add_header_left.}
\item{width}{A character string telling HTML & LaTeX how wide the column
needs to be, e.g. "10cm", "3in" or "30em".}
diff --git a/tests/visual_tests/add_header_left.Rmd b/tests/visual_tests/add_header_left.Rmd
index be34a14..3083fa3 100644
--- a/tests/visual_tests/add_header_left.Rmd
+++ b/tests/visual_tests/add_header_left.Rmd
@@ -11,9 +11,9 @@
mtcars[1:10, 1:6] %>%
kable("latex", booktabs= T) %>%
- # kable_styling(latex_options = "striped") %>%
+ kable_styling(latex_options = "striped") %>%
add_header_above(c(" ", "a%" = 3, "b" = 3)) %>%
- add_header_left(c("a%" = 3, "b" = 7), "new") %>%
+ add_header_left(c("a%knjnuulkjlkj" = 3, "b" = 7), "new", align = "l") %>%
add_header_left(c("aadjfoi adlfsjs adsa" = 4, "b" = 6), "new2", width = "1.5cm") %>%
add_header_left(c("a" = 5, "b" = 5), "xx", width = "1cm", align = "r")
```
diff --git a/tests/visual_tests/column_row_spec_pdf.Rmd b/tests/visual_tests/column_row_spec_pdf.Rmd
index d76c802..b639092 100644
--- a/tests/visual_tests/column_row_spec_pdf.Rmd
+++ b/tests/visual_tests/column_row_spec_pdf.Rmd
@@ -39,35 +39,8 @@
)
)
-kable(dt, "latex", booktabs = T) %>%
- kable_styling(latex_options = "scale_down") %>%
- column_spec(2, "30em", bold = T) %>%
+kable(dt, "latex", booktabs = T, align = "r") %>%
+ column_spec(2, "3cm", bold = T) %>%
column_spec(4, "3cm", italic = T)
```
-```{r}
-dt <- mtcars[1:5, 1:6]
-dt$wt <- paste(dt$wt, "%")
-dt$mpg <- paste(dt$mpg, "&")
-dt$cyl <- paste(dt$cyl, "$")
-
-kable(dt, "latex", booktabs = T) %>%
- group_rows("Group 1", 2, 4) %>%
- column_spec(7, bold = T) %>%
- row_spec(5, bold = T, italic = T)
-```
-
-
-```{r}
-dt <- mtcars[1:5, 1:6]
-dt$wt <- paste(dt$wt, "%")
-dt$mpg <- paste(dt$mpg, "&")
-dt$cyl <- paste(dt$cyl, "$")
-
-kable(dt, "latex", booktabs = T) %>%
- add_header_left(c("aaa aaa aaa aaa aaa" = 2, "bbb" = 3), "A") %>%
- column_spec(8, bold = T) %>%
- column_spec(1, bold = T, width = "1em") %>%
- row_spec(5, bold = T, italic = T)
-```
-
diff --git a/tests/visual_tests/kable_styling_pdf.Rmd b/tests/visual_tests/kable_styling_pdf.Rmd
index ba1f1a2..7acd914 100644
--- a/tests/visual_tests/kable_styling_pdf.Rmd
+++ b/tests/visual_tests/kable_styling_pdf.Rmd
@@ -65,3 +65,8 @@
```
+```{r}
+usepackage_latex("caption", "singlelinecheck=false")
+kable(dt, format = "latex", booktabs = T, caption = "long caption long caption long caption long caption long caption long caption long caption") %>%
+ kable_styling(latex_options = c("striped", "hold_position"))
+```