LaTeX code in knitr figure caption - r

I'm trying to insert LaTeX code into a figure caption created by knitr. I have the code below in a .Rnw file and I'm converting it to a .tex file using knitr:
<<my_plot, fig.cap="X is $\leq$1 and Y is $\geq$2">>=
plot(1,2)
#
Code as it stands give an error. How can I use insert the LaTeX code into the figure caption?

Related

Decide exact position of figures and plot in knitr R Markdown

I had trouble position exactly my plots and figures when knitting to pdf a R Markdown document with knitr.
This question helped me: https://community.rstudio.com/t/cant-control-position-of-tables-and-figures-in-knitted-pdf-document/37364 , where the answerer uses a text file to fit in the latex instructions for the knit.

Printing long code block R markdown pdf

I am trying to print a long code, highlighted with R syntax, at the end of my thesis, written in R markdown. I am knitting to pdf. I receive the error: ! Dimension too large.
I am copy pasting my code into the document:
```R
Pasted code
```
I was wondering if there is a way to avoid the error when printing my code in R markdown.

centre title in PDF converted from markdown using Pandoc

I'm converting a markdown document to a PDF document using Pandoc from within R. I'm trying to centre the title.
So far, I've tried:
<center># This is my title</center>
and
-># This is my title<-
but neither have worked. Is there a way to centre the title when converting from markdown to PDF using Pandoc?
pandoc has its own extended version of markdown. This includes a title block.
If the file begins with a title block
% my title
% Me; Someone else
% May 2013
This will be parsed into LaTeX and the resulting pdf as
\title{my title}
\author{Me \and Someone Else}
\date{May 2013}
and then `
\maketitle
called within the document.
This will create the standard centred title.
If you want to change how the title etc is formatted you could use the titling package.
If you want to change how the section headers are formatted, you could use the titlesec package.
To automagically have pandoc implement these you could define your own template. A simpler option is to have a file with your desired latex preamble to be included in the header. and then use the appropriate arguments when calling pandoc (eg -H FILE or --include-in-header=FILE)

Can I use a knitr inline expression in the title of a latex document?

I'd like to use a Knitr/Sweave in-line call (\Sexpr{}) in the title of a LaTeX document, after the \begin{document} command but before the \maketitle command. The in-line R code would extract one or two pieces of information from an R data-frame created early in the R script I'm embedding in LaTeX.
I have a couple of Knitr chunks that create a data.frame from which I derive the information I want to put in the Title. I've tried placing these chunks between LaTeX's \begin{document} call and the \title code, like this:
\documentclass
[LaTex Preamble]
\begin{document}
[%% Knitr chunks that initialize an R data-frame]
\title \Sexpr{--a snippet of R code that extracts an element from the data-frame --}
\maketitle
... (rest of the LaTeX document)
and I've also tried putting the Knitr chunks in the preamble to the LaTeX code before \begin{document} statement.
But in Knitr seems to ignore code (other than initialization) that is placed ahead of the \maketitle call in LaTeX, so the in-line snippets included the title look like errors to Latex and it halts output.
I can't find any information in the Knitr documentation on including in-line code in the Title of a LaTeX document.
Any ideas?
OK: Found the solution thanks to the hint from #ben-bolker below. Ben uses the formatting of R chunks before output to an RNW file (in a 2-step Knitr process: latex -> rnw -> pdf) . But I'm compiling the LaTeX file to PDF in one-step without going to an RNW file from inside TeXShop (on Mac OSX). I found that I could get Ben's example to work using the RNW delimiters (<<>>=) and one-step compiling. But I couldn't mix the usual LaTeX chunk-delimiters (%%begin.rcode and %% end.rcode) and the RNW in-line statement hook (\Sexpr{}). The latter didn't work no matter how I fiddled with it. Eventually I found that the correct in-line hook for LaTeX is \\rinline{}.
It's not very clear in the Knitr documentation that this is the required format for LaTeX and I found it eventually mainly thanks to Ben's example. Best, Peter
Update 2 ... and then there's RTFM (or the 'cheat sheet' in this case): http://cran.r-project.org/web/packages/knitr/vignettes/knitr-refcard.pdf
Hmm. The following file works for me:
\documentclass{article}
<<echo=FALSE>>=
x <- 5
#
\title{The number is \Sexpr{x^2}}
\begin{document}
\maketitle
Some stuff
\end{document}
with knitr version 0.8 on Ubuntu 10.04, via knit2pdf("knitr_title.Rnw") ...

How to convert .Rmd file to .Rnw file of Rstudio?

I really like using knitr in Rstudio and have been using it to write markdown presentations and data analysis. I want to use the same code and results in a paper and want to convert the code chunks in Rmd file ```{r} to the chunks of Rnw file << >>= #.
This allows using the same document and code written for presentation for the main paper as well.
Is there a way of converting between code chunks of markdown and Rnw files ?
or the entire file itself as apart from the difference in syntax of code chunks, they are quite similar in the markup (converting the text to latex is easy with say pandoc)
Instead of converting the whole document, you can just externalize your R code so it can be shared across different documents; see http://yihui.name/knitr/demo/externalization/
Once you have read_chunk('Rcode.r'), you can use ```{r label} in your Rmd and <<label>>= in your Rnw document, where label comes from the line ## #knitr label in the R script.

Resources