Knitr - stop superscript in R Markdown - r

I need to stop superscript before comma - , or full stop - .. When converting the Rmd file to HTML knitr makes comma superscripted as well. Example:
example.Rmd
MyTitle
========================================================
J.Smith^1, K.John^2, H.Gone^*.
example.html
Let me know if this was asked before, I can't find relevant questions.

You may insert HTML tags directly to Markdown documents. To toggle the "superscripting" mode, use sup.
MyTitle
========================================================
J.Smith<sup>1</sup>, K.John<sup>2</sup>, H.Gone<sup>*</sup>.
EDIT: You may also use round brackets to indicate exactly the part of text to be reformatted.
J.Smith^(1), K.John^(2), H.Gone^(*)
EDIT: This has changed for Markdown V2
2^10
2^(10)
2^10^
2^(10)^

Related

How to reference two times to a single footnote in rmarkdown?

I try to reference to a single footnote in a few places in the text. However, with the code below, I've got two footnotes with the same content.
---
title: "My document"
output: html_document
---
One part of the text [^1].
Two pages later [^1].
[^1]: My footnote
Is it possible to reference more than once to a specific footnote using rmarkdown?
I had the same problem. I used html tags which worked really well. Whatever you want as superscript just put between the <\sup> like below:
<sup> text </sup>
jan 6 2023 update:
now I use quarto which is eally easy as you can just use do[^1]
[^1]footnote text
My workaround here is to just do it manually using inline latex mathmode (e.g, \(^2\) ).. Annoying, but even if they had a solution, you'd have to remember the citation number anyways...
I would suggest you to go with latex solution if you do not have many footnotes per page. By latex solution I mean:
(in Markdown, use Latex to superscript the footnote number)
First part$^1$
(and the next one)
Second part$^2$
(at the end of your text add *** to create a line across the document)
(under the line, add the text below:)
1, 2: Text for your footnote
On the other hand, there is a thread created on this specific R-Markdown bug. Maybe take a look at it, in this link.
Hope I helped somehow.

R markdown: can I insert a pdf to the r markdown file as an image?

I am trying to insert a pdf image into an r markdown file. I know it is possible to insert jpg or png images. I was just wondering if it is also possible to insert a pdf image. Thanks very much!
If you are just trying to insert an image that has been exported from, for example, some R analysis into a pdf image, you can also use the standard image options from the knitr engine.
With something like:
```{r, out.width="0.3\\linewidth", include=TRUE, fig.align="center", fig.cap=c("your caption"), echo=FALSE}
knitr::include_graphics("./images/imagename.pdf")
```
Unfortunately you can't specify the initial dimensions of your image output (fig.width and fig.height), which you would need to pre-define in your initial output, but you can specify the ultimate size of the image in your document (out.width). As noted below, however, this is limited to scaling down.
You could also of course leave out the initial directory specification if your files are in the same working directory. Just be aware of operating system differences in specifying the path to the image.
An alternative method is to use Markdown syntax noted by #hermestrismegistus on this post:
![Image Title](./path/to/image.pdf){width=65%}
This can also be collected for multiple images side-by side:
![Image Title](./path/to/image.pdf){width=33%}![Image2 Title](./path/to/image2.pdf){width=33%}![Image3 Title](./path/to/image3.pdf){width=33%}
Edit:
After working more extensively with in-text referencing, I have found that using r chunks and the include_graphics option to be most useful. Also because of the flexibility in terms of image alignment (justification).
As an example:
```{r image-ref-for-in-text, echo = FALSE, message=FALSE, fig.align='center', fig.cap='Some cool caption', out.width='0.75\\linewidth', fig.pos='H'}
knitr::include_graphics("./folder/folder/plot_file_name.pdf")
```
The reference can later be used in-text, for example, Figure \#ref(fig:image-ref-for-in-text) illustrates blah blah.
Some important things to note using this format:
You can only expand PDF images via a code chunk up to the out.width and out.height conditions set in the original .pdf file. So I would recommend setting them slightly on the larger side in your original image (just note that any chart text will scale accordingly).
The in-text reference code (in this case image-ref-for-in-text) CANNOT contain any underscores (_) but can contain dashes (-). You will know if you get this wrong by an error message stating ! Package caption Error: \caption outside float.
To stop your plots drifting to the wrong sections of your document, but in a way that unfortunately will generate some white space, the above example includes fig.pos='H'. Where H refers to "hold" position. The same can be achieved for the former Markdown option by placing a full-stop (period .) immediately after the last curly bracket.
Example:
![Image Title](./path/to/image.pdf){width=75%}.
Unfortunately, this latter option results in some unsightly full-stops. Another reason I prefer the include_graphics option.
Sorry, I found that there is a similar post before:
Add pdf file in Rmarkdown file
Basically, I can use something like below works well for the html output:
<img src="myFirstAlignment2.pdf" alt="some text" width="4200" height="4200">
And something like below works well for the pdf output:
(1)possible solution
\begin{center} <br>
\includegraphics[width=8in]{myFirstAlignment2.pdf} <br>
\end{center}
(2)possible solution
![Alt](myFirstAlignment2.pdf)
The myFirstAlignment2.pdf should be replaced with path\myFirstAlignment2.pdf if the pdf file is not in your working directory.
In relation to the comment of the best answer, there is a way to use the second option, and the output not come out tiny.
Use the following syntax below with the height being a large number. Having text in the brackets is necessary for it to work.
![Alt](./file.pdf){width=100% height=400}
None of the answers outlined worked well for me in terms of sizing the pdf, so adding another answer using the code chunk options for out.height and out.width to control the size:
```{r out.height = "460px", out.width='800px', echo=F}
knitr::include_graphics("./images/imagename.pdf")
```

Enabling R Markdown syntax highlighting in TextWrangler/BBEdit

I would like to enable syntax highlighting for R Markdown (.Rmd) highlighting in TextWrangler. I managed to get syntax highlighting for .R files using this R.plist file (direct link to file). R Markdown has similar highlighting, so I have that file as a starting point, as I cannot find a similar .plist file for .Rmd files.
The issue I am having is that in RMarkdown, the code is between a set of ``` (three backticks) and the text (which is what I'd like to demarcate as comments) are the remainder of the file.
I would like to tell TextWrangler to only highlight what it sees between a set of ``` - currently, it highlights the entire file (including my text sections).
I thought editing the values for keys
<key>Open Block Comments</key>
<string></string>
<key>Close Block Comments</key>
<string>```</string>
would be a workaround, but it doesn't seem to help.
Edit, here is the Rmd syntax:
here is some text that is whatever I want.
```
in here between the backticks is some code
```
here is some more exposition
The code I'd like to highlight with R syntax is between backticks, whereas the text I'd like to display as a comment/without highlighting (whichever is easiest) is not demarcated.

How do I add a note or text in addition to the caption to a knitr figure for latex output?

I am using knitr to write my thesis and compiling it as a latex document. I have successfully included figures with the appropriately labeled caption; however, in addition to the caption I would like to include additional lines of text directly below the caption and associated with the figure. For example, below the caption I would like to hove: "NOTE: These data are distributed with mu=0 and sd=1" and "SOURCE: These data were randomly generated in R."
EDIT 1 to question: I also need to have a list of figures page with just the caption information included not any of the NOTE or SOURCE information. In addition the caption under the figure must be centered, while the NOTE and SOURCE are left hand justified.
I believe that many publications have figures with notes about the figure or information about the source of the data that is separate from the caption title; so, I believe this question is not just applicable to me.
EDIT 1 to code. Here is a synthesis of the code that #user29020 provided and the link they suggested, as well as the .pdf work around I have assembled from other posts. The advantage of the "pure" knitr method provided by #user29020 is that the plot is beautiful with the text scaled consistently with the body of the text. The disadvantage is a lack of control of the caption information: I would like to center the "Caption" element and left justify the and sub-caption lines and I do not know how to accomplish this; so, if anybody knows how to do this that would be great. The advantage of the pdf work around is that I have greater control over the captions. In addition to not taking full advantage of knitr's ability to create and track figures, the disadvantage is that while the font matches, by using "Ghostscript" the scaling appears a little off and needs to be adjusted in an ad-hoc fashion in the R code.
\documentclass{article}
\usepackage{caption}
\usepackage{tikz}%Draw Graphs
\begin{document}
\listoffigures
\newpage
%THIS IS A SYNTHESIS OF user29020 CODE AND THE LINK THEY PROVIDED IN THEIR COMMENTS
<<cap-setup1,include=FALSE>>=
scap="This is a Caption."
NOTE="NOTE: Here is a note with one linebreak from above."
SOURCE="SOURCE: Here is my source with an extra linebreak to separate it."
EXTRA="{\\tiny EXTRA: Here is smaller text. You could explore more text sizing in \\LaTeX.} And bigger again."
hspace="\\\\\\hspace{\\textwidth}"
cap<- paste(
scap,
hspace,
NOTE,
hspace,
SOURCE,
hspace,
EXTRA)
#
In Figure \ref {fig:fig1}, we see a scatter plot.
<<fig1, echo=FALSE,fig.cap=cap, fig.scap=scap,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
#
%THIS IS THE PDF WORK AROUND
%Loading R Packages
<<loading,include=FALSE>>=
require(extrafont)
require(graphicx)
font_install("fontcm") #this gets the cm roman family from package extrafont for pdf output in r to match latex
#
<<fig-pdf,echo=FALSE, include=FALSE,fig.cap=paste("This is a Caption"),fig.pos="!h", >>=
Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.15/bin/gswin64c.exe") #this needs to go inside every r code chunk to call GhostScript for embedding latex font "computer modern roman" (so R font in pdfs matches latex document font)
pdf("tikz-plot-pdf.pdf", family="CM Roman")
set.seed(2015)
r<-rnorm(100)
plot(r)
dev.off()
embed_fonts("tikz-plot-pdf.pdf") #this embeds the pdf font so that when graphicx calls the pdf the fonts from R to latex match
#
\newpage
In Figure \ref{fig:scatter}, we see a scatter plot.
\begin{figure}[!h]
\centering
\includegraphics[width=\textwidth]{tikz-plot-pdf}
\caption{This is also a Caption}\label{fig:scatter}
\begin{minipage}{\textwidth}
NOTE: Here is a note with one linebreak from above.\\
SOURCE: Here is my source with an extra linebreak to separate it.\\
EXTRA:{\tiny EXTRA: Here is smaller text. You could explore more text sizing in \LaTeX.} And bigger again.
\end{minipage}
\end{figure}
\end{document}
I would like to have the "NOTE" and "SOURCE" lines below the caption line, EDIT 1: only the caption included in the \listoffigures and the caption centered, while the NOTE and SOURCE are left justified (like the output from the pdf workaround):
I have not asked a question on this forum before, although I have found many answers, so any meta-feedback on my question EDIT 1: or how I am editing is also appreciated.
Thank You
This answer assumes that multiline captions are sufficient for your purposes. I used your method of passing an R variable to the Knitr fig.cap= argument. This lets us compose the caption ahead of time in other code chunks before use.
The tricks were that A) we need to find out how to add line break to caption without using caption package, and B) know how to pass literal "\" (backslash) characters to the resulting Latex document.
The reference about adding captions suggests adding \\\hspace{\textwidth} to start a new line within the caption (in the Latex file). So, we escape it in the Knitr file like this \\\\\\hspace{\\textwidth}.
Indeed, almost any Latex code you want to end up in the Latex file will have to be escaped this way. When writing your thesis, you'll want to refer to your Figures, no doubt. You can use the Latex references by putting the figure \label into the caption and then using a \ref{} elsewhere.
See the modified version of your file below for multiline captions and figure references:
\documentclass[12pt]{article}%12 Pt font, article document class
\usepackage{caption}
\usepackage{tikz}%Draw Graphs
\begin{document}
<<>>=
x <- paste(
"This is a Caption.",
"\\\\\\hspace{\\textwidth}",
"NOTE: Here is a note with one linebreak from above.",
"\\\\\\hspace{\\textwidth}",
"\\\\\\hspace{\\textwidth}",
"SOURCE: Here is my source with an extra linebreak to separate it.",
"\\\\\\hspace{\\textwidth}",
"{\\tiny EXTRA: Here is smaller text. You could explore more text sizing in \\LaTeX.} And bigger again.",
"\\label{figure:demo-of-multiline-captions}",
"\n" )
cat(x)
#
We can also show the variable "x" in the "asis" formate here:
<<results="asis">>=
cat(x)
#
This text right here (now) is a new paragraph, so it is indented.
Below, in \textbf{Figure~\ref{figure:demo-of-multiline-captions}}, we use the "x" variable in the "fig.cap=" argument to Knitr.
<<fig1, echo=FALSE,fig.cap=x,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
#
\end{document}
Results give this:

Using in line r code as part of a R markdown header

I wish to use in line R code as part of a header in a r markdown file. However when i knit the file the fonts used on the header are different. How can I ensure the fonts are the same. A simple example is below.
`r 1+1` Header
-------------------------
You can wrap content in backticks to denote r code inline, as follows:
## Title `r 1+1` Header
Without a reproducible example it is hard to be precise, but one thing you might want to consider is to use the results="asis" chunk option in your R code so that the results are not wrapped in a code markup block. I am not sure how this works with inline commands, but you could use a regular R block and have it create the entire header from the R code, something like:
```r results="asis"
cat('# ', 1+1, " Header")
```

Resources