Rmarkdown knit pdf - getting underlined text instead of italic using *italic* (huxtable issue?) - pdf

Rmarkdown text (between chunks) when formated italic using * * knits to pdf underlined and not italic format when I print huxtable.
Here is my example:
```
---
title: "<center><center>"
author: "<center> jd <center><br>"
date: "<center> `r Sys.Date()` <center>"
output:
pdf_document:
fig_caption: yes
toc: yes
toc_depth: 3
number_sections: true
latex_engine: xelatex
html_document:
code_folding: show
df_print: paged
theme: yeti
highlight: tango
toc: yes
toc_float:
collapsed: false
smooth_scroll: false
number_sections: true
fontsize: 10pt
---
This * * makes text *italic*.
```{r lib, message = FALSE}
library(huxtable)
library(tidyverse)
data(iris)
dt_hux <- iris[1:5,1:5] %>% as_hux() %>%
set_font_size(8) %>% set_font("Arial") %>%
set_bold(1, everywhere) %>%
set_top_border(1, everywhere) %>%
set_bottom_border(c(1, 6), everywhere)```
Until this point using * * will give italic format in knit pdf (if next chunck is not run).
But after the next chunk is run * * will underline text (in whole Rmarkdown). Commenting out **dt_hux** returns formatting to italic. Also knit to html will print italic formatting even with dt_hux.
```{r table}
options(huxtable.latex_use_fontspec = TRUE)
options(huxtable.print=print_latex)
dt_hux```
```
Is there a solution to this issues as I need to print huxtable in pdf?

From the TeXnical perspective the problem is that the ulem package is loaded without the normalem option. A couple of workarounds:
use classoption: normalem (based on Knitr hook to add code before \documentclass line in tex file to avoid options clash with xcolor). Caveat: this will pass the option to all packages and might be undesired in case the same option name is also used by other packages (I'm not aware of any other package that uses this option, but just in case ...)
add \normalem either as header-include or at the start of your document

This problem was fixed in huxtable 5.2.0, so you just need to update your package.

Related

Put 2 chunks of code side by side in RMarkdown or Quarto

How can I put 2 chunks of code side by side in the output file of RMarkdown or Quarto ?
Code
library(dplyr)
mtcars %>% select(gear)
library(dplyr)
select(mtcars, gear)
Desired layout in the PDF or HTML file
The canonical way for something like this is to use column divs:
::::: columns
::: column
```r
library(dplyr)
mtcars %>% select(gear)
```
:::
::: column
```r
library(dplyr)
select(mtcars, gear)
```
:::
:::::
This will work with HTML, reveal.js, Beamer, and Powerpoint. The default result looks a bit ugly in HTML, as there is no space between the two blocks, but we can fix that with a tiny bit of CSS. We can put it directly into the document:
<style>
.column { padding-right: 1ex }
.column + .column { padding-left: 1ex }
</style>
Things get more complicated if we wish to do the same for PDF. We'll need convert the divs into a table, as that's the most effective way to get elements side-by-side. But that requires some heavier tools. In the YAML header, add
output:
pdf_document:
pandoc_args:
- "--lua-filter=columns-to-table.lua"
Then save the below code into a file column-to-table.lua.
function Div (div)
if div.classes:includes 'columns' then
local columns = div.content
:filter(function (x)
return x.classes and x.classes[1] == 'column'
end)
:map(function (x)
return x.content
end)
local aligns = {}
local widths = {}
local headers = {}
for i, k in ipairs(columns) do
aligns[i] = 'AlignDefault'
widths[i] = 0.98/ #columns
end
return pandoc.utils.from_simple_table(
pandoc.SimpleTable('', aligns, widths, headers, {columns})
)
end
end
You can get rid of the lines around the table by adding
\renewcommand\toprule[2]\relax
\renewcommand\bottomrule[2]\relax
at the beginning of your document.
---
title: "Untitled"
output: html_document
---
:::::::::::::: {.columns}
::: {.column width="50%"}
```{r warning=FALSE,message=FALSE}
library(dplyr)
mtcars %>% select(gear)
```
:::
::: {.column width="50%"}
```{r warning=FALSE,message=FALSE}
library(dplyr)
select(mtcars, gear)
```
:::
::::::::::::::
used This SO question as a resource. This is using pandoc to format the document in Rmarkdown HTML output

Short header for a very long Section's title in beamer presentation R Markdown (Pandoc)

I am working on a beamer_presentation which consists of several sections. The section titles are too long to fit into the CambridgeUS headline. I have already tried several options from related latex sources to use some short title for the section (say just "Chapter 1" instead of "Chapter 1 with a very-very-very-very-very long title" by \AtBeginSection{\title[Short title]{Long title}}) but yet can't figure out how to manage this in header-includes of R Markdown YAML. I set the section title through # in Markdown
---
title: Title for the whole presentation
subtitle: 'Presentation'
author:
- Author
institute: "Institution"
date: " `r format(Sys.Date(), '%B %d, %Y')`"
output:
beamer_presentation:
latex_engine: xelatex
toc: true
highlight: tango
theme: "CambridgeUS"
colortheme: "lily"
fonttheme: "serif"
slide_level: 3
keep_tex: true
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \AtBeginDocument{\title[Presentation]{Title for whole presentation}}
- \renewcommand{\raggedright}{\leftskip=0pt \rightskip=0pt plus 0cm}
- \def\sectionname{Chapter}
- \AtBeginSubsection{}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
# Chapter 1 with a very-very-very-very-very long title
## Intro
### The first page
In beamer, this is trivial, you can use \section[short version]{long version}. But Markdown makes such an easy job excruciatingly difficult.
However you can trick markdown like this:
---
title: Title for the whole presentation
subtitle: 'Presentation'
author:
- Author
institute: "Institution"
date: " `r format(Sys.Date(), '%B %d, %Y')`"
output:
beamer_presentation:
latex_engine: xelatex
toc: false
highlight: tango
theme: "CambridgeUS"
colortheme: "lily"
fonttheme: "serif"
slide_level: 3
keep_tex: true
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \AtBeginDocument{\title[Presentation]{Title for whole presentation}}
- \renewcommand{\raggedright}{\leftskip=0pt \rightskip=0pt plus 0cm}
- \def\sectionname{Chapter}
- \AtBeginSubsection{}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{=latex}
\tableofcontents[hideallsubsections]
\end{frame}
\section[short version for headline]{Chapter 1 with a very-very-very-very-very long title}
\subsection[short subsection for headline]{Intro}
\begin{frame}
\frametitle{The first page}
```
some text
(be careful where you place this command, otherwise you might end up with empty extra frames - that's why I had to switch off the automatic toc and insert it manually...)

R markdown: simplify creating tables of figures and text

For R markdown Rmd web pages I want to generate tables containing in the first column thumbnail images (that link to a larger image or a web site) and
descriptive text in the 2nd column. One example is the following image:
I know I can create this manually in raw HTML, but that is very fiddly and time-consuming. There must be some easier way.
On a different page, I tried a markdown / pandoc table, but that didn't work, and I reverted to manual coding of HTML
icon | title
--------------------------------------------------+--------------------------
<img src="images/books/R-Graphics.jpg" height=50> |Paul Murrell, *R Graphics*, 2nd Ed.
<img src="images/books/R-graphics-cookbook.jpg" height=50> | Winston Chang, R Graphics Cookbook
<img src="images/books/lattice.png" height=50> | Deepayan Sarkar, *lattice*
<img src="images/books/ggplot2.jpg" height=50> | Hadley Wickham, *ggplot2*
Perhaps the htmltools package would be useful here, but I can't quite see how to use it in my Rmd files for this application.
Probably forgot escaping quotes? This works fine for me:
---
title: "The Mighty Doge"
output: html_document
---
```{r}
library(knitr)
create_thumbnail <- function(file) {
paste0("<img src=\"", file, "\" style=\"width: 50px;\"/>")
}
df <- data.frame(Image = rep("unnamed.png", 5),
Description = rep("Doge", 5))
df$Image <- create_thumbnail(df$Image)
kable(df)
```
Here is an approach that uses htmltools and seems much more flexible, in that I can control the details somewhat more easily.
I'm not familiar with bootstrap <div> constructs, so I used HTML table constructs. I had to define functions for tr(), td() etc.
```{r html-setup, echo=FALSE}
library(htmltools)
# table tags
tab <- function (...)
tags$table(...)
td <- function (...)
tags$td(...)
tr <- function (...)
tags$tr(...)
# an <a> tag with href as the text to be displayed
aself <- function (href, ...)
a(href, href=href, ...)
```
Then functions to construct my table entries the way I wanted:
```{r table-functions, echo=FALSE}
# thumnail figure with href in a table column
tabfig <- function(name, img, href, width) {
td(
a(class = "thumbnail", title = name, href = href,
img(src = img, width=width)
)
)
}
tabtxt <- function(text, ...) {
td(text, ...)
}
```
Finally, use them to input the entries:
## Blogs
```{r do-blogs, echo=FALSE}
width="160px"
tab(
tr(
tabfig("FlowingData", "images/blogs/flowingdata.png", "http://flowingdata.com/", width=width),
tabtxt("Nathan Yau,", aself("flowingdata.com/"),
"A large number of blog posts illustrating data visualization methods with tutorials on how do do these with R and other software.")
),
tr(
tabfig("Junk Charts", "images/blogs/junkcharts.png", "http://junkcharts.typepad.com/", width=width),
tabtxt("Kaiser Fung,", aself("http://junkcharts.typepad.com/"),
"Fung discusses a variety of data displays and how they can be improved.")
),
tr(
tabfig("Data Stories", "images/blogs/datastories.png", "http://datastori.es/", width=width),
tabtxt("A podcast on data visualization with Enrico Bertini and Moritz Stefaner,",
aself("http://datastori.es/"),
"Interviews with over 100 graphic designers & developers.")
)
)
```
I still need to tweak the padding, but this gives me more or less what I was after:

shiny ioslides selectInput scrollbar failure

I need to have two consecutive slides use similar selectInputs. When I do this, the scrollbar on the first slide fails. I use "selectize=FALSE" so that the user can deselect options. The scrollbar works fine when it's just one slide.
Am I doing something wrong, or is this a bug? Any help would be greatly appreciated!
---
title: "Scrollbar Failure Demo"
author: "KM"
date: "May 20, 2016"
runtime: shiny
output: ioslides_presentation
---
## Slide1
```{r Slide1, echo=FALSE}
fulllist <- c(1,2,3,4,5,6,7,8,9,10)
inputPanel(
selectInput("Option1", label="Pick Many", multiple=TRUE, selectize=FALSE,
choices=fulllist, selected=fulllist))
renderPlot({plot(input$Option1)
title(main="Scrollbar Fails on Slide 1")})
```
## Slide2
```{r Slide2, echo=FALSE}
inputPanel(
selectInput("Option2", label="Pick Many", multiple=TRUE, selectize=FALSE,
choices=fulllist, selected=fulllist))
renderPlot({plot(input$Option2)
title(main="Scrollbar Works on Slide 2")})
```

How to extract Highlighted Parts from PDF files

Is there any way to extract highlighted text from a PDF file programmatically? Any language is welcome. I have found several libraries with Python, Java, and also PHP but none of them do the job.
To extract highlighted parts, you can use PyMuPDF. Here is an example which works with this pdf file:
Direct download
# Based on https://stackoverflow.com/a/62859169/562769
from typing import List, Tuple
import fitz # install with 'pip install pymupdf'
def _parse_highlight(annot: fitz.Annot, wordlist: List[Tuple[float, float, float, float, str, int, int, int]]) -> str:
points = annot.vertices
quad_count = int(len(points) / 4)
sentences = []
for i in range(quad_count):
# where the highlighted part is
r = fitz.Quad(points[i * 4 : i * 4 + 4]).rect
words = [w for w in wordlist if fitz.Rect(w[:4]).intersects(r)]
sentences.append(" ".join(w[4] for w in words))
sentence = " ".join(sentences)
return sentence
def handle_page(page):
wordlist = page.get_text("words") # list of words on page
wordlist.sort(key=lambda w: (w[3], w[0])) # ascending y, then x
highlights = []
annot = page.first_annot
while annot:
if annot.type[0] == 8:
highlights.append(_parse_highlight(annot, wordlist))
annot = annot.next
return highlights
def main(filepath: str) -> List:
doc = fitz.open(filepath)
highlights = []
for page in doc:
highlights += handle_page(page)
return highlights
if __name__ == "__main__":
print(main("PDF-export-example-with-notes.pdf"))
Ok, after looking I found a solution for exporting highlighted text from a pdf to a text file. Is not very hard:
First, you highlight your text with the tool you like to use (in my case, I highlight while I'm reading on an iPad using Goodreader app).
Transfer your pdf to a computer and open it using Skim (a pdf reader, free and easy to find on the web)
On FILE, choose CONVERT NOTES and convert all the notes of your document to SKIM NOTES.
That's all: simply go to EXPORT an choose EXPORT SKIM NOTES. It will export you a list of your highlighted text. Once opened this list can be exported again to a txt format file.
Not much work to do, and the result is fantastic.