Search code examples
rknitrcaptionxtable

xtable caption alignment left aligned with table or centered (using knitr)


I'd like to align an xtable figure caption left adjusted with the table, or if that doesn't work, at least centered, because I think it looks kind of stupid a small table being centered and the corresponding caption being left aligned. Also, left aligning both table and caption is not optimal.

It should be possible using latex.environments = "left", but I get an error message, just like in this post.

I don't think the problem lies in the referencing, because I get the same error message with and without label="myLabel".

Here a MWE:

<<table, echo=FALSE, results='asis'>>=
print(xtable(lm(mpg~hp, data=mtcars), caption="Linear Model", label="myLabel") , caption.placement = "top", latex.environments="left")
@

The error (short form, for long version see this post again):

Missing $ inserted.
Missing delimiter (. inserted)
Missing $ inserted
Missing \right. inserted

Does somebody know a solution? Left aligned with left side of the table or centered, I'd be happy with any solution.


Solution

  • Ok so I found a solution to at least center the table captions, using package caption, specifying

    \usepackage[
        singlelinecheck=false,
        justification=centering
    ]{caption}