how to change the width of TOC in rmarkdown?

how to change the width of TOC so all titles in one line with no break.

---
title: "Data Visualization"
output:
  html_document:
    number_sections: false
    toc: true
    toc_float: true
    toc_depth: 5
    theme: cosmo
    highlight: zenburn
date: "`r format(Sys.time(), '%d %B %Y')`"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)

Including Plots

You can also embed plots, for example:

plot(pressure)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

a very looonnnnnnnnnnnnnnnnnnnnnnnnnnnnng header

Hi Mohamed!

So, there might be some other ways, but a straight-forward solution is to customize the CSS for the TOC.
Taking your example, I added a css chunk with the rules that would avoid text-wrapping, make the TOC width fit the TOC content and avoid overlapping with the main content.

---
title: "Data Visualization"
output:
  html_document:
    number_sections: false
    toc: true
    toc_float: true
    toc_depth: 5
    theme: cosmo
    highlight: zenburn
date: "`r format(Sys.time(), '%d %B %Y')`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
    ```
# R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

``` {r}
summary(cars)
    ```
# Including Plots
You can also embed plots, for example:

```{r, echo=FALSE}
plot(pressure)
    ```
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# a very looonnnnnnnnnnnnnnnnnnnnnnnnnnnnng header

```{css, echo=FALSE}
  #TOC {
    max-width: fit-content;
    white-space: nowrap;
  }
  
  div:has(> #TOC) {
    display: flex;
    flex-direction: row-reverse;
}
    ```
Some text here.

Try knitting this code and open the knitted HTML it in a browser tab (with full screen width if possible).

Notice the css chunk. This is where the visible change happens.
The first part is telling the HTML element with id TOC (which unsurprisingly corresponds to the TOC you want to change) to avoid text-wrapping and to take the max width of its content.

The second part is just to avoid overlapping of the TOC with the main content (as by default this is set via media queries). This part is not ideal, but you may further customize it following the actual layout of your content.

Hope this helps :slight_smile:

Cheers!


This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.
Appsilon_GIFsmall_whitebg

I think the problem is the TOC contanier because if the header is long, you can scroll horizontal.
i want to show the header fully without scrolling right and left.

i tried this

div.tocify {
width: 100%;
}

if you have better solution, please share