I'm writing some code with descriptions using Rmarkdown 2 and knit PDF.
I've been trying many method to write a degree symbol inline:
Latex package: siunitx's \ang
Latex package: textcomp's \textdegree
Latex: \circ
And many possible RMarkdown symbols, such as:
$$ \textdegree $$ or $\textdegree$
But nothing is working. Is there a way to write a degree symbol in RMarkdown 2 and convert it do PDF?
EDIT (18 AUGUST 2014):
Ok, I found out where is the problem. If you use \circ in normal sentence or first-level list it is ok. But when I try to use \circ in second-level list - it's not working.
The problem was with RMarkdown converting nested lists. On this page http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html you can find sentence:
The nested list must be indented four spaces or one tab
Although, using the tab could be a problem. When using four spaces - it works:
* Let's turn this round 360$^\circ$
+ Let's turn this round 360$^\circ$
Using \circ works for me, RStudio, knit to pdf:
Let's turn this round 360$^\circ$
You can use plotmath's degree, e.g.
plot(1, xlab=expression(4*degree))
Related
I have in R a mathematical operation like this:
x = 5
y = (x / 5) * 2
and I want to print y to console or pdf file so it looks like:
(x / 5) * 2 like on the paper
Can I do this using R basic functions or some kind of library?
#Yehor: you can use the power of RStudio and RMarkdown to combine text, code, and visualisations in output formats like pdf. Under the hood you need a bit more than "some kind of library". But if you use RStudio as your R editor, the infrastructure (what you need) is actually in place already. RStudio will ask to have a few packages installed when you open a RMarkdown file for the first time, but do not worry about this.
In RStudio open a new RMarkdown file. During the opening dialogue you can (pre-)select an output type, e.g. pdf. Please note that you can change this later.
The example that opens gives you an idea of what you can do. The magic happens when you hit the "knit-button" in the top bar of the editor pane. R/RStudio will render the document and interpret code-chunks. These chunks can include "just" code, code to produce tables and/or graphics.
For math & formulae, RMarkdown supports 2 ways of presenting LATEX:
inline equation
equation mode
(I) inline equation - within single dollar signs $
You can include an inline equation anywhere in the text part of the Rmd document. For example:
This is how I add a formula: $y := \frac{x}{5} * 2$ within a line using inline code.
(II) equation mode - statement within double dollar signs $$
For the equation mode use $$ and have this on a separate line in Rmd.
$$y := \frac{x}{5} * 2$$
Knitting the Rmd in RStudio renders the document into a pdf (you can also export to html, MS Word or even Powerpoint).
For example:
is produced with this minimal Rmd:
If you want to combine this with the calculation, you would add a "code-chunk".
In RMarkdown you can include R-code inside 3 backticks, e.g. {r} # R-stuff ...
Thus, the following code chunks performs the operation:
x <- 5
y <- (x/5) * 2
If you want to print the result "inline" in your text, you can add so-called inline code. This is done by having R-statement inside single backticks and a starting r, i.e. r ... within the text part of the Rmd. For example:
My result is `r y` as inline code.
This will print: My result is 2 as inline code.
You could include more sophisticated R-statements as inline code by separating each statement with a semi-colon (~end of command line). However, I recommend to do the fluffy stuff in a code chunk and use the inline for simple statements. It is much easier on the eye and for debugging.
I cannot find any information about spacing between letters in R Markdown. All I found only were questions about vertical spacing. I basically have two issues:
In LaTeX I usually use the siunitx package to correctly typeset numbers and units. I can of course use this in R Markdown as well if I load the package with \usepackage{siunitx}. But this does not output to the docx format, only to pdf. That brings me to my other question...
So I tried using different spaces of LaTeX to display numbers and their units with at least less space, e.g. writing 40\,m^2 to display 40 m² (in LaTeX I would use \SI{40}{\square\meter}). However, apparently R Markdown does not handle the \, nor \; at all, not even in the pdf output.
Question: What is the correct way to add smaller spaces between letters in R Markdown? (irrespective of output format!) How do I replace the \, command?
And: Is there a way to handle units nicely using R Markdown? I have found this question on the R units package, and I could live with it. That is, if I want to write hardcoded numbers like 40 m² I would have to use something like`r format(set_units(40, m2))`,right?.
Have you tried adding thin spaces using Unicode chars? For example, this page http://jkorpela.fi/chars/spaces.html suggests that "\u2009" and "\u200A" should display as thin spaces.
When I try this with a PDF document (using latex_engine: xelatex to handle Unicode), this is what I see:
It also appears to work with HTML and Word output.
Edited to add:
To be clear, this needs to go through R. If you want it inline, use code like this:
This is standard spacing: 40 m²,
this is narrow `r knitr::asis_output("40\u2009m²")` spacing.
This produces this output in PDF:
I am putting an assignment together in R Markdown to PDF (through Latex) and been using xtable to nicely format my output. The below code generates a small table that I would like to align right in the document and have text wrap around it. Is there a way to achieve this?
summary.table <- xtable(ddply(aircon, ~when, summarise, Mean=mean(hours), StdDev=sd(hours)), caption="Mean and Standard Deviation (in Hours) Before and After Change")
print(summary.table, include.rownames=FALSE)
Given the update regarding using LaTeX to do the wrapping, how could I write this in R Markdown to take advantage of this? I have tried to print inline using r <code> but that didn't work.
I guess you could add the begin/end wraptable commands in the r-chunk that creates the table, like this:
cat("\\begin{wraptable}{r}{5.5cm} \n")
...
print(summary.table,...)
cat("\\end{wraptable} \n")
You can use the Includes mechanism documented here, to include a custom header.tex which would contain usepackages, etc.
I get a weird result when trying to output my regression results into latex tables using the stargazer package in R: when I simply do
stargazer(linear.1,linear.2)
I get a perfectly fine latex table displayed in the R console that I can then copy and paste into my latex editor. However, when I use
table<-stargazer(linear.1,linear.2)
the character object 'table' does not have the same latex output that one would expect given the results of the first line of code, but rather the same latex code as before with an additional backslash before every backslash in the original latex code, e.g. instead of \begin{document} I get \\begin{document}, instead of two backslashes for a line break I get three backslashes etc.
Any idea what is causing this behavior and how I could fix it?
Thanks!
Perfectly normal behavior, as backslashes in R string require an escape character that just happens to be... a baskslash!
I am trying to group row names in a R data frame for typesetting with the Hmisc latex() function. Problem is that latex() adds two tilde characters before each row name, and these show up in my document.
How can I either remove these characters or have them not show up?
Example:
test.df <- data.frame(row.names=letters[1:4], col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="", n.rgroup=c(2,2), rgroup=c("First","Second"))
Edit:
The latex function occurs inside a knitr chunk. The resulting .Rnw file is compiled through the knit2pdf function, which uses pdfLatex by default, I think. All other tables/figures in the document compile fine, without any residual LaTex syntax showing up.
They will not show up if you use latex with a TeX processor:
test.df <- data.frame(row.names=letters[1:4], col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="test", n.rgroup=c(2,2), rgroup=c("First","Second"))
If you want to "capture" the text that is "printed" to the screen and remove the double tildes with sub then you probably need to use capture.output, because it appears that latex is not returning a value but is acting more like the cat function which has output to the screen as a side-effect:
out <- sub("^~~", "", capture.output(
latex(test.df, file="",
n.rgroup=c(2,2), rgroup=c("First","Second"))))
You could then use writeLines or cat with a file argument to send that text to a destination. I suppose it is possible that you could just put the sub call inline without diverting the results to a named object. That will depend on exactly how your are processing this text.
If you don't want to use LaTeX then i suggest either the ascii package that has pretty advanced options that do a nice raw text output (it also has the rgroup & n.rgroup options for grouping row names). If you are interested in getting the tables into a Word document (or just HTML) i suggest Markdown with my htmlTable function - the arguments are based upon the Hmisc latex arguments as I needed a replacement when I was switching to Markdown, thus all you need to do is change the function name to htmlTable after loading my package.