# passing a variable from R to Tex in knitr

I am sure this has been asked multiple times since it seems to be a common issue. I looked around but all I could find was the \Sexpr for sweave which does what I want. I am using knitr and I can't find a way to pass the variables from the Rcode chunk to Tex. I want is the variable a to be passed from knitr to TEX as given below. Is this possible? I get the error:

 object 'a' not found


The sample.Rtex file is.

\documentclass{article}
\begin{document}
\section{Summary}
\begin{figure}[!h]
%% begin.rcode hist1, echo=FALSE, cache=TRUE, results=FALSE, dev='cairo_pdf', fig.width=8,  fig.align='center', out.width='.9\\textwidth'
% temp_setup_vars(File_1, File_1A, File_2, File_2A)
% generate_graph()
% a = 7
%% end.rcode
\caption{Hist}
\label{fig:hist}
\end{figure}

\section{Conclusion}
\par A is \Sexpr{a}.

\end{document}


## 1 Answer

You need to respect the fact that you're using Rtex format and not Rnw format. See the syntax table in section 1 of the knitr reference card : in particular, you need rinline{} rather than \Sexpr{}. I think single rather than double % might also help.

\documentclass{article}
\begin{document}
\section{Summary}
\begin{figure}[!h]
% begin.rcode hist1,results="hide",echo=FALSE
% a <- 7
% end.rcode
\caption{Hist}
\label{fig:hist}
\end{figure}

\section{Conclusion}
\par A is \rinline{a}.

\end{document}