The first chapter {#firstchap}

This is the first chapter with id {#sec:firstchap}

A subchapter {#sec:subchapter}

Another one

This chapter only exists to demonstrate implicit anchors. You should prefer the above explicit ones.

library(knitr)
library(pander)
library(DT)
library(kableExtra)
library(ggplot2)
mcaption = function(tag, caption){
  # Do not use underscores in tag!
  stopifnot(length(grep("_", tag)) == 0)
  cat("<caption>(#tab:", tag, ")", caption, "</caption>", sep="")
}

kable Table

Too bad, caption is always at the top

# Chunk iris, not tab:iris!. Gives Table 1.1
kable(head(iris), caption = "Iris with kable")
kable(head(iris), caption = "Iris with kable")

kableExtra Table

kableExtra also plays well with numbering and captions.

kable(head(iris), caption = "Iris with kableExtra") %>% 
   kable_styling()

pander Table

# Must use echo = FALSE (or globally). 
mcaption("pandertable", "This is a pander caption")
pander(head(iris))

DT Table

mcaption("dtiris", "This is a DT")
DT::datatable(head(iris), caption = "Iris with pander")

flextable

Currently requires a version from github (https://github.com/davidgohel/flextable/pull/205). This was tested with flextable version r packageVersion("flextable").

use_flex = TRUE # set to FALSE to test huxtable
suppressPackageStartupMessages(library(flextable))
ft =  flextable( head( iris ) )
ft = flextable::set_caption(ft, "Iris with flextable")
ft

huxtable

huxtable extends flextable, but cannot be used together with flextable because of a naming conflict. When used alone, it does not show autonumbering. This was tested with huxtable version r packageVersion("huxtable").

suppressPackageStartupMessages(library(huxtable))
hx =  hux( head( iris ) ) 
hx = add_colnames(hx)
hx = huxtable::set_caption(hx, "Iris with huxtable")
hx = huxtable::set_label(hx, "tab:huxiris")
hx

A figure

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width) ) + geom_point()

\begin{equation} \bar{X} = \frac{\sum_{i=1}^n X_i}{n} (#eq:mean) \end{equation}

References to tables, figures and equations

Currently, referencing tables and autonumbering only works for kable, not for pander or DT, promised for flextable and its descendant huxtable.

Links to tables and figures

For pander a link to the chunk label fails \@ref(panderiris), but you can use the mcaption tag using the function defined in this document. Do not forget to put results='asis' into the chunk header.



hughjonesd/huxtable documentation built on Feb. 17, 2024, 12:20 a.m.