Before you say use search, I did, I read this answer, I tried it and it does not help, I assume it might be because of including it from another file. (not sure..)
I have a Sweave document I am writing using R studio. Running R code works perfectly, including other files works as well. Here are some examples of what the main file looks like:
\documentclass{report}
\begin{document}
\SweaveOpts{concordance=TRUE}
\maketitle
\tableofcontents
\input{tex/introduction}
\input{tex/methods}
\SweaveInput{tex/analysis}
\input{tex/conclusions}
\SweaveInput{tex/further}
\bibliographystyle{plain}
\bibliography{tex/literature}
\appendix
\input{tex/appendix}
\end{document}
Then in one of the .Rnw
files referenced by \SweaveInput{}
I use something like this:
\begin{figure}[h]
\label{fig1}
\caption{This is a caption!}
\centering
<<fig=TRUE, echo=FALSE>>=
...data and plotting...
@
\end{figure}
In figure \ref{fig1} we can see that there is...
and instead of a figure number I just get [??]
Do you maybe know why this is happening? and how can I fix it?
I looked at the knitr
package as well but I don't know how to include an external file like in my example and I get other funny errors. Also, I don't know if it will work there. Basically I would have to do more research why my code doesn't work and I would just like to get the current code running.
Tnx.
label
needs to come after caption
. See for example, https://tex.stackexchange.com/q/23385