4

Since there's no working solution to my prior question, I tried to just place a citation exactly under a figure. This works until the figure experiences an automatic page-break. Then, the citation is placed where I meant to place the figure, but the figure itself appears on the next page and both get separated. Does anyone know the remedy?

Here is my code and a screenshot:

---
title: "Test"
output:
  pdf_document: default
references:
- id: hawking_thermodynamics_1983
  author:
  - family: Hawking
    given: S. W.
  - family: Page
    given: Don. N.
  publisher: Communications in Mathematical Physics
  title: Thermodynamics of Black Holes in Anti-de Sitter Space.
  volume: 87
  type: article-journal
  issued:
    year: 1983
header-includes:
- \usepackage{lipsum}  # just used for producing example text in document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Headline
\lipsum[1-5]

\begin{figure}[h]
\centering
\includegraphics[width=10cm]{example.png}
\caption{Example figure}\label{fig1}
\end{figure}
[source: @hawking_thermodynamics_1983]

\lipsum[1-4]

## Bibliography

EDIT: This is for a *.bib version:

@article{hawking_thermodynamics_1983,
    title = {Thermodynamics of black holes in anti-de Sitter space},
    volume = {87},
    pages = {577--588},
    number = {4},
    journaltitle = {Communications in Mathematical Physics},
    author = {Hawking, S. W. and Page, Don N.},
    date = {1983-12},
}

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • The normal pandoc/rmarkdown syntax for figures is a paragraph which contains just an image; the caption is taken from the image descriptions: `[source: @hawking_thermodynamics_1983](example.png){width=10cm}`. Would that me an option? – tarleb Dec 25 '17 at 20:34

2 Answers2

3

You can include the [source: <cite>] as part of your figure:

\begingroup
\figure[h]
  \centering
  \includegraphics[width=10cm]{example.png}
  \caption{Example figure}\label{fig1}
  [source: @hawking_thermodynamics_1983]
\endfigure
\endgroup

The above simulates the same grouping suggested by \begin{figure}[h]...\end{figure} without the explicit use of the figure environment; the Rmarkdown process skips over and assumed everything within a LaTeX environment (like figure) will contain LaTeX-only code, so it does not recognize the markdown syntax [source: @<cite>] but instead complains that <cite> contains an underscore, which can only be used inside math-mode.

Alternatively, if you must, add the float package to your YAML preamble

header-includes:
- <other packages>
- \usepackage{float}

and then use the [H] float specifier to require the float to stay Here:

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{example.png}
  \caption{Example figure}\label{fig1}
\end{figure}
[source: @hawking_thermodynamics_1983]

Note that this will disrupt the regular setting of text if the float can't fit on the page.

Werner
  • 14,324
  • 7
  • 55
  • 77
  • The alternative with `float` is actually working, thanks!!! Sure the interruption—but this way I can better play around placing the figure manually until there eventually comes a working solution to my [prior question](https://stackoverflow.com/q/47860390/6574038). The first option would have been intuitive which I surely tried before but threw me an error `! Missing $ inserted.`; I should have mentioned this in my question. – jay.sf Dec 26 '17 at 11:39
  • 1
    @jaySf: I've supplied an option to address the issue with `[source: @]` inside the `figure` environment. – Werner Dec 26 '17 at 16:31
  • This is great stuff, exactly what I needed! It's even better than the `float` solution since there's nothing disrupted like text till the bottom of the page, footnotes etc. Even the citation is centered and in normal distance to figure caption and text. Thanks! – jay.sf Dec 26 '17 at 17:00
0

When you are using a bib file, it might be possible to use the stackengine package and \cite{}:

\usepackage{stackengine}

% ....

\begin{figure}[h]
\centering
\stackunder[5pt]{\includegraphics[width=10cm]{unnamed.png}}{\cite{hawking_thermodynamics_1983}}%
\caption{Example figure}\label{fig1}
\end{figure}

enter image description here

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98