Formatted scientific names from R to LaTeX (using Sweave or knitr)


Formatted scientific names from R to LaTeX (using Sweave or knitr)



I am implementing a function print_name in the package taxlist.
For example, I can format a name for markdown:


print_name


library(taxlist)
data(Easplist)
print_name(Easplist, 206, style="markdown")



The output in the console is then:


[1] "*Cyperus papyrus* L."



I can use this command for example to mention a species in the content of a markdown document by `r I(print_name(Easplist, 206, style="markdown"))`, where the scientific name appears as italics and the author name does not.


`r I(print_name(Easplist, 206, style="markdown"))`



I would like to implement an option (i.e. style="latex") retrieving the string formatted for LaTeX, which can be inserted through something like Sexpr{print_name(Easplist, 206, style="latex")} (this example is only an hypothetical one).
The output should be then textit{Cyperus papyrus} L. but all my attempts failed because backslash is a scape in R strings.


style="latex"


Sexpr{print_name(Easplist, 206, style="latex")}


textit{Cyperus papyrus} L.



Is there a way to achieve properly this task?



Note: The function is at the moment not implemented in the CRAN version of the package, thus to reproduce the example use the last version from GitHub:


devtools::install_github("kamapu/taxlist")




1 Answer
1



What I imagine that you are doing is this:


<<example, echo=FALSE>>=
library(taxlist)
data(Easplist)
Sexpr{print_name(Easplist, 206, style="markdown")}
@



Simply move the Sexpr{}to outsite the chunk.


Sexpr{}


<<example, echo=FALSE>>=
library(taxlist)
data(Easplist)
@

Sexpr{print_name(Easplist, 206, style="markdown")}



That will output you what you want.



EDIT:



If you are trying to incorporate "latex" as a style option for the output, then it should look like this when it outputs outside of latex:


library(taxlist)
data(Easplist)
print_name(Easplist, 206, style="latex")
[1] "textit{Cyperus papyrus} L."



The "" will escape the escape. I did not incorporate it into your function, but here is an example:


<<>>=
example_text <- "Cyperus papyrus L."
example_text <- strsplit(example_text, split = " ")
test1 <- paste0("textit{", example_text[[1]][1], " ", example_text[[1]][2], "}",
" ", example_text[[1]][3])
@

Sexpr{test1} is a paper reed.



The output looks like this in the rendered pdf.



enter image description here





I apologize for the confusion, please see my edited answer above.
– AndS.
2 days ago





Wow! This is really working! Nevertheless it is working only with knitr but not with Sweave.
– Miguel Alvarez
2 days ago



knitr


Sweave






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift