I have a regular parameterized Rmd report that generally works fine. Periodically I need to include a complete .md file as a preface to the normal content, and that second doc contains references to local (same-dir) .png files. Currently, the LaTeX rendering errs with "not found", since the working directory is relative to the original .Rmd file (which is correct), and there is no way with knit_child() to set a local path, nor a way to easily tell the rendering engine where to find the files.
I suspect that LaTeX is not the right space for solving this, it's likely within R's rmarkdown or knitr packages, or something bespoke designed to work around them.
---
params:
rmd:
output:
pdf_document:
pandoc_args: [ "-fmarkdown-implicit_figures" ]
---
# Hello
World.
```{r rmdnotes, echo = FALSE, comment = FALSE, results = "asis"}
rmdout <- character(0)
for (rmd in params$rmd) {
rmdout <- c(rmdout, knitr::knit_child(rmd))
}
cat(rmdout, sep = "\n\n")
```

I named the sub document sub/quux.md, and there exists a PNG file sub/image.png. The fact that they are in neighboring directories is a convenience for this question, that is not an expected condition in reality. Here there is no yaml front matter; if that's a helpful thing, I can add it.
# Another Hello
World.

The number of images varies (can be zero), their names are not pre-defined, and the whole path/filename is variable.
If I render the document with no rmd= parameters, it renders just fine:
rmarkdown::render("~/StackOverflow/r2evans/rmdquestion/main/hello.Rmd")
# processing file: hello.Rmd
# 1/3
# 2/3 [rmdnotes]
# 3/3
# output file: hello.knit.md
# /opt/homebrew/opt/pandoc/bin/pandoc +RTS -K512m -RTS hello.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output hello.tex --lua-filter /Users/r2/Library/R/4.4/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Users/r2/Library/R/4.4/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics -fmarkdown-implicit_figures --variable 'geometry:margin=1in'
# Output created: hello.pdf
If I include the secondary, however, it fails with
rmarkdown::render(
"~/StackOverflow/r2evans/rmdquestion/main/hello.Rmd",
params=list(rmd="~/StackOverflow/r2evans/rmdquestion/sub/quux.md")
)
# processing file: hello.Rmd
# 1/3
# 2/3 [rmdnotes]
# 3/3
# output file: hello.knit.md
# /opt/homebrew/opt/pandoc/bin/pandoc +RTS -K512m -RTS hello.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output hello.tex --lua-filter /Users/r2/Library/R/4.4/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Users/r2/Library/R/4.4/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics -fmarkdown-implicit_figures --variable 'geometry:margin=1in'
# ! Package pdftex.def Error: File `image.png' not found: using draft setting.
# Error: LaTeX failed to compile hello.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See hello.log for more info.
If I copy the .md file and all of its resources (the image.png file here) into the main/ subdirectory, then rendering and inclusion works as hoped. One issue with this approach is that I do not know ahead of time the names of each of the PNGs.
It's not critical that it must be a .md file, if there is a sane way that works on .Rmd files (even if there are no language-chunks to execute), that's fine (and hopefully just a technicality).
Ideally, the rendering environment would recognize the external resources referenced, and either find them naturally or at worst copy them to the "real" rendering working directory. For example, rmarkdown::find_external_resources(..) might be useful ... though for me, attempting to use that function on the console works and in the main Rmd file it fails cryptically.
Edit: some comments have been deleted, I'll try to keep track of relevant ideas/links:
Edit #2: an astute viewer might see that I'm using knitr::knit_child() on a markdown file that is not R-markdown. This was an unintentional oversight. I cannot say that using knit_child("quux.md") does no harm, because the .md suggests I do not want any execution of code blocks yet knit_child() ignores the file extension and executes anyway. A more complex form of a solution might use conditional execution of knit_child() or simply readLines(), still updating the image paths. A more formal/complete solution to this problem might take that into account, but it is not required for this question.
As discussed in the comments, post-processing the knitted child documents to insert absolute paths from the parameter via regex is perhaps the simplest way to go about this, so the chunk might look like the following:
```{r rmdnotes, eval = !is.null(params$rmd), echo = FALSE, comment = FALSE, results = "asis"}
rmdout <- sapply(params$rmd, \(rmd) {
out <- knitr::knit_child(rmd)
out <- gsub("(?<=\\]\\()([^]()]+?)(?=\\))", file.path(dirname(rmd), "\\1"), out, perl = TRUE)
})
cat(rmdout, sep = "\n\n")
```