0

I want to write my thesis in Rmarkdown and am just trying to figure out how to merge multiple .Rmd files into one big file using the example here. It all works fine, but as soon as I add a figure to one of my child files I get an error message. I think it has something to do with the directories but I'm struggling to wrap my head around it. This is what I'm doing:

This is my parent file thesis.Rmd:

---
output:
  pdf_document:
    toc: yes
---

```{r child = 'chapter3/chapter3.Rmd'}
```

```{r child = 'chapter4/chapter4.Rmd'}
```

with

---
title: chapter 3
output: pdf_document:
---

```{r child = '3-1-test.Rmd'}
```

3-1-test.Rmd is just the standard .Rmd file you get when you create a new .Rmd file in Rstudio.

chapter4.Rmd looks like this:

---
title: chapter 4
output: pdf_document:
---

```{r child = '4-1-test.Rmd'}
```

With 4-1-test.Rmd

---
output: 
    pdf_document:
        fig_caption: yes
---

text text text
![caption.\label{4-fig-1}](figure.pdf)

Resulting in an error: pandoc.exe: Could not find image `figure.pdf', skipping...

If I remove the figure everything works just fine. How do I run thesis.Rmd with the figure in 4-1-test.Rmd without getting an error?

Edit: As @rawr suggested, I can make all the file path relative to the thesis.Rmd file. That is a good start. I would also like to know if there is a way to reset the root directory for every child directory (so that within a child directory, the root directory becomes that of the child directory).

Community
  • 1
  • 1
rosannavh
  • 195
  • 1
  • 1
  • 9
  • 1
    the current directory will be the one where thesis.rmd is. where is figure.pdf? did you try to give the path relative to thesis.rmd? – rawr Jul 31 '15 at 04:42
  • @rawr, that works! But than I still don't understand why I don't have to make the path relative to thesis.Rmd in chapter3.Rmd and chapter4.Rmd... Also, I would really like to be able to not have to set all my paths relative to the thesis.Rmd file.. I'm going to change my question a bit more. – rosannavh Jul 31 '15 at 04:56
  • I don't think you can have it so you can compile each chapter individually from within its folder, as well as the master document from the thesis folder. knitr inserts the figure path exactly as given (`figure.pdf` or `chapter4/figure.pdf`) in both cases, and in one of them it'll be wrong. A workaround could be to store chapter 4's pics in `chapter4/figures/`, use `chapter4/figures/figure.pdf` in your documents, and symlink from `chapter4/` so that `./chapter4/figures -> ./figures`. Then the link works in both cases. but not sure if you weird infinite symlinking then, and no Windows support. – mathematical.coffee Jul 31 '15 at 05:10
  • [Here](http://stackoverflow.com/questions/26489328/knitr-how-to-use-child-rnw-docs-with-relative-figure-paths) is another option. Make a function that automagically picks the right path, and instead of `![caption](/path/to/fig)` you ``![caption](`r calcPath('figure.pdf')`)`` – mathematical.coffee Jul 31 '15 at 05:12
  • That is perfect! Thanks @mathematical.coffee – rosannavh Jul 31 '15 at 05:16
  • I tried to do as described [here](http://stackoverflow.com/questions/26489328/knitr-how-to-use-child-rnw-docs-with-relative-figure-paths) but my figures just dissappear (using this: `![Indoors camera test \label{fig3}]ap("figures/detectionzone/factory.png")`. Is this the right way to do it? – rosannavh Aug 03 '15 at 01:32

0 Answers0