Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show toc in a rmarkdown file? #58

Closed
likanzhan opened this issue Feb 21, 2017 · 6 comments
Closed

show toc in a rmarkdown file? #58

likanzhan opened this issue Feb 21, 2017 · 6 comments
Milestone

Comments

@likanzhan
Copy link

When the document is a .md file, the table of contents from the file can be extracted via .TableOfContents. But if the file extension is .rmd, the toc cannot be extracted. Any suggestions? Thanks.

@yihui
Copy link
Member

yihui commented Feb 22, 2017

Yes. You can add this to the YAML metadata of your Rmd document:

output:
  blogdown::html_page:
    toc: true

Full documentation at https://bookdown.org/yihui/blogdown/templates.html#how-to

@yihui yihui added this to the v0.1 milestone Feb 22, 2017
@raybuhr
Copy link

raybuhr commented May 4, 2017

EDIT: This is totally working, just I made some poor assumptions. For anyone who finds this in the future, don't render the .Rmd using knitr just use the blogdown::serve_site() and blogdown will do the rendering of the Rmd for you.

---- Previous

This ins't working for me.

Here is my YAML metadata:

---
title: "Using Hugo and Blogdown"
keywords: tech
date: '2017-05-04'
disqusIdentifier: 2c5a0040
tags:
- tech
- blog
- rmd
- blogdown
- hugo
output:
  blogdown::html_page:
    toc: true

---

I also tried substituting blogdown::html_page: with html_document, which does generate the toc in the knitted html but doesn't render correctly once hugo builds the site. Both options end up looking like this on my website:

January 1, 1
Using Hugo and Blogdown code{white-space: pre;} pre:not([class]) { background-color: white; } if (window.hljs && document.readyState && document.readyState === “complete”) { window.setTimeout(function() { hljs.initHighlighting(); }, 0); } h1 { font-size: 34px; } h1.title { font-size: 38px; } h2 { font-size: 30px; } h3 { font-size: 24px; } h4 { font-size: 18px; } h5 { font-size: 16px; } h6 { font-size: 12px; } . 

I am using hugo version 0.20.7 on ubuntu, Rmarkdown version 1.5 and blogdown version 0.0.6 fwiw.

@hezhaobin
Copy link

Is it possible to add a checkbox in the "new post" addins to add the toc automatically? I find myself google the grammar of this all the time. Besides, will the floating TOC function be added to blogdown any time soon? Thanks Yihui for the nice work.

@yihui
Copy link
Member

yihui commented May 7, 2018

@hezhaobin The problem for a checkbox in the New Post addin is that it is hard to tell what it should do in the case of plain Markdown posts (the implementation of TOC is theme-dependent).

For the floating TOC, it is also up to the specific Hugo theme. If you are familiar with HTML/CSS/JS, you could implement it by yourself.

@DataStrategist
Copy link

DataStrategist commented Apr 1, 2019

Hi Yihui.

I just got my toc to work in a Rmd in blogdown. I wanted to implement something like:

output: 
  blogdown::html_page:
    toc: true
    toc_float: true
    smart: false

or

output:
  html_document:
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false

but then I saw section 5.3 of https://bookdown.org/yihui/blogdown/rmd-website.html and I guess the folding and following etc stuff is not implemented in blogdown? I guess it's hard because it would have to be done for each theme, no?

So I should just go with the standard toc and be damn happy that it works? :)

Just in case it's useful, there IS a really nasty hack... not sure if you'll like it or not, you COULD instead of blogdown::build_site(), if you DO just knit the document instead, the resultant html file will bypass all the Hugo stuff and will create the file w/ the floating TOC, but it will break everything else. So it'll work as a freestanding file. But you have to use html_document for that.

output: 
  html_document:
    toc: true
    toc_float: true
    smart: false

@yihui
Copy link
Member

yihui commented Apr 1, 2019

I guess it's hard because it would have to be done for each theme

That is correct. It is easy to do the floating TOC well under the Bootstrap framework. It would be hard to do it for arbitrary CSS frameworks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants