Producing subscripts in R markdown - r

I'm aware that R markdown can produce superscripts:
text^superscript
But is it possible to produce proper subscripts? Or is the only way to do so to cheat and use LaTeX math mode:
$\sf{text_{subscript}}$
The intended final output is HTML.

R Markdown subscript is working normally as it should.
Maybe this is an old post. I'm using RStudio Version 0.99.902 + R Version 3.4 on a Mac.
Subscript: H~2~O is a liquid.
Superscript: 2^10^ is 1024.

Since you mention Pandoc in your comments, maybe it's not cheating to depend on Pandoc's extensions for subscript and superscript. From here, we can create a minimal example Rmd file:
Testing Subscript and Superscript
========================================================
This is an R Markdown document.
Pandoc includes numerous extensions to markdown, and one
of them is *subscript* and *superscript*.
Here's the example from the Pandoc help page
(http://johnmacfarlane.net/pandoc/README.html#superscripts-and-subscripts):
H~2~O is a liquid. 2^10^ is 1024.
For fun, here's an R code block with some code from #Spacedman:
```{r}
list.depth <- function(this, thisdepth = 0) {
# http://stackoverflow.com/a/13433689/1270695
if(!is.list(this)) {
return(thisdepth)
} else {
return(max(unlist(lapply(this, list.depth, thisdepth = thisdepth+1))))
}
}
```
Using Knitr results in an HTML file that renders like this:
That clearly doesn't work. But you can run pandoc on the resulting markdown file (which I've named "Subscripts.md"):
pandoc -o Subscripts.html Subscripts.md -s -S
and you'll get this:
The CSS is different, but perhaps you can call pandoc with a custom CSS argument to use the same CSS used by Knitr.
Subscripts in PDF files also work as expected with that markdown file:
pandoc -o Subscripts.pdf Subscripts.md
Edit
If you want the pandoc output to match the visual appearance of the output when you knit with RStudio, download the CSS file that RStudio uses here and make a reference to that file when you create your HTML file from pandoc. (The following assumes you have kept the name as markdown.css an it is in the same directory as your other files.)
pandoc -o Subscripts.html Subscripts.md -s -S --css=markdown.css

I found that the X~j~ syntax for subscripts works fine in Rmarkdown when knitting in RStudio. However, it does not work if you embed knitting in a shiny app. In my app,
knit2html("Steps.Rmd")
browseURL("Steps.html")
works fine except for the subscripts.
But vanilla HTML subscript syntax will work in your Rmd document for both RStudio and from within a shiny app: X<sub>j</sub> renders as Xj.

For R version 4.0.2 (2020-06-22) this works for me:
Subscript H~2~O~
Superscript R^2^

Related

! LaTeX Error: File `knitr.sty' not found

I am trying to knit a simple markdown file to a pdf using knitr and R Studio. The chunks all run fine and I have no problems knitting to an HTML document, but get the following error when trying to knit to a pdf:
! LaTeX Error: File `knitr.sty' not found.
I have reinstalled tinytex and made sure that the knitr and rmarkdown packages are up to date. I've looked around and while there are similar issue with other *.sty files, I can't find anything relating to this one.
Thanks in advance for any advice
Solved this: for some reason I had listed several R packages in the 'extra_dependencies' part of the YAML metadata. Once removed it worked.

Using dsfont package in R Markdown via R Studio

So I can't for the life of me including this dsfont package in R Markdown. I really want to use the double-stroke letters.
I include it in the header like so:
header-includes:
- \usepackage{dsfont}
And I use $\mathds{P}$, for example to denote probability.
The file knit fine with no error but the HTML output is \mathdsP, i.e. it doesn't work.
I tried downloading the dsfont zip directly from CTAN but when I click download nothing happens.
What do I need to do to make this work?
\usepackage is a command for PDF output.
It is not possible to use dsfont with HTML output.

R package knitr misses R chunks

I haven't been able to use the neater knitter package with the code chunks.
Basically there's only a few number of occasions in which it interprets them well, but for the most of it the chunks are not recognized as such. That is:
Running a markdown from RStudio only renders chunks before the file is saved. If the file has been saved, then it will show the code as is: no R output.
I also tried using knitr from within LyX, and this presented other problems. The simplest example knitr.lyx was rendered correctly as a pdf, but not html. Using more complicated documents, like the RJournal template showed other errors.
Rscript --verbose --no-save --no-restore
At first I thought it had to do with my Rstudio installation, but now I'm not so sure anymore.
By the way, I'm on Ubuntu 15.04 and the files that I'm using are examples from the documentation:
Rstudio > New File > Rmarkdown... renders R output only before it's saved.
knitr's manual in LyX from Github repo.
I found the answer for this.
It's very obvious and yet easy to overlook if you don't know it.
The problem was in saving the file with the wrong extension.
If you're using R code chunks you need .Rmd extension in Rstudio.
As I was starting with markdown I was using .md. Pfff.

pandoc.convert pdf formatting in R

I am new to R and am trying to convert a markdown document to PDF using the Pandoc.convert function. Pandoc.convert creates the PDF just fine, although the border on the document is ridiculously large. Is there a way to set the border in Pandoc.convert to a smaller border?
Example used:
Pandoc.convert(f="myfile.md", format="pdf", options="-s")
I have tried looking in the Pander package help, and on the Pander site and found no result for Pandoc.convert.
There are three option in pandoc, including: -V geometry:margin=1in
Although, I have found none for Pandoc.convert specifically. Is this even possible with just the Pandoc.convert command, without getting into LaTeX?
Pass any number of further options to Pandoc simply by extending your options argument, e.g.:
Pandoc.convert(f="myfile.md", format="pdf", options="-s -V geometry:margin=1in")

workflow between Latex and R screwed

Trying to write my second Latex file
\documentclass{article}
\title{simple Sweave script}
\usepackage{/Users/Brenden/Documents/R/R-2.15.1/share/texmf/tex/latex/Sweave}
\begin{document}
\maketitle
This is a simple script to demonstrate Sweave. Let's
begin by generating some results
<<>>=
x=rnorm(30)
y=rnorm(30)
mean(x)
cor(x,y)
#
and follow that up with some random text
\end{document}
File saved with .Rnw as extension. I could then use Sweave under R to convert the file to tex. I then run pdflatex under cmd to get my .pdf file. R is stored under /Users/Brenden/Documents. MiKTex is stored under /Users/Brenden/Desktop.
It is said that usually the line of "usepackage" is not needed, as when I run Sweave under R, a line of "usepackage{Sweave}" will be added to the tex file which is stored under /Users/Brenden/Documents. However, if I don't put in userpackage line, when I run pdflatex under cmd (either under /Documents or /Desktop), I got a messsage that says "Sweave.sty not found". So I have been through this trouble by always adding a line of usepackage with a detailed path to help circumvent the problem. Although the "Sweave.sty not found" problem is circumvented, I do notice that when I run pdflatex under cmd, I got the response
LaTeX Warning: You have requested package '/Users/Brenden/Documents/R/R-
2.15.1/share/texmf/tex/latex/Sweave', but the package provides "Sweave'.
Then it runs through several files with .sty under MiKTex
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\base\ifthen.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\graphicx.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\keyval.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\graphics.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\trig.sty)
...
to eventually create a .pdf
From another post on Stackoverflow, it is said, "That path is set automatically when you install LaTeX; it's your personal texmf tree and you can put any style or class files you want LaTeX to find there. The directory names don't matter, it searches recursively in them for Sweave.sty". To me, however, clearly my MiKTex could not find the Sweave.sty unless I specify the path. And even with the path specification, LaTex still gives me a warning. Could someone explain to me where I screwed up (during installing MiKTex, perhaps? ) so that I could help MiKtex find its way to Sweave without my specifying the path?
Thank you.
In the MiKTeX Settings program, there is a tab called "Roots". It is there that you can specify where additional .sty files can be found. In your case, I believe that you will want to add C:\Users\Brenden\Documents\R\R-2.15.1\share\texmf as an additional root. Doing this, any of the TeX programs will be able to find Sweave.sty whether run from inside R or from the command line.
It is annoying to be always distracted by little devils like this (new concept texmf tree, endless environment variables TEXINPUTS, SWEAVE_STYLEPATH_DEFAULT, ...). You will never worry about LaTeX packages if you use knitr, and if you like the Sweave style, you can simply put render_sweave() in the document like:
\documentclass{article}
\title{simple Sweave script}
<<setup, include=FALSE>>=
render_sweave()
#
\begin{document}
\maketitle
This is a simple script to demonstrate Sweave. Let's
begin by generating some results
<<>>=
x=rnorm(30)
y=rnorm(30)
mean(x)
cor(x,y)
#
and follow that up with some random text
\end{document}
Save it as, say, test.Rnw and compile it with
library(knitr); knit('test.Rnw') # or knit2pdf('test.Rnw')
And I guess you are probably better off without render_sweave() (i.e. use the default LaTeX style in knitr).
I suspect the path referred to in the other post is the path to your personal texmf tree, which is not where Sweave puts it. One option is to move it to your personal texmf tree, but the usual method nowadays, I believe, is to run pdflatex from within R, like this:
texi2dvi("foo.tex", pdf = TRUE)
I think you can also have Sweave add the full path automatically by adding
export SWEAVE_STYLEPATH_DEFAULT="TRUE"
to your .bashrc file, or whatever the equivalent would be on your system.
At the command line, R CMD sets up the environment with variables appropriate for running R, for instance on Windows
C:\Users\mtmorgan> "c:\Program Files\R\R-2.15.1\bin\R" CMD set
includes (this is not found if one types set at the command line)
TEXINPUTS=.;c:/PROGRA~1/R/R-215~1/.1/share/texmf/tex/latex;
and so
C:\Users\mtmorgan> "c:\Program Files\R\R-2.15.1\bin\R" CMD pdflatex myfile.tex
will find Sweave.sty. Also, tools::texi2dvi can be invoked from within R.
I eventually end up with the following solution:
install.packages("tinytex")
require("tinytex")}
install_tinytex(force = TRUE)
Sweave( "Report.Rnw" , encoding = "utf8" )
xelatex('Report.tex')
This code install TinyTex, then you can compile with pdflatex(), xelatex() or lualatex().

Resources