I am having trouble with the table of content in a md_document or github_document.
If I do that, the TOC is displayed in the rendered document, but the links do not work:
---
output:
md_document:
toc: true
toc_depth: 4
---
### 1. Header
### 2. Header
#### 2.1. Subheader
#### 2.2 Subheader
Same problem with github_document; but no problem with html_document.
If I remove the numbers in the section headers, the links in the TOC work.
Is there a way to have section numbers in an md or github_document?
Unfortunately, GitHub does not display HTML docs nicely, so I have to use either md or github_document, but my file is long and the numbering would help to follow its structure.
Thank you in advance
What if you back "md_document:" up so it is directly under the "Output:"? You said your links work when you remove the numbers in the section headers, but I don't see any links?
Related
I'm trying to reduce the size of a section of text within a qmd document that needs rendering in docx.
I tried using a styled div:
---
title: "Test"
format: docx
---
The main text will be normal-sized.
::: {style="font-size: 10pt;"}
This block of text should be sized 10pt.
:::
Observed output:
Intended outptut:
Of note, my code works well in html.
Is there another way of doing this with a docx output?
The styling process for docx is a little different: one needs to create a "reference doc", set the styles in that doc and apply it with
---
format:
docx:
reference-doc: my-styled-reference.docx
---
See How to specify the font used for word doc exported using pandoc? and How do I add custom formatting to docx files generated in Pandoc? for more details on this process.
How can i find out which bootstrap scss variable is used by rmarkdown / bslib to color page elements? e.g. for coloring the TOC background?
Here is a page's yaml
output:
html_document:
self_contained: false
theme:
version: 4
bootswatch: cyborg
toc: yes
toc_depth: 3
toc_float:
collapsed: true
There are several ways, but the easiest might be to use developer tools. You can do this in RStudio or your browser. After knitting, right-click and select "Inspect Element," "Inspect," or something along those lines (different browsers use slightly different names).
Either your screen will shift to fit this new view, or you'll see a new window open.
In RStudio, the top left of the window has a box/arrow symbol. Click it on (it turns green for me).
That icon looks like this:
Then move your cursor to the viewer pane, to the element in your RMD output that you want to know more about. Select the element by clicking on it two times (not double-click, more like 'select' and 'ya, I'm sure...').
Return to the inspector window. You're going to see something is highlighted. That's the HTML element for the element in your RMD output.
The style pane's content (on the right) has the styling—AKA CSS.
Next to the "Styles" header over the styles pane is "Computed." If you select "Computed," you'll get a summary of the applied styles.
My example RMD title is black:
This could look different depending on your browser and your OS, but the functionality is similar. If you have any questions, let me know.
Update
From your comments, here are more specifics.
I started with the default RMD for bslib templates but replaced the YAML with the one provided in your question. I added some additional TOC elements and deleted a bit of the template. This is what I started with (hovering on one of the TOC elements to show the contrast).
Then I went to the inspector and selected the TOC two times. Then I returned to the inspector window to see what element was highlighted.
Next, I went to the Computed tab, and scrolled to the element background-color.
This told me what to look for and where to look. I could go to the inspector pane 'Source', but since you provided the link to the code in Github, I went there. I searched for "list-group." This is what I found there:
I can go back to the RMD and update my YAML now, like this:
---
title: "Theming with bslib and thematic"
output:
html_document:
self_contained: false
theme:
version: 4
bootswatch: cyborg
list-group-bg: '#b21e29'
list-group-active-bg: '#dde26a'
toc: yes
toc_depth: 3
toc_float:
collapsed: true
---
(I choose a hideous color combination, not entirely on accident...)
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
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}
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)`.
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.
<u> - the obvious solution - didn't work.
<ins> which is also often suggested - didn't work.
html5 markup - didn't work
LaTeX - did not work
Might well be, that this just is not possible.
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
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.
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.***
Knit Rmarkdown document into docx
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
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.
Using r markdown, I need to overlay a string on an existing image in the header of a docx one that is aligned to the bottom right hand corner of the header.
Also, I need to be able to alter the content of the string if possible.
I've got the image using the reference document approach suggested by Yihui Xie in the YAML header:
---
title: "Untitled"
author: "Simon"
date: "Thursday, May 21, 2015"
output:
word_document:
reference_docx: template_image.docx
---
[edit] In case it is helpful you can find template_image.docx here. If anyone could tell me how I could overlay text on that header image using rmarkdown I could appreciate it very much.
I've got an .Rmd I need to output using the word_document2 format with R bookdown. It has a .docx template which works fine for heading styles etc.
However, it appears that the figure/table captions are output using the "normal" style, so I can't control the font, size etc. on them as a group and am having to go through after the fact and change them all by hand. Is there a way to get bookdown to designate them to use Word's "caption" style, or alternatively to specify a custom style for them?
In your YAML header for your .Rmd, you can specify a Word doc to use as the basis for all formatting. Your rendered word doc will pick up all the styles from this reference_docx and apply them to your output:
---
title: "Example .Rmd"
output:
word_document:
toc: yes
reference_docx: word-styles-reference-01.docx
theme: hpstr
fontsize: 11pt
---
The Word file that I am using for styles is here: https://drive.google.com/open?id=1gSyE22hJbGdsTj6C-RWBTnyBVnG0XwB3
In trying to make a reproducible example, I found the problem (apologies for not including an example in the first place).
The issue wasn't with the .Rmd, but the reference .docx itself. It had a "Caption" style, but for some reason it didn't have "Image Caption" or "Table Caption" styles, which is what markdown was looking for. I copied those styles over from another Word document according to these instructions, and now it works fine.