ismayc / reedtemplates

(DEPRECATED - Please don't use this!) A template for creating Reed College senior theses using R and Markdown.
Other
27 stars 13 forks source link

knitr citation inside figure caption string #6

Closed irmingard closed 6 years ago

irmingard commented 7 years ago

I want to include a figure where the caption contains a citation like [@Source2016].

Just adding [@Source2016] within the caption string breaks the PDF because subsequent code is printed to the PDF as text (e.g. \begin{figure}[htbp]).

I've also tried escaping the brackets with \[@Source2016\], however that leads to the error message: '\[' is an unrecognized escape in character string

My R chunk:

```{r test-figure, results = "asis", echo = FALSE}
label(path = "figure/test-figure.jpg",
      caption = "Caption text including citation [@Source2016]",
      label = "test-figure",
      type = "figure")


NB: The PDF is generated correctly when the caption is text only.

**Now is there a way to fetch the reference correctly and write it to the caption of the image?**

Maybe the key is to first generate a variable that evaluates an R Markdown reference, however I haven't found a way to do that yet...

Any help is appreciated.
ismayc commented 7 years ago

I haven't had a chance to check this but it would seem you should be able to use the LaTeX workaround here:

label(path = "figure/test-figure.jpg",
      caption = "Caption text including citation \\protect\\cite{Source2016}",
      label = "test-figure",
      type = "figure")

The label function just copies the appropriate LaTeX code and doesn't do another conversion of any Markdown text like [@Source2016]. You might need to compile your TeX document a second time since you are bypassing Pandoc's control of the bibliography here. Hope that works for you!

irmingard commented 7 years ago

The caption then becomes "Caption text including citation [?]". Any other suggestions?

ismayc commented 7 years ago

Did you compile the TeX file again?

irmingard commented 7 years ago

So do I manually compile it and then press knit again? Doesn't that overwrite the manually compiled version? If I compile it manually, how will I get the PDF then without pressing knit again? Sorry for the confusion, I'm new to all of this.

ismayc commented 7 years ago

It appears my suggestion won't work either. I am working on a solution but it may take me a couple weeks. With pandoc taking over the bibiliography there are lots of issues that are difficult to overwrite at times. It appears this functionality is not currently directly supported in pandoc, unfortunately.

irmingard commented 6 years ago

Are there any updates to this issue? As a workaround, I'm citing sources in captions using plain text right now, however that leads to the problem that the source may not get added to the references if I don't cite it anywhere using the @ notation.

ismayc commented 6 years ago

I recommend checking out the updated thesisdown package which has a lot more feature built-in than this package, which was the precursor. thesisdown is built on bookdown so a lot of the pandoc issues are taken care of there. Here is the repo: http://github.com/ismayc/thesisdown.

ismayc commented 6 years ago

@irmingard This might be of use to you: https://github.com/ismayc/thesisdown/issues/27

irmingard commented 6 years ago

Thank you, it actually worked right away for me when I switched to thesisdown. :)