# Caption above figure using knitr

This is a question originally asked on stackoverflow, but it seems to receive no attention and answers there. Since it's related to LaTeX, I'm posting it also here hoping to receive some hints.

I would like to place the caption above the figure using knitr in Texmaker. I know that this question has already been asked, and I understand that the solution suggested so far is to use:

\begin{figure}
\caption{This is a caption above the figure}
<<a-plot, echo=FALSE>>=
plot(1)
@
\end{figure}


But in this way I cannot show the code (given echo=FALSE), while I would like to show the code for R and the graph plotted with that R codes, with the caption above the graph.

This is a bit of a kludge but accomplished what you want.

\documentclass{article}
\usepackage{filecontents,fancyvrb}
\begin{document}

\begin{filecontents*}{test190893a.Rnw}
#<<a-plot, echo=FALSE,out.width='3in'>>=
#plot(1)
#@
\end{filecontents*}

\VerbatimInput[gobble=1]{test190893a.Rnw}

\textbf{This is a caption above the figure}

\begin{flushleft}
<<a-plot, echo=FALSE,out.width='3in'>>=
plot(1)
@
\end{flushleft}

\end{document}


Use fig.show='hide'

<<a-plot, echo=TRUE, fig.show='hide'>>=
plot(1)
@

\begin{figure}
\caption{This is a caption above the figure}
\includegraphics{figure/a-plot-1}
\end{figure}


N.B. the figure name has suffix -1.