inline Latex code inside knitr R block

浪尽此生 提交于 2019-12-13 14:46:29

问题


I am looking for a way to put inline latex code into a R code chunk in Knitr. Here is my example code from the knitr example site :

\documentclass{article}
\begin{document}
Example text outside R code here; we know the value of pi is \Sexpr{pi}.
<<my-label, echo=FALSE, eval=TRUE>>=
set.seed(1213)  # for reproducibility
x = cumsum(rnorm(100))
m <- mean(x)  # mean of x
print(m)
cat(m)
plot(x, type = 'l')  # Brownian motion
@
\textit{Mean is :} \textbf{\Sexpr{m}}
\end{document}

For something simple like this is I could use result='asis' but for a more complicated piece of code, where you want to periodically write the result out to the document, (especially you have complex ggplot graphs), that solution does not work very well.

In the given example, I have 3 queries :

  1. How would I use inline latex code for the output from line 8, in case I wanted to color, bold etc. that text.
  2. Can one eliminate the grey box that appears when we use the cat or print command.
  3. Can the numbering which appears with the print command, which is eliminated with the cat command be eliminated for the print command as well, since print has many variants in many packages for data frames data tables etc. and might be more commonly used to print a portion of data.

In summary, I am mainly looking for the inverse of line 12 in the code.

I have also unsuccessfully tried knit_print with printr, and asis_output, in lieu of print. Although I may have been incorrectly using them.

Thanks!

来源:https://stackoverflow.com/questions/34239835/inline-latex-code-inside-knitr-r-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!