How to add table of contents to R Markdown HTML file using pandoc but retain all the HTML formatting and header information?
E.g., If I had a file called test.html, I tried:
pandoc -s -S --toc test.html -o test-toc.html
This adds the table of contents but it removes the existing header information which makes all the formatting attractive.
Thus, it makes this html file look like this one. I’d like to preserve the formatting.
I’m not sure how you can tell Pandoc to keep all the style and header information. However, presumably this style does not change very much, so why don’t you copy this into a
.cssstylesheet file and point to this file when generating the output HTML (with the-cor--csscommand line options)? You can tell Pandoc to embed this stylesheet information into your final output using the--self-containedcommand line option (see here for the full list of options).For the MathJax script in your input HTML file you can use
Finally, for the R syntax highlighter you can use
where
r_syntax.htmlcontains the appropriate JavaScript wrapped in<script>tags.Update: Having said all that, you can include all the style and script information in a single file (say
r_styling.html) and include this at the end of the header of your output HTML usingThe
-hoption is shorthand for--include-in-header.