使用knitr的地块的位置

Mar*_*rcG 2 plot r knitr

考虑这两个块:

\subsection{Section figure 1}
\begin{figure}[!h]
<<figure1>>=
plot(1, 1)
@
\caption{Figure 1}
\end{figure}

\newpage

\subsection{Section figure 2}
<<figure2, fig.cap="figure 2", fig.show='asis'>>=
plot(2, 2)
@
Run Code Online (Sandbox Code Playgroud)

该图位于第一个块中的"Section figure x"标题之后,但位于第二个块之前.但是,基于文档,在我看来这两个块应该是相似的.

所以我的问题是:如何使用fig.cap ="figure 2"在标题部分后显示情节.

我尝试这个解决方案:

\subsection{Essai figure 2 bis}
\begin{figure}[!h]
\end{figure}
<<figure2bis, fig.cap="figure 2 bis", fig.show='asis'>>=
plot(2.5, 2.5)
@
Run Code Online (Sandbox Code Playgroud)

但是它会在绘图之前生成一个只包含节标题的页面.

非常感谢

小智 5

您应该使用fig.pos标记将图形位置指定为块选项.在您的示例中,您可以执行以下操作:

\subsection{Section figure 2}
<<figure2, fig.cap="figure 2", fig.show='asis', fig.pos='!h'>>=
plot(2, 2)
@
Run Code Online (Sandbox Code Playgroud)