Luca T
Luca T

Reputation: 51

knitr not aligning figures to center in pdf output?

Using knitr to make pdfs, the figures don't show when using the fig.align='center' option:

require(knitr)
opts_chunk$set(fig.align='center') 

OR

```{r chunkname, fig.align='center'}
...code that makes figure...
```

Either way, no figures come out on the pdf when pressing the knit PDF button. But I remove the fig.align option and the figures appear, left aligned.

This was an old problem that should have been solved with the 1.8 knitr version, but still have the same problem :(

Any ideas?

Thank you!

Upvotes: 3

Views: 4060

Answers (2)

Luca T
Luca T

Reputation: 51

Problem solved! It wasn a quite "easy" problem, probably due to the laTex rules (I can't program in laTex so I am not sure of this). The problem was in the chuncks identifier. I put some identifier made of multiple words with spaces between a word and another:

'''{r #identifier one}   #before
code
'''

'''{r #identifier-one}   #after
code
'''

Putting a tick between the word solved the problem allowing markdown to render the file as a pdflatex.

Thank you so much anyway!

Upvotes: 2

Denis Rasulev
Denis Rasulev

Reputation: 4069

You need to set GLOBAL options for this. I have this header as standard for my docs, altering it as required:

### Set global and markdown global options

```{r global.options, include = TRUE}
knitr::opts_chunk$set(
    cache       = TRUE,     # if TRUE knitr will cache the results to reuse in future knits
    fig.width   = 10,       # the width for plots created by code chunk
    fig.height  = 10,       # the height for plots created by code chunk
    fig.align   = 'center', # how to align graphics in the final doc. 'left', 'right', 'center'
    fig.path    = 'figs/',  # file path to the directory where knitr shall store the graphics files
    results     = 'asis',   # knitr will pass through results without reformatting them
    echo        = TRUE,     # in FALSE knitr will not display code in the code chunk above it's results
    message     = TRUE,     # if FALSE knitr will not display any messages generated by code
    strip.white = TRUE,     # if FALSE knitr will not remove white spaces at the beg or end of code chunk
    warning     = FALSE)    # if FALSE knitr will not display any warning messages in the final document
```

Also, please, check that you have set output type in the beginning of the doc:

---
title:  "blah-blah"
author: "Denis Rasulev"
date:   "November 2015"
output: pdf_document
---

Upvotes: 4

Related Questions