Avoiding repetitive LaTeX codes in R Markdown

Sorry for being so late. It was a really busy week. I could not understand how to use the hooks effectively for my requirement, though I admit that I went through the materials rather hastily. I decided finally that I will use use captioner for captions for figures (which works fine except that I can't set the aligning), and either the same for tables, or the combination of kable and default markdown format. The default format and kable correctly track the total number of tables already present, but captioner can't. It helped indirecctly though, if one wants to start the counter once again for diffferent sections (I don't know any alternative for that). I tried the following:

---
title: "Example"
date: "28 April 2018"
output: pdf_document
---

```{r}
set.seed(1703)
fig_cap <- captioner::captioner()
tab_cap <- captioner::captioner("Table")
```

Bla...Bla...Bla

```{r}
x <- rnorm(10)
plot(x)
```

`r fig_cap(1, display = "cite")`

Bla...Bla...Bla

```{r}
y <- rcauchy(10)
plot(y)
```

`r fig_cap(2, caption = "this should be shown only figure 2", display = "full")`

Bla...Bla...Bla

```{r}
z <- runif(10)
plot(z)
```

`r fig_cap(3, display = "num")`

Bla Bla Bla

|          x|
|----------:|
|  0.5034229|
|  0.8314031|
|  0.3090216|
| -0.9383080|
|  0.7897246|
| -0.9350987|
|  0.7087313|
|  2.5481824|
|  1.3814061|
|  1.8230985|

`r tab_cap(1, "captioner table one, actually one")`

Bla Bla Bla

`r knitr::kable(cbind(w = sample(100, 10)), caption = "knitrs table one, actually two")`

Bla...Bla...Bla

|           y|
|-----------:|
|  -0.2705723|
|  -0.2776932|
|  -0.2327131|
| -11.7280841|
|  -0.7160195|
|   8.5216133|
|  -0.5755096|
|  -9.6421121|
|   2.7299350|
|   1.5299834|

Table: default table one, actually three

Bla...Bla...Bla

|         z|
|---------:|
| 0.6082432|
| 0.5308447|
| 0.1027917|
| 0.9105425|
| 0.3845761|
| 0.0170002|
| 0.3300129|
| 0.0526674|
| 0.1263493|
| 0.6596739|

`r tab_cap(2, "captioner table two, actually four")`

Bla...Bla...Bla

|          w|
|----------:|
| -0.7160195|
|  0.7897246|
|  1.3814061|
|  0.5034229|
|  0.1027917|
| -9.6421121|
|  0.7087313|
|  8.5216133|
| -0.2776932|
|  0.9105425|

Table: default table one, actually five

Bla...Bla...Bla

It creates the following Captions.pdf (131.4 KB).

If there's a better solution, please let me know.

Thanks.

3 Likes