Search code examples
rmarkdownfigurebibtexcitations

R Markdown citation broken by fig.align and other chunk options


I'm writing a document in R Markdown and use a Bibtex library for my citations. They work fine when I use them in the text, but give me trouble when I try to implement them in a figure caption.

The Bibtex reference is:

@book{TEST,
    title = {R for Data Science},
    author = {Test Person},
    year = {2018},
}

How it works:

```{r carplot, echo=F, warning=F, fig.cap="This is a Test [@TEST]"}
plot(cars)
```

Ouput: Working citation in figure caption

How it gets broken:

```{r carplot, echo=F, warning=F, fig.cap="This is a Test [@TEST]", fig.align="right"}
plot(cars)
```

Output: enter image description here

I've tried other code chunk options like out.width=".7\\textwidth" and out.extra = 'trim = {0 1.1cm 0 0}, clip' which both cause the citation to break as well. Chunk options like echo=F and warning=F don't seem to be a problem though.

Any Ideas how i can put figure options in the code chunk options whithout it breaking my citation?


Solution

  • I have found a working solution for my problem, even though I still don't unterstand how it was caused in the first place. But for anybody looking for a workaround in the future, here is how I managed to do it:

    (ref:CAP1) This is a Test [@TEST]
    
    ```{r carplot, echo=F, warning=F, fig.cap="(ref:CAP1)", fig.align="right"}
    plot(cars)
    ```
    

    Like this, the fig.align="right" does not seem to be a problem anymore.