`else` should not appear on its own line (it is either after `}` or on the same line as `if`); in this case one-liner is good enough for me:

```s
cap = if (is.null(caption)) '' else sprintf('\n\\caption{%s}', caption)
```