Text wrapping in code sections using Tufte format - r

Text in the code section of a Tufte formatted, Rmarkdown document does not seem to wrap. I have tried several options and suggestions, but nothing works. I am guessing that all of the other suggestions I could find on StackOverflow did not work, because of the "tufte" format. I am using knitr to construct HTML or PDF documents. Here is the minimal example:
---
title: "Width Minimal Example"
output:
tufte::tufte_handout: default
tufte::tufte_html: default
---
```{r setup, include=FALSE}
library(tufte)
library(formatR)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = TRUE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r, tidy='styler', tidy.opts=list(width.cutoff=50)}
filename <- "/Test/test/test/test/test/test/test/test/test/test/test/test.data"
```
When I use knitr to produce HTML or PDF (handout), the "filename" variable runs out of the textbox allotted for code. I've looked through the "tufte.css" file and nothing there seems to describe the width of text in code boxes.
Ideally, the text in the code box would not only wrap, but indent a bit so that the person reading the code would easily be able to see which lines were new lines and which lines were wrapped lines. Thanks!

Related

Make R Markdown output accessible

I'd like to output an accessible version of a presentation I'm making with R Markdown ioslides. At this point, I've been struggling to find much documentation on alt-text for dynamically generated images - not pointers to images - there's lots of information available online on that front.
Seems like the easiest thing to do might be to output my document as a word_document instead of a presentation, and ensure that figures have captions, using fig.cap argument in the chunk header.
However, I'd like the captions to be available only as alt-text - so that this document doesn't look strange to folks who aren't using screen reader tech (I'd like to make my alt-text nice and descriptive, but for folks who can see ok, this will seem odd, because the graph will do the talking instead).
I've included a reprex below. How can I still include the fig.cap as alt-text in the output word document, but not have it show up underneath the graph?
Also, if you are aware of any good resources on making RMarkdown docs accessible, I am all ears!!
Thanks for your help.
---
title: 'test doc'
author: "Nova"
date: "April 2020"
output:
word_document:
fig_height: 5
fig_width: 10
height: 500
widescreen: yes
width: 500
---
```{r setup and comments, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = F,
warning = F,
fig.height = 5,
fig.width = 10,
fig.cap = NA)
library(ggplot2)
```
## cars
```{r ggplot, include=T, fig.cap = "this is some alternative text about this figure"}
ggplot(cars)
```
I'd go back to some kind of HTML output, because it's simpler.
If you want ioslides output, you can use fig.cap="Some alt text" in the chunk option to set the alt text for the figure. Unfortunately, this also generates a visible caption, but you can suppress that. For one slide, just insert inline CSS:
<style>
.caption {
display:none;
}
</style>
somewhere on the slide before the figure. To do it for the whole presentation, put that text in a .css file and include it with YAML like
output:
ioslides_presentation:
css: style.css
It's probably also possible to change the template for this output style, but that looks like a lot more work.

Is there a way to add line breaks ONLY when exporting to PDF in R Markdown?

I think the question is quite self-explanatory but for avoidance of doubt I'll explain with more detail below:
I have an R Markdown document that works well if converted to HTML or uploaded to GitHub. When converting to PDF (using Latex), the results are not so pretty. I find that the biggest problem in a Latex PDF document are line breaks. I can fix the line breaks issue on the PDF document by adding "\ " characters, but that throws my HTML document out of whack too.
Is there a way to manually add line breaks (or "space before/after paragraphs") for the PDF output only?
Thank you!
You can redefine the relevant spacings in the YAML header. \parskip controls the paragraph spacing. Code blocks are shaded using a snugshade environment from the framed package. We can also redefine the shaded environment for code blocks to have some vertical space at the start. Here's a reproducible example. Note: I also added the keep_tex parameter so you can see exactly what the generated tex file looks like, in case this is useful:
title: "test"
author: "A.N. Other"
header-includes:
- \setlength{\parskip}{\baselineskip}
- \renewenvironment{Shaded}{\vspace{\parskip}\begin{snugshade}}{\end{snugshade}}
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Once you output to HTML, you can just print the HTML webpage as PDF. that might be an easy way keep the original format

Company logo in PDF output only on the first page

I was wondering if there is a way to include the company logo to a PDF document created by R Markdown only on the first page of the document. I have searched for an answer and the closest I could find is this. There are multiple solutions presented in the answers, however, neither of them works for me because they either:
include the logo at every page of the document (not just first); or
include the chapter title in the header (and a horizontal line below it) along with the logo.
I am looking for a way to include just the plain logo, with no chapter titles only on the first page of a PDF R Markdown document and I've ran out of all the resources I could find online. Is there someone who could aid me?
Before anyone asks: yes, it strictly has to be a PDF document, not HTML.
The \includegraphics code line is insert after the R SETUP chunk in markdown.
Here is an example of where I put the line \includegraphics :
---
geometry: "margin=1.5cm"
classoption: landscape
output:
pdf_document: # Export to pdf
number_sections: yes
includes:
in_header: ltx.sty
---
```{r SETUP, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
results = "asis")
library(knitr)
library(kableExtra)
library(some_packages)
options(scipen = 999,
digits = 2,
width = 110)
```
\definecolor{astral}{RGB}{87,146,204}
\allsectionsfont{\color{astral}}
\setcounter{tocdepth}{5}
<!-- Title page -->
\includegraphics[width=7cm]{logo.jpg}
\begin{center}\begin{Large}
Project 1
\end{Large}\end{center}
\vfill
# Some R chunk
```{r results='asis'}
# Table, code, graphics.
```
So the line is insert between the R SETUP chunk and the next R chunk.

Change margin size in cover page

I am trying to add a cover page which is already created in a PDF.
```{r echo=FALSE,out.width='8.27in',out.height='11.69in'}
knitr::include_graphics('CoverPage.pdf')
```
I want to change the margins of the first page only. Can you please help me with this?
The following was tried but it changes the margin of the whole document:
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
If you are fine with some bare LaTeX in your RMD file, then this can be easily achieved using the geometry package. Just define a newgeometry before the cover is included and restoregeometry afterwards (reads almost like English …).
---
output:
pdf_document
---
```{r, echo = FALSE, results = "asis"}
cat("\\newgeometry{left=3cm,right=3cm,top=2cm,bottom=2cm}")
knitr::include_graphics("CoverPage.pdf")
cat("\\restoregeometry")
```
\clearpage
Note the position of the page number. Restoring the margins was successful!
It is important to use the chunk option results="asis" and cat() to print raw LaTeX and to escape the backslashes with an additional backslash.

Changing page size within Rmarkdown document

I have a very large phylogenetic tree that I'd quite like to insert into a supplementary material I'm writing using Rmarkdown and knitr. I dislike splitting trees across pages and I doubt anybody would print this out anyway so I thought I'd just have a large page in the middle of the pdf I'm generating.
The question is how do I change page size for one page in an otherwise A4 document? I'm pretty new to knitr and I've found global paper size options but I'm struggling to find ways of setting up what would be the equivalent of sections in Word.
(Update) Hi does anybody else have a suggestion? I tried the pdfpages package but this seems to result in an equally small figure on a page the size of the pdf that is being pasted in i.e. if I make a 20in by 20in pdf figure then paste the page in using \includepdf then I get a 20in by 20in page with a much smaller figure on it (the same as the \eject example above). It seems like knitr or Latex is forcing the graphics to have a specific size regardless of page size. Any ideas? Here's a reproducible example:
---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{pdfpages}
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = FALSE,
warning=FALSE,
message=FALSE,
dev = 'pdf',
fig.align='center')
```
```{r mtcars, echo=FALSE}
library(ggplot2)
pdf("myplot.pdf", width=20, height=20)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
dev.off()
```
#Here's some text on a normal page, the following page is bigger but has a tiny figure.
\newpage
\includepdf[fitpaper=true]{myplot.pdf}
You should be able to use \ejectpage like this:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\eject \pdfpagewidth=20in \pdfpageheight=20in
```{r mtcars}
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
```
\eject \pdfpagewidth=210mm \pdfpageheight=297mm
Back
(I can only remember the A4 height in mm for some reason)
I have just faced the same struggle when dealing with a large phylogenetic tree.
Based on hrbrmstr's answer, I came up with the snippet below.
The trick is to make knitr generate the figure but not include it in the intermediate .tex right away (hence fig.show="hide"). Then, because figure paths are predictable, you can insert it with some latex after the code chunk.
However, the new page height is not taken into account when positioning page numbers, so they tend to be printed over your image. I have tried to overcome this behavior multiple times, but in the end I simply turned them off with \thispagestyle{empty}.
---
output: pdf_document
---
```{r fig_name, fig.show="hide", fig.height=30, fig.width=7}
plot(1)
```
\clearpage
\thispagestyle{empty}
\pdfpageheight=33in
\begin{figure}[p]
\caption{This is your caption.}\label{fig:fig_name}
{\centering \includegraphics[height=30in, keepaspectratio]{main_files/figure-latex/fig_name-1} }
\end{figure}
\clearpage
\pdfpageheight=11in

Resources