I have a chunk of R code that I'm trying to put into a word doc using "Knit Word" in R Markdown. The R Markdown chunk is as follows:
```{r}
all <- readHTMLTable('http://en.wikipedia.org/wiki/Demographics_of_the_United_States')
```
When I Knit Word, however, the line of code goes across three lines because the font size is too large.
I tried using size = 'tiny' as an option in the chunk, but this doesn't change the font size at all. How can I change the font size of this line of code, so that it is all on one line, without manually changing the font size in the Word doc? Thank you!
Related
I'm using pdf output.
![title at top^[Font: Own Elaboration (this must be under the image)] \label{fig:VPBFX}](diagram.png)
Or how can i do it with knitr?
Thank you.
I'm using Rmarkdown to create pdf files. I have chunks inside the .Rmd file like this one:
```{r}
knitr::include_graphics('image.png')
```
This is converted in the .tex file to:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr}\OperatorTok{::}\KeywordTok{include_graphics}\NormalTok{(}\StringTok{'image.png'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=8.88in]{image}
Unfortunately, in most cases that I've seen the width is not appropriate. How to automatically remove the "[width=8.88in]" ? On an other computer the width was not set like this, even with the same .Rmd file, and the images had the appropriate size.
But I cannot figure where this difference comes from, as I do not have access to this computer anymore.
Thank you
Please see the argument dpi on the help page ?knitr::include_graphics. In short:
knitr::include_graphics('image.png', dpi = NA)
Or manually specify a width via the out.width chunk option.
I am programming in Rstudio using markdown files, alternating between Markdown and R chunks of code. If I plot something in a chunk, the plot is shown below the chunk.
Does anybody know how I can make the plot look square instead of rectangular?
use this kind or header of chunk with fig.width = fig.height
<<R_chunk_title,echo=FALSE,warning=FALSE,message=FALSE,fig.width=6,fig.height=6,cache=TRUE>>=
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")
```
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: