I am using overleaf to compile my R codes into a pdf file. For a chunk of code, we use for example:


But now I want to use these commands inline. I want my output to be like:

"The mean of the data is mean(reading), the maximum is max(reading), and the minimum is min(reading)", where the mean(reading) etc will directly print the output of the command inline. But I don't quite know how to do that.

Please help, and I apologize if this seems trivial, for I am quite new to R.


  • @DavidCarlisle But the chunk I mentioned in the question works in overleaf if we just add the extension .RTex instead of .Tex in the file in overleaf. Jan 24, 2022 at 8:17
  • 1
    sorry my first comment was wrong overleaf.com/learn/latex/Knitr Jan 24, 2022 at 8:40
  • Thanks a lot! It works perfectly now! Jan 24, 2022 at 8:47
  • 1
    if you have working code can you self answer, to get this off the unanswerd list Jan 24, 2022 at 9:18
  • @DavidCarlisle Done. Please check for any corrections. Jan 24, 2022 at 9:29

1 Answer 1


As mentioned in the document in the link posted by David Carlisle in the comments, using $\Sexpr{command}$ works.

For example, if we need the mean of the data inline, we can say, "The mean of the data is $\Sexpr{mean(reading)}$" and same for other commands.

Thanks a lot for helping out.

  • 1
    The output of a function as mean() alone could be a bad idea for inline texts (supposedly to be read for humans). Some rounding, according to the size of mean, is more than advisable to avoid a ridiculous precision (e.g.: \Sexpr{round(mean(x),2)} € to type 9,45 € instead of 9,45226283 €).
    – Fran
    Mar 2, 2022 at 2:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .