This is the first chapter with id {#sec:firstchap}
{#sec:subchapter}
.[arbitrary text]{#atext}
. The hash is required.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
TableToo 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
TablekableExtra
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
Tablemcaption("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
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}
Currently, referencing tables and autonumbering only works for kable
, not for pander or DT, promised for flextable
and its descendant huxtable
.
kable
table \@ref(tab:iris) is best seen here. The tab:
is implicit, the chunk must not have the tab:
prefix.kableExtra
table: As the extra Table \@ref(tab:irisextra) shows ....flextable
: Please look at flextable \@ref(tab:flexiris).[An internal link](#atext)
. The hash is required.[Link to figure](#fig:irisf)
, the hash is required.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.
mcaption
function.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.