How to Keep text from running off page when knitting to pdf? - r-markdown

This is my first question on Stack Overflow so I'm going to do my best to follow the guidelines.
My problem is that when I knit to pdf, I often can't keep the code from running off the page.
code running off page in pdf
I know that my first step is to look through previously asked questions, and I have found a few that address my question, but none of the answers in those questions worked for me.
I've tried the following:
adding options(width=X)
adding {r, tidy=TRUE, tidy.opts=list(width.cutoff=X)}
Note that I tried a variety of different numbers for X in both of my attempts.
Thank you in advance for your help.

An answer from Rmarkdown Cookbook is using a latex file. Your Rmarkdown YAML should look like this
---
output:
pdf_document:
pandoc_args: --listings
includes:
in_header: preamble.tex
---
and then your preamble.tex file should contain this
\lstset{
breaklines=true
}
which would help wrap your text. This uses the latex package listings
more documentation

Related

RMarkdown to Word - Page Break

I am using Rmarkdown (notebook) in Rstudio to generate tables and charts, using about 10 code chunks. The R code is running fine. When I knitr to MS-Word (.docx), the results look pretty good, but in some cases I would like to force a page break to improve the appearance, and sometimes to prevent a table from being split across pages (I am using 'flextable' package to create the tables). I tried the ##### page break approach, which involves creating a Word template file and changing the Header 5 properties, but that did not work. I also notice the following post from 7 years ago, which uses a simple Lua filter to allow page breaks in Word. I admit I have not tried that yet. The question is, is there an updated / easier way to control page breaks when outputting to Word in RMarkdown ? It's such a basic part of document control I am surprised how complicated the solutions are (at least based on the older posts).
Pandoc markdown page break
Any suggestions are welcome.

Markdown and math formulas in C++ code with VS Code [duplicate]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I've been using Markdown for class notes, and it's great. I even do some preprocessing on the Markdown so I can do things like tables. But this term I'm teaching a class with a lot of math, and I'd love to be able to put LaTeX formulas with Markdown, something like this:
The refinement relation is written $a \sqsubseteq b$, which can be
pronounced "$a$ approximates $b$" or "$b$ is at least as defined as $a$".
I'd like to be able to take each fragment of LaTeX and preprocess it into a nice antialiased PNG file which I could then include in my Markdown via the HTML <img> tag. But I have absolutely no idea how to take a fragment of LaTeX and get a nice image that
Has the right bounding box
Is antialiased
All I know how to do is get full pages in DVI, PostScript, or PDF formats.
I'm sure this problem has been addressed, but I haven't been able to guess the right search terms. Any suggestions how to solve it or where to look for an existing solution?
EDIT: Having installed mathTeX, I can say that the code is inflexible, that it violates the Linux Filesystem Hierarchy standard, and that it is amateur work—in both the good and bad senses of that word. The code is so complex that there are no obvious faults. I will be looking for alternatives.
Also, it's clear that at bottom, solutions are based on dvipng.
ONE YEAR LATER: I never did get the seamless integration I had been hoping for, but I am limping along on a script of my own devising. It turns out that instead of dvipng it is a little easier to use dvips -E and the convert program of ImageMagick. The benefits are slightly more control of things like scaling, and ease of making a transparent background.
The curious can inspect this example.
I can't recommend this solution to anyone. But I can't recommend MathTeX either.
Have you tried with Pandoc?
EDIT:
Although the documentation has become a bit complex, pandoc has supported inline LaTeX and LaTeX templates for 10 years.
Documents like the following one can be written in Markdown:
---
title: Just say hello!
author: My Friend
header-includes: |
\usepackage{tikz,pgfplots}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[CO,CE]{This is fancy}
\fancyfoot[CO,CE]{So is this}
\fancyfoot[LE,RO]{\thepage}
abstract: This is a pandoc test with Markdown + inline LaTeX
---
Just say hello!
===============
This could be a good example or inlined \LaTeX:
\begin{tikzpicture}
\begin{axis}
\addplot[color=red]{exp(x)};
\end{axis}
\end{tikzpicture}
%Here ends the furst plot
\hskip 5pt
%Here begins the 3d plot
\begin{tikzpicture}
\begin{axis}
\addplot3[
surf,
]
{exp(-x^2-y^2)*x};
\end{axis}
\end{tikzpicture}
And now, just a few words to terminate:
> Goodbye folks!
Which can be converted to LaTeX using commands like this: pandoc -s -i Hello.md -o Hello.tex
Following is an image of the converted Hello.md to Hello.pdf file using MiKTeX as LaTeX processor with the command: pandoc -s -i Hello.md -o Hello.pdf
Finally, there are some open source LaTeX templates like this one: https://github.com/Wandmalfarbe/pandoc-latex-template, that can be used for better formatting.
As always, the reader should dig deeper if he has less trivial use cases than presented here.
Perhaps mathJAX is the ticket. It's built on jsMath, a 2004 vintage JavaScript library.
As of 5-Feb-2015 I'd switch to recommend KaTeX - most performant Javascript LaTeX library from Khan Academy.
Add the following code to the top of your Markdown files to get MathJax rendering support
<style TYPE="text/css">
code.has-jax {font: inherit; font-size: 100%; background: inherit; border: inherit;}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'] // removed 'code' entry
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i = 0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML-full"></script>
and then `$x^2$` or `$$x^2$$` will render as expected :-)
You can always install a local version of MathJax if you don't want to use the online distribution, but you might need to host it through a local webserver.
UPDATE: these days I just use pandoc instead of canonical markdown, but the above is still useful.
I'll answer your question with a counter-question...
What do you think of Org-mode? It's not as pure as Markdown, but it is Markdown-like, and I find it as easy to work with, and it allows embedding of Latex. Cf. http://www.gnu.org/software/emacs/manual/html_node/org/Embedded-LaTeX.html
Postscript
In case you haven't looked at org-mode, it has one great strength as a general purpose "natural markup language" over Markdown, namely its treatment of tables. The source:
| 1 | 0 | 0 |
| -1 | 1 | 0 |
| -1 | -1 | 1 |
represents just what you think it will...
And the Latex is rendered in pieces using tex-mode's preview-latex.
you should look at multimarkdown http://fletcherpenney.net/multimarkdown/
it has support for metadata (headers, keywords, date, author, etc), tables, asciimath, mathml, hell i'm sure you could stick latex math code right in there. it's basically an extension to markdown to add all these other very useful features. It uses XSLT, so you can easily whip up your own LaTeX styles, and have it directly convert. I use it all the time, and I like it a lot.
I wish the markdown would just incorporate multimarkdown. it would be rather nice.
Edit: Multimarkdown will produce html, latex, and a few other formats. html can come with a style sheet of your choice. it will convert into MathML as well, which displays in Firefox and Safari/Chrome, if I remember correctly.
RStudio has a good free IDE that allows for Markdown and LaTeX.
kramdown does exactly what you describe:
https://kramdown.gettalong.org/syntax.html#math-blocks
And it's way more reliable and well-defined than Markdown.
Hey, this might not be the most ideal solution, but it works for me. I ended up creating a Python-Markdown LaTeX extension.
https://github.com/justinvh/Markdown-LaTeX
It adds support for inline math and text expressions using a $math$ and %text% syntax. The extension is a preprocessor that will use latex/dvipng to generate pngs for the respective equations/text and then base64 encode the data to inline the images directly, rather than have external images.
The data is then put in a simple-delimited cache file that encodes the expression to the base64 representation. This limits the number of times latex actually has to be run.
Here is an example:
%Hello, world!% This is regular text, but this: $y = mx + b$ is not.
The output:
$ markdown -x latex test.markdown
<p><img class='latex-inline math-false' alt='Hello, world!' id='Helloworld' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAAAQBAMAAABpWwV8AAAAMFBMVEX///8iIiK6urpUVFTu7u6YmJgQEBDc3NxERESqqqqIiIgyMjJ2dnZmZmbMzMwAAAAbX03YAAAAAXRSTlMAQObYZgAAAVpJREFUKM9jYICDOgb2BwzYAVji8AQg8fb/PZ79u4AMvv0Mrz/gUA6W8F7AmcLAsJuBYT7Y1PcMfLiUgyWYF/B8Z2DYAVReABKrZ2DHpZwdopzrA0nKOeHKj66CKOcKPQJWwJo2NVFhfwCQyymhYwCUYD0avIApgYFh2927/QUcE3gDwMpvMhRCDJzNMIPhKZg7UW8DUOIMg9sCPgGo6e8ZODeAlAP9xLEArNy/IIwhAMx9D3IM+3cgi70BqnxZaNQFkHJWAQbeBrByjgURExaAuc9AyjnB5hjAlEO9ygVXzrplpskEMPchQvkBmGMcGApgjjkAVs7yhyWVAcwFK2f/AlJeAI0m5gMsEK+aMhQ6aDuA1DcDIZirBg7IOwxlB5g2QBJBF8OZVUz95hqfC3hOXWGYrwBSHskwk4EByGXab8QAlOBaGizFKYAtUlgUGEgBTCSpZnDCLQUA+y6MXeYnPDgAAAAASUVORK5CYII='> This is regular text, but this: <img class='latex-inline math-true' alt='y = mx + b' id='ymxb' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAAOBAMAAABOTlYkAAAAMFBMVEX///9ERETu7u4yMjK6urp2dnZUVFSIiIjMzMwQEBDc3NwiIiJmZmaYmJiqqqoAAADS00rKAAAAAXRSTlMAQObYZgAAAOtJREFUKM9jYCAACsCk4wYGgiABTLInEKuS+QGxKvkVGBj47jBwI8tcffI84e45BoZ7GVcLECo9751iWLeSoRPITBQEggMMDBy9sxj2MDgz8DIE8yCpPMxwjWFBGUMMkpFcbAEMvxjKGLgYxIE8NkHBiYIyQMY+hmoGhi0Mdsi2czawbGCQBTJ+ILvzE0MaA9MHIIWwnWE9A+sBpk8LGDgmMCnAVXJNYPgCJHhRQvUiA/cDXoECZx4DXoSZTBtYgaaEPw5AVnkOGBRc5xTcbsReQrL9+nWwyxbgC88DcJZ+QygDcYD1+QPiFAIAtLA8KPZOGFEAAAAASUVORK5CYII='> is not.</p>
As you can see it is a verbose output, but that really isn't an issue since you're already using Markdown :)
yes, but you'll have to hack it a little yourself. I've written a filter that replaces latex tags $\some\inline\latex$ or $$\some\equation$$ with appropriate image tags to a mimetex.cgi script. It took all of 5 minutes.
Warning: spectacularly ugly...
#!/usr/bin/env python
import sys, markdown,re
MIMETEX_LOC="http://some.server.com/cgi-bin/mimetex.cgi"
def sanitizeLatex(text):
return re.sub(r"\\",r"%5C", text)
def wrapLatexBlock(text):
return '<img alt="equation" class="block" src="%s?%s"></img>'%(MIMETEX_LOC,text)
def wrapLatexInline(text):
return '<img alt="equation" class="inline" src="%s?%s"></img>'%(MIMETEX_LOC,text)
def prepLatexBlock(matchobj):
return wrapLatexBlock(sanitizeLatex(matchobj.group()[2:-2]))
def prepLatexInline(matchobj):
return wrapLatexInline(sanitizeLatex(matchobj.group()[1:-1]))
if __name__ == "__main__":
# initialise markdown
md=markdown.Markdown()
raw_md=open(sys.argv[1],"r").read()
##
# deal with embedded latex
##
raw_md=re.sub(r'\$\$(.*?)\$\$',prepLatexBlock, raw_md)
raw_md=re.sub(r'\$(.*?)\$',prepLatexInline, raw_md)
##
# once latex is parsed, convert md to html
##
main_html=md.convert(raw_md)
# hey presto!
print(main_html)
Of course, you have to define the appropriate css yourself for .block and .inline images...
I came across this discussion only now, so I hope my comment is still useful. I am involved with MathJax and, from how I understand your situation, I think that it would be a good way to solve the problem: you leave your LaTeX code as is, and let MathJax render the mathematics upon viewing.
Is there any reason why you would prefer images?
What language are you using?
If you can use ruby, then maruku can be configured to process maths using various latex->MathML converters. Instiki uses this. It's also possible to extend PHPMarkdown to use itex2MML as well to convert maths. Basically, you insert extra steps in the Markdown engine at the appropriate points.
So with ruby and PHP, this is done. I guess these solutions could also be adapted to other languages - I've gotten the itex2MML extension to produce perl bindings as well.
I was looking for exactly the same thing when I found teqhtml. It does the conversion of $ and $$ equations to images with the nice bonus of aligning the resulting image vertically with the surrounding text. Not a lot of doc but it's quite straightforward.
Hope it helps some future readers.
Sorry to rouse a really old thread, but I've been using jemdoc for a couple of years and it is really excellent.
It is possible to parse Markdown in Lua using the Lunamark code (see its Github repo), meaning that Markdown may be parsed directly by macros in Luatex and supports conversion to many of the formats supported by Pandoc (i.e., the library is well-suited to use in lualatex, context, Metafun, Plain Luatex, and texlua scripts).
The project was started by John MacFarlane, author of Pandoc, and the tool's development tracks that of Pandoc quite closely and is of similar (i.e., excellent) quality.
Khaled Hosny wrote a Context module, providing convenient macro support. Michal's answer to the Is there any package with Markdown support? question gives code providing similar support for Latex.

R markdown show markdown output in .Rmd

I remember there was a time that when you type # in R markdown it will immediately transfer to the heading output. Also, when you type **bold** it will immediately show the text in bold. But now I can't find a way to do this.
Is it possible to show the markdown output in the .rmd document? Thanks!
This behaviour will depend on the text editor you use. Some Markdown editors have a "What You See Is What You Get" (or WYSIWYG) philosophy that will do exactly what you describe. You can find examples of WYSIWYG Markdown editors online, I personally have only tried Typora. But as far as I can tell, none of them seem to support Rmarkdown (perhaps someone has made an Rstudio add-in?).
On the other hand, in the early days of knitr, it used to support Lyx (I don't know if it still does), which is maybe closer to what you have in mind? See this post for an example: https://yihui.org/knitr/demo/lyx/

debugging options with rmarkdown

I am using the rmarkdown with the rshiny for generating word file reports. I am using the R studio-server for development. On executing the rshiny application, it halts due to some error in the one of the rmarkdown.
The error says...
Quitting from lines 11-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
Quitting from lines NA-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
It's providing the line numbers which are not useful to identify the root cause. Adding print statements are also not useful as I am generating the word file report, until and unless the complete .Rmd doesn't get successfully executed, I won't be able to see print statements output.
I tried changing the rmarkdown output setting from chunk output inline to chunk output to console as mentioned here as well but it is of no use.
Is there any way to print the .Rmd file print statements or the output to the console or is there any way to debug the .Rmd file?
In addition to my comment above, Abhinandan, I've recently stumbled across a new package, called testrmd.
Although it is new, it seems to work with a number of different test packages and provides a useful front-end for Rmarkdown documents. (I'm certainly going to use it.)
You might want to check it out. Here's the link: https://github.com/ropenscilabs/testrmd.
I hope this helps you.
See My .Rmd file becomes very lengthy. Is that possible split it and source() it's smaller portions from main .Rmd?
That's what I do. -
Split your code chunks in separate files and add them one by one

To interrupt org-latex-pdf-process to regexp-replace some string of the .tex intermediate file and continue to export

I am using org-mode to write some article now. Org-mode is really a great tool to outline a article with great table and image support.
Org-mode can display inline .png image but .pdf file. Because now org-mode can not control the width or height of shown inline image, so i use matplotlib to produce low dpi .png image in PNG folder for inline display and higher dpi pdf image in PDF folder for finally article export.
In .org file, the image link is like [[file:PNG\*.png]] and \includegraphics{PNG\*.png}in the produced .tex file. Then emacs will use org-latex-pdf-process to render it to pdf file. What I want is that before or in org-latex-pdf-process, a regexp replace function is added to replace the \includegraphics{PDF\*.pdf}, and then produce the final pdf file.
I am a beginner to elisp, so do not know how to put some code here. Can anyone help me?
With help of some great guys in org-mode mailing list, I finally got the perfect solution.
The detailed can be found in http://lists.gnu.org/archive/html/emacs-orgmode/2014-04/msg01014.html.
The solution is offered by John Kitchin in github: https://github.com/jkitchin/jmax/blob/master/ox-manuscript.el. The functions he wrote really satisfies what I need, which gives me a great help.
Many thanks to him.
Any questions can be posted to ox-manuscript.el.
Cheers!