cite figure in other figure caption in markdown to pdf












1














Using R markdown with PDF output, I want to cite a figure into another figure caption. I also want to cite a BibTex reference in the caption. Any ideas? Here's an example of code:



---
title: "Untitled"
author: "me"
date: "today"
output:
pdf_document:
latex_engine: lualatex
number_sections: no
linestretch: 1.5
bibliography: input/Library.bib
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```

```{r}
df <- mtcars

library(ggplot2)
```

```{r, fig.cap="some stuff"}
ggplot(df, aes(cyl, mpg)) + geom_point()

```



```{r, fig.cap="some more stuff. here I'd like to cite figure 1. I would also like a BibTex citation"}
ggplot(df, aes(cyl, hp)) + geom_point()
```









share|improve this question



























    1














    Using R markdown with PDF output, I want to cite a figure into another figure caption. I also want to cite a BibTex reference in the caption. Any ideas? Here's an example of code:



    ---
    title: "Untitled"
    author: "me"
    date: "today"
    output:
    pdf_document:
    latex_engine: lualatex
    number_sections: no
    linestretch: 1.5
    bibliography: input/Library.bib
    ---

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
    ```

    ```{r}
    df <- mtcars

    library(ggplot2)
    ```

    ```{r, fig.cap="some stuff"}
    ggplot(df, aes(cyl, mpg)) + geom_point()

    ```



    ```{r, fig.cap="some more stuff. here I'd like to cite figure 1. I would also like a BibTex citation"}
    ggplot(df, aes(cyl, hp)) + geom_point()
    ```









    share|improve this question

























      1












      1








      1







      Using R markdown with PDF output, I want to cite a figure into another figure caption. I also want to cite a BibTex reference in the caption. Any ideas? Here's an example of code:



      ---
      title: "Untitled"
      author: "me"
      date: "today"
      output:
      pdf_document:
      latex_engine: lualatex
      number_sections: no
      linestretch: 1.5
      bibliography: input/Library.bib
      ---

      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
      ```

      ```{r}
      df <- mtcars

      library(ggplot2)
      ```

      ```{r, fig.cap="some stuff"}
      ggplot(df, aes(cyl, mpg)) + geom_point()

      ```



      ```{r, fig.cap="some more stuff. here I'd like to cite figure 1. I would also like a BibTex citation"}
      ggplot(df, aes(cyl, hp)) + geom_point()
      ```









      share|improve this question













      Using R markdown with PDF output, I want to cite a figure into another figure caption. I also want to cite a BibTex reference in the caption. Any ideas? Here's an example of code:



      ---
      title: "Untitled"
      author: "me"
      date: "today"
      output:
      pdf_document:
      latex_engine: lualatex
      number_sections: no
      linestretch: 1.5
      bibliography: input/Library.bib
      ---

      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
      ```

      ```{r}
      df <- mtcars

      library(ggplot2)
      ```

      ```{r, fig.cap="some stuff"}
      ggplot(df, aes(cyl, mpg)) + geom_point()

      ```



      ```{r, fig.cap="some more stuff. here I'd like to cite figure 1. I would also like a BibTex citation"}
      ggplot(df, aes(cyl, hp)) + geom_point()
      ```






      r latex r-markdown pandoc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 1:00









      Tony D

      1118




      1118
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:



          ---
          title: "Untitled"
          author: "me"
          date: "today"
          output:
          bookdown::pdf_document2:
          latex_engine: lualatex
          number_sections: no
          bookdown::html_document2:
          default
          linestretch: 1.5
          bibliography: packages.bib
          ---

          ```{r setup, include=FALSE}
          knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
          knitr::write_bib(c(.packages()), 'packages.bib')
          ```

          ```{r}
          df <- mtcars

          library(ggplot2)
          ```

          ```{r stuff, fig.cap="some stuff"}
          ggplot(df, aes(cyl, mpg)) + geom_point()

          ```



          ```{r, fig.cap="some more stuff. here I'd like to cite figure \@ref(fig:stuff). I would also like a BibTex citation [@R-base]"}
          ggplot(df, aes(cyl, hp)) + geom_point()
          ```


          For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.






          share|improve this answer























          • That doesn't generalize to html_output. It would be nice to have a solution that does.
            – Frank Harrell
            Nov 24 '18 at 13:34






          • 1




            @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
            – Ralf Stubner
            Nov 24 '18 at 22:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439541%2fcite-figure-in-other-figure-caption-in-markdown-to-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:



          ---
          title: "Untitled"
          author: "me"
          date: "today"
          output:
          bookdown::pdf_document2:
          latex_engine: lualatex
          number_sections: no
          bookdown::html_document2:
          default
          linestretch: 1.5
          bibliography: packages.bib
          ---

          ```{r setup, include=FALSE}
          knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
          knitr::write_bib(c(.packages()), 'packages.bib')
          ```

          ```{r}
          df <- mtcars

          library(ggplot2)
          ```

          ```{r stuff, fig.cap="some stuff"}
          ggplot(df, aes(cyl, mpg)) + geom_point()

          ```



          ```{r, fig.cap="some more stuff. here I'd like to cite figure \@ref(fig:stuff). I would also like a BibTex citation [@R-base]"}
          ggplot(df, aes(cyl, hp)) + geom_point()
          ```


          For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.






          share|improve this answer























          • That doesn't generalize to html_output. It would be nice to have a solution that does.
            – Frank Harrell
            Nov 24 '18 at 13:34






          • 1




            @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
            – Ralf Stubner
            Nov 24 '18 at 22:06
















          1














          In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:



          ---
          title: "Untitled"
          author: "me"
          date: "today"
          output:
          bookdown::pdf_document2:
          latex_engine: lualatex
          number_sections: no
          bookdown::html_document2:
          default
          linestretch: 1.5
          bibliography: packages.bib
          ---

          ```{r setup, include=FALSE}
          knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
          knitr::write_bib(c(.packages()), 'packages.bib')
          ```

          ```{r}
          df <- mtcars

          library(ggplot2)
          ```

          ```{r stuff, fig.cap="some stuff"}
          ggplot(df, aes(cyl, mpg)) + geom_point()

          ```



          ```{r, fig.cap="some more stuff. here I'd like to cite figure \@ref(fig:stuff). I would also like a BibTex citation [@R-base]"}
          ggplot(df, aes(cyl, hp)) + geom_point()
          ```


          For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.






          share|improve this answer























          • That doesn't generalize to html_output. It would be nice to have a solution that does.
            – Frank Harrell
            Nov 24 '18 at 13:34






          • 1




            @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
            – Ralf Stubner
            Nov 24 '18 at 22:06














          1












          1








          1






          In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:



          ---
          title: "Untitled"
          author: "me"
          date: "today"
          output:
          bookdown::pdf_document2:
          latex_engine: lualatex
          number_sections: no
          bookdown::html_document2:
          default
          linestretch: 1.5
          bibliography: packages.bib
          ---

          ```{r setup, include=FALSE}
          knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
          knitr::write_bib(c(.packages()), 'packages.bib')
          ```

          ```{r}
          df <- mtcars

          library(ggplot2)
          ```

          ```{r stuff, fig.cap="some stuff"}
          ggplot(df, aes(cyl, mpg)) + geom_point()

          ```



          ```{r, fig.cap="some more stuff. here I'd like to cite figure \@ref(fig:stuff). I would also like a BibTex citation [@R-base]"}
          ggplot(df, aes(cyl, hp)) + geom_point()
          ```


          For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.






          share|improve this answer














          In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:



          ---
          title: "Untitled"
          author: "me"
          date: "today"
          output:
          bookdown::pdf_document2:
          latex_engine: lualatex
          number_sections: no
          bookdown::html_document2:
          default
          linestretch: 1.5
          bibliography: packages.bib
          ---

          ```{r setup, include=FALSE}
          knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
          knitr::write_bib(c(.packages()), 'packages.bib')
          ```

          ```{r}
          df <- mtcars

          library(ggplot2)
          ```

          ```{r stuff, fig.cap="some stuff"}
          ggplot(df, aes(cyl, mpg)) + geom_point()

          ```



          ```{r, fig.cap="some more stuff. here I'd like to cite figure \@ref(fig:stuff). I would also like a BibTex citation [@R-base]"}
          ggplot(df, aes(cyl, hp)) + geom_point()
          ```


          For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 22:05

























          answered Nov 23 '18 at 7:36









          Ralf Stubner

          13.7k21437




          13.7k21437












          • That doesn't generalize to html_output. It would be nice to have a solution that does.
            – Frank Harrell
            Nov 24 '18 at 13:34






          • 1




            @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
            – Ralf Stubner
            Nov 24 '18 at 22:06


















          • That doesn't generalize to html_output. It would be nice to have a solution that does.
            – Frank Harrell
            Nov 24 '18 at 13:34






          • 1




            @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
            – Ralf Stubner
            Nov 24 '18 at 22:06
















          That doesn't generalize to html_output. It would be nice to have a solution that does.
          – Frank Harrell
          Nov 24 '18 at 13:34




          That doesn't generalize to html_output. It would be nice to have a solution that does.
          – Frank Harrell
          Nov 24 '18 at 13:34




          1




          1




          @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
          – Ralf Stubner
          Nov 24 '18 at 22:06




          @FrankHarrell For HTML output you can use bookdown::html_document2. See updated answer.
          – Ralf Stubner
          Nov 24 '18 at 22:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439541%2fcite-figure-in-other-figure-caption-in-markdown-to-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...