Search code examples
rformattingr-markdownchunks

Unable to use title format from chunk after figure


I have tried the following R Markdown document (Word output):

cat("##Title 1", "\n")
cat("##Title 2", "\n")

This works fine, both lines are displayed as titles, however:

cat("##Title 1", "\n")
a=c(1,2,3)
b=c(2,2,5)
plot(a,b,type='l')
cat("##Title 2", "\n")

shows Title 1 properly, then the figure, but Title 2 appears as "##Title 2" without title formatting. Both chunks use options 'echo=FALSE'and results='asis'

What am I doing wrong? Why does this happen?


Solution

  • pandoc requires a blank line before the heading, c.f. https://pandoc.org/MANUAL.html#atx-style-headings. Use

    cat("##Title 1", "\n")
    a=c(1,2,3)
    b=c(2,2,5)
    plot(a,b,type='l')
    cat("\n", "##Title 2", "\n")
    

    instead.