Drthm1456
Drthm1456

Reputation: 479

Underline in RMarkdown to Microsoft Word

I am trying to figure out in RMarkdown how to underline some words. If I am knitting to HTML I can do this:

<u>These words are underlined</u>

Which works fine in that case. But the underlining is not persisted when I knit to Microsoft Word. I don't believe any changes have been made to RMarkdown to natively do it such as the commands for bold and italics. Any suggestions?

Thanks

Upvotes: 2

Views: 9274

Answers (5)

Carlos Luis Rivera
Carlos Luis Rivera

Reputation: 3663

Simply you can use the underline class [Your text]{.underline}, which pandoc innately supports, as like the small caps class [Your text]{.smallcaps}. This method also works for the production of other formats, such as HTML and PDF.

---
title: "Untitled"
output: word_document
---

[Underlined text]{.underline}

[Small Capital text]{.smallcaps}

Upvotes: 9

Cyrus Yip
Cyrus Yip

Reputation: 649

A workaround

RMarkdown is based on Pandoc, and Pandoc's Markdown does not natively support underline. Underline in Pandoc's Markdown is implemented by HTML tag <u>...</u>. However, <u>...</u> cannot be not converted into underline text in docx or pdf(tested with pandoc 2.10.1). A workaround is modifying the text style in MS Word.

  1. Choose a substitute for <u>...</u>, such as ***...***.

    ---
    output:
      word_document: default
    ---
    
    This is a regular paragraph.
    
    ***This paragraph is bold italic at first. It will be underlined later.***
    
  2. Knit Rmarkdown document into docx

  3. Replace bold italic text with underline text in Word.

    • Press Ctrl + H.
    • Set the text format in Format > Font.
      • Find Bold Italic text.
      • Replace with Regular Underline text.
    • Press Replace All
  4. If you have used <u>...</u>, replace them with ***...***.

    • Press Ctrl + F.

    • check the regex option.

    • replace <u>(.+)<\/u> with ***$1*** .

    • Press All next to Replace.

    • Return to step 1 and step 2.

Advice

I recommend sticking to the native syntax of Rmarkdown and Pandoc's Markdown. Thought the syntax is limited, it works well with different output formats. Limited syntax is the power of Markdown! It is so simple that it can be converted into many different output formats. I also recommend following the concept of separation of content and style. It saves me from formatting contents repeatedly.

Upvotes: 1

fry
fry

Reputation: 538

The problem has two parts:
1.) Getting Rmarkdown/pandoc to render the html when knitting to a word file. How to achieve this has been explained here: HTML tags in Rmarkdown to word document by user @tarleb (please consider upvoting that answer as it is the basis for mine).
2.) <u> for underlining has been more or less deprecated. However, the <u> tags simply have to be wrapped inside a <p> to work.

So this should achieve what you want:

---
output:
  word_document:
    md_extensions: +raw_html-markdown_in_html_blocks
    pandoc_args: ['--lua-filter', 'read_html.lua']
---

<p><u>This text will be underlined.</u> This not anymore</p>

<u>But this will not.</u>

<p style="text-decoration: underline;">Neither will this.</p>

with a file read_html.lua in the same directory with this content:

function RawBlock (raw)
  if raw.format:match 'html' and not FORMAT:match 'html' then
    return pandoc.read(raw.text, raw.format).blocks
  end
end

Upvotes: 1

stefan
stefan

Reputation: 125373

For what it's worth ... (; As far as I get it, this is not possible by using HTML tags. The reason why HTML tags work when rendering to HTML is that the HTML code is not touched upon by rmarkdown, knitr or pandoc and simply passed through to the final HTML document as text. In case of HTML the browser knows what to do with this "text". But in Word or Latex it's simply text which will be displayed as is.

However, for Word output you could have a look at the officedown package which adds some addtional Word functionalities to rmarkdown via the officer officer package , e.g. the following example RMD shows how to get underlined text in Word:

---
title: "officedown template"
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.cap = TRUE)
library(officedown)
library(officer)

ft <- fp_text(underlined = TRUE)
```


This document presents most of the features of the package `r ftext("officedown", ft)`. 

enter image description here

Upvotes: 2

Steffen Moritz
Steffen Moritz

Reputation: 7730

Just tried everything that came to my mind - seems to be not possible (would be great to hear about actually working solutions)

I tried the following:

<u> text </u>
<ins> text </ins>
<span style="text-decoration:underline"> text </span>
$\text{\underline{LaTeX makes it possible}}$
$\underline{LaTeX makes it possible}$

While a most of these worked for html_document - none really worked for Rmarkdown to Word.

  1. <u> - the obvious solution - didn't work.

  2. <ins> which is also often suggested - didn't work.

  3. html5 markup - didn't work

  4. LaTeX - did not work

Might well be, that this just is not possible.

Upvotes: 1

Related Questions