pandoc.convert pdf formatting in R - 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")

Related

What is the best way to write down in-line math equations in a jupyter notebook to have best LaTeX results?

When I am writing math equations in a markdown cell of a jupyter notebook, I casually put all of that in $ ... $. Today, I converted an .ipynb file to .tex, and realized most of these equations are converted into messy stuff in the output pdf. For instance, I realized it is converting all '$'s into '$'s.
This is an example of what has happened:
Jupyter Notebook markdown:
$ \nabla ^{2} f(x) = \frac{-1}{(x+1)^{2}} $
Tex Output:
\$ \nabla \^{}\{2\} f(x) = \frac{-1}{(x+1)^{2}} \$
Does anyone have any ideas why this is happening? Is there a better way to write down in-line math equations so that it is more compatible with LaTeX?
If you are eventually going to LaTeX version, the traditional advice is spelled out in minrk's comment from November 8 of 2012:
"The best solution for that right now would be to use 'raw' cells instead of markdown, and just type LaTeX as you would. Then use nbconvert to turn the ipynb to TeX (code, figures and all), and run latex to render that to PDF, etc. You don't get live-rendered TeX in the browser like you do with MathJax / Markdown, but you do still have TeX / code in one document."
The step where you are converting now is probably using nbconvert under the hood even if you aren't directly. (You may be using it directly since you tagged with 'nbconvert`.)
Alternatively, if you are going to LaTeX ultimately, you may want to use a code cell and use the LaTeX magic cell line at the start of that cell. See here and here about %%latex cell magic. (You'll note it is also mentioned among the StackOverflow thread I referenced earlier.) In regards to the %%latex cell magic, I suggest actually consulting the link that leads use in an example notebook in this post because it seems it has to be full-blown LaTeX code and not just simple equations that work easily elsewhere with just dollar signs bracketing them, i.e., MathJax.
I tried that option with your equation and when I output the notebook as LaTeX, I didn't see additional backslashes added. (I didn't however actually try rendering the LaTeX, and so I cannot address if all the cruft/boilerplate that Jupyter is adding causes any issues for downstream useability.)

Syntax highlight in R Sweave

I am fairly new to doing report with R Sweave and know the very basic applications of Latex. And I have been asked to produce some statistical reports. The R markdown is great and simple, and by default it has really nice syntax frame and grey background and syntax highlights, however, it is quite limited in terms of other type setting, not really optimal when you want to produce lengthy reports. Then I am switching to use R Sweave in R studio.
I basically want the same after-effect similar to R markdown in the Sweave. What are the easiest ways to do it? I have previously read the following post discussing:
Sweave syntax highlighting in output. And I have tried reading those package pdf, but have no clues what they are talking about, as they seem to assume readers have prior knowledges about the rendering process.
i have checked them out, but I seem to get stuck in making it to work. Can anyone tell me step by step on how to set it up (such as what to include in preamble), if possible can you kindly upload a simple Rnw file with a demonstration?
Thank!
If you use knitr rather than Sweave, you'll get syntax highlighting. It's probably possible to do it in Sweave, but knitr makes it easier.
Go to your Tools | Global Options | Sweave menu (or the similar one in Project Options) in RStudio, and choose to Weave Rnw files using knitr.
The two systems are very similar, but knitr is generally preferable these days.

Knitting R HTML help documents with rgl examples

knitr provides a neat function to compile R HTML help documents which contain evaluated examples embedded in the document. This is achieved with the knit_rd function, which has some simple input arguments
packageVersion('knitr')
[1] ‘1.12.3’
args(knit_rd)
function (pkg, links = tools::findHTMLlinks(), frame = TRUE)
However, when the examples contain rgl code for interactive graphics these do not get embedded in the document (but I'd sure like them to be!). But I know it is possible to embed rgl into knitr documents, but I don't see an easy way to do this with knit_rd(). Is there a simple way to accomplish this?
Edit:
Here's a situation that I'm working on (BASH commands):
mkdir matlib-dir
cd matlib-dir
git clone https://github.com/philchalmers/matlib.git
Rscript -e "library('knitr');knit_rd('matlib')"
Of the various files generated documents like the vectors3d.html contain rgl code, but nothing really there because they have not been embedded.
I think this isn't currently possible --- it would need modifications to knit_rd to call rglwidget() after each rgl call, or modifications to rglwidget to make that automatic. I'm working on the latter possibility, but it won't appear soon.

knitr pandoc: "cannot produce pdf output with pdf writer"

Up front: using pandoc() in knitr, it complains when trying to compile .md or .Rmd into a PDF.
I'm streamlining the process for reproducible research, as has been documented in many places. I'm using pandoc and knitr and producing great documents. I'm also trying to streamline for some co-workers who are not as adept with programming, yet we're trying to use similar files. There are several options for "user friendly" markdown-centric editors, and for several reasons I'm leaning on RStudio (for them, emacs/ess for me, but that's different).
My workflow: give them a markdown (.md or .Rmd) file and have them be able to make changes and optionally re-render it into a PDF. Unfortunately, RStudio does not (AFAICT) allow setting templates or other arbitrary pandoc configuration parameters (e.g., chapters, number-sections), so using pandoc() in R/knitr makes a lot of sense here.
Using whitepaper.Rmd as the input file, I run pandoc('whitepaper.Rmd', 'pdf') in R and immediately get:
> pandoc('whitepaper.Rmd', 'pdf')
executing pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -t pdf -o whitepaper.pdf "whitepaper.Rmd"
pandoc.exe: cannot produce pdf output with pdf writer
Error in (function (input, format, ext, cfg) : conversion failed
I explicitly have "t:latex" in my knitr-specific header, though without it, pandoc() is still adding "-t pdf" to the system call, something that pandoc.exe does not accept.
With troubleshooting, the command works just fine if I remove '-t pdf', so it seems that there is nothing wrong with the input file itself:
> system('pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -o whitepaper.pdf "whitepaper.Rmd"')
There have been numerous other conversations regarding this topic: 14586177, 14508429, 15258233, and the heavily-discussed 11025123. They all resolve to solutions that require command-line work, extra middle-steps, external Makefiles, or knit2pdf() (which uses texi2pdf, not desired).
The constraints as I see them:
operate easily within the R environment;
take advantage of Yihui's "<!--pandoc ... -->" in-file configuration (which allows
me to continue to switch arbitrarily between my templates, for one of several examples);
preferably, execute this with a single "standardized" command (i.e., "pandoc('whitepaper.Rmd', 'pdf')").
... so that, once the parameters are set in-file, editing and re-rendering is relatively brain-dead.
I can patch and overwrite Yihui's knitr:::pandoc_one() to remove the offending addition of '-t' and format, but I wonder what side-effects that might have elsewhere. This solution isn't sustainable nor "The Right Way (tm)".
Suggestions for "Right Ways (tm)" to solve this problem? Am I missing an easy/obvious solution?
BTW: thanks, Yihui Xie, for knitr, and John MacFarlane for pandoc. Awesomeness!
(Perhaps I could submit patch suggestions to either or both to work around for my use-case, though if it's just me then it might not be worthwhile.)
I think all there information you need is there in ?pandoc, which includes the example of running system("pandoc -h") to see possible output formats. From that you learn that
Output formats: asciidoc, beamer, context, docbook, docx, dzslides,
epub, epub3,
fb2, html, html5, json, latex, man, markdown, markdown_github,
markdown_mmd, markdown_phpextra, markdown_strict, mediawiki,
native, odt, opendocument, opml, org, pdf*, plain, revealjs,
rst, rtf, s5, slideous, slidy, texinfo, textile
[*for pdf output, use latex or beamer and -o FILENAME.pdf]
So basically format = "pdf" is invalid, you should use pandoc("tmp.Rmd", format = "latex", ext = "pdf") (and acutally the ext="pdf" part is the default, according to ?pandoc, so all you really need is pandoc("tmp.Rmd", "latex")). As for why pandoc('whitepaper.Rmd', 'pdf') resulted in a call with -t pdf, well, you told it to do that in the second argument to your pandoc() call.

Producing subscripts in R markdown

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^

Resources