Render Custom LaTeX Commands within RStudio - r

Is it possible to have RStudio render custom LaTeX commands in an rmarkdown document within the editor?
That is, I'd like to be able to see my custom commands rendered on-the-fly, without having to knit the document first, in the same way that standard LaTeX is rendered automatically in the editor view.

Yes, though in my experience support is somewhat spotty.
RStudio uses MathJax for LaTeX display. It's not all of LaTeX, it's a subset designed for displaying math mode expressions in a web page. You can read the details here: https://docs.mathjax.org/en/latest/tex.html.
From the "Defining TeX Macros" section of that web page:
You can use the \def, \newcommand, \renewcommand, \newenvironment, \renewenvironment, and \let commands to create your own macros and environments. Unlike actual TeX, however, in order for MathJax to process such definitions, they must be enclosed in math delimiters (since MathJax only processes macros in math-mode).
So if you have something like
$$
\newcommand{\myexp}{\exp}
$$
in your html_document, you can use \myexp later. But this doesn't work in a pdf_document. As I said, spotty.
Generally speaking I wouldn't use Markdown in RStudio for a serious LaTeX document that needs macro definitions. Use knitr's Sweave-like format in TeXworks or TeXShop, and install a processing engine so knitting is supported.

Related

Mathjax \text{} with greek letters inside

I am trying to use mathjax in Jupyter Notebook, and trying to render the following
in Latex typesettings I can render only the followin:
\varepsilon-\text{greedy}(Q)$\
As you can see the dash is very long and adds extra space around dash.
If i move the epsolon within the \text expression the result will be:
\text{\varepsilon-greedy}(Q)$\
\
The dash has no problem bu the epsilon is not rendered.
How can I render the epsilon as intend.
Resolved: UPDATE
Resolved: UPDATE2
That is not the answer for rendering the same outout is just move the dash into the \text
\varepsilon\text{-greedy}(Q)$\
Note that \text{\varepsilon-greedy}(Q) is not valid LaTeX. The \varepsilon macro is a math-mode macro, and is not allowed in text mode, as it appears here, so it would cause an error message in actual LaTeX.
MathJax is designed to process primarily math-mode material, so it does not process macros in text mode, so \varepsilon will be displayed verbatim. If you want to use it in text mode, you could use \text{$\varepsilon$-greedy}(Q) to temporarily enter math mode within text mode.
MathJax version 3 has a textmacros extension that allows processing of some macros in text mode, but that is not available in version 2, which is what is used on StackExchange. (They are using a four-year-old version of MathJax here.)

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.)

IDE with LaTeX and R support: Inline output in .Rmd notebooks and weaving LaTeX document with R code

I'm trying to improve my workflow when working with R and generating documentation. I've been going between TeXStudio, JupyterLab and RStudio for a while, and I'm trying to improve my workflow. TeXStudio has limited R support, and RStudio limited support for LaTeX.
VS Code has support for multiple languages, including R and LaTeX. The fact that it can run both Jupyter notebooks, R notebooks, and LaTeX, and has plugins for other languages as well, makes it seem desirable. However, I am unable to find documentation on how to configure it to work with R and LaTeX code in the same file. In addition, I am unable to configure R notebooks to allow inline code execution output.
However, I am unable to (a) set up code execution output under the code for .Rmd notebooks, and (b) I can't figure out how to weave .Rnw (R/LaTeX) documents with Sweave/knitr.
I'm trying to find an IDE that would include features like:
Markdown, code and code execution output in the same document
Auto R and LaTeX code completion
Automatic display of R function documentation
Spell check
Simple R console access
Compile .Rnw
Syntax highlighting for both R code and LaTeX code
I am, primarily, requesting ways to configure VS Code, or, secondly, way to configure another IDE that can meet my requirements. A tutorial on this would be much appreciated.
After a bit of digging around, I found that VS Code does nearly all the things I need.
Auto R and LaTeX code completion, Display of R function documentation in a tab in VS Code, Simple R console access, and Syntax highlighting for both R code and LaTeX code:
The R and LaTeX Workshop extensions, will provide highlighting and autocompletion of code in both languages. By installing R, you can easily open a session in a terminal window in VS Code, and from there open documentation inside VS Code.
Spell check
Code Spell Checker offers spell check for multiple languages. Install the extension and any desired dictionaries, and set the langauges you want to be included in the extension settings.
Compile .Rnw files
Turns out LaTeX Workshop can actually do this by default.
Markdown, code and code execution output in the same document
This is the only thing VS Code doesn't do as far as I can tell. It can compile .Rmd files, however, but the output can only be seen in the compiled PDF. I consider this less important, since I can use Jupyter notebooks instead.

pandoc latex template

I have found this latex template,
http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/ThesisStyle/
which is perfect for my thesis. I am currently writing it directly in Latex with TexStudio, and playing around with the template directly is easy.
I am however switching to RStudio and wiriting it as a RNotebook, as I need to be able to easily produce docx versions of it (or other formats).
I already produced a Rmd version which works perfectly as a html/docx, but the pdf it produces (and i tried bookdown, thesisdown, oxfordown etc..) is never as good looking as the template above.
Is there a way to "extract" a template from the latex template in the link, which can be used for the pandoc conversion in RStudio? How?
(i am quite a newbie so please try with an easy accessible answer!)

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.

Resources