问题
I'm having difficulty using psych::fa2latex in the context of an RMarkdown document to produce a factor analysis result. If you just run the first bit of code in an RMarkdown document in RStudio (1.0.153), you get an error message : " LaTeX Error: \caption outside float” and the document does not compile. Thanks for any help.
##Place In RMarkdown Document, save and compile.
---
title: "Test"
author: "Simon Kiss"
date: '2018-01-09'
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(psych)
```
```{r code, results='asis'}
data("Thurstone")
mod<-fa(Thurstone, nfactors=3)
fa2latex(mod)
```
However, if you take the raw latex output that is produced by fa2latex() and put it straight into a Sweave document, it compiles just fine.
#Place in Sweave document, save and compile.
\documentclass{article}
\begin{document}
\begin{table}[htpb]\caption{fa2latex}
\begin{center}
\begin{scriptsize}
\begin{tabular} {l r r r r r r }
\multicolumn{ 6 }{l}{ A factor analysis table from the psych package in R } \cr
\hline Variable & MR1 & MR2 & MR3 & h2 & u2 & com \cr
\hline
Sentences & \bf{ 0.90} & -0.03 & 0.04 & 0.82 & 0.18 & 1.01 \cr
Vocabulary & \bf{ 0.89} & 0.06 & -0.03 & 0.84 & 0.16 & 1.01 \cr
Sent.Completion & \bf{ 0.84} & 0.03 & 0.00 & 0.74 & 0.26 & 1.00 \cr
First.Letters & 0.00 & \bf{ 0.85} & 0.00 & 0.73 & 0.27 & 1.00 \cr
Four.Letter.Words & -0.02 & \bf{ 0.75} & 0.10 & 0.63 & 0.37 & 1.04 \cr
Suffixes & 0.18 & \bf{ 0.63} & -0.08 & 0.50 & 0.50 & 1.20 \cr
Letter.Series & 0.03 & -0.01 & \bf{ 0.84} & 0.73 & 0.27 & 1.00 \cr
Pedigrees & \bf{ 0.38} & -0.05 & \bf{ 0.46} & 0.51 & 0.49 & 1.96 \cr
Letter.Group & -0.06 & 0.21 & \bf{ 0.63} & 0.52 & 0.48 & 1.25 \cr
\hline \cr SS loadings & 2.65 & 1.87 & 1.49 & \cr
\cr
\hline \cr
MR1 & 1.00 & 0.59 & 0.53 \cr
MR2 & 0.59 & 1.00 & 0.52 \cr
MR3 & 0.53 & 0.52 & 1.00 \cr
\hline
\end{tabular}
\end{scriptsize}
\end{center}
\label{default}
\end{table}
来源:https://stackoverflow.com/questions/48175808/fa2latex-produces-latex-code-that-does-not-compile-when-produced-in-a-rmarkdown