How Can I Prevent a Kable Table From Exceeding the Page Width

I have this simple table:

one = c("one")
two = c ("two")

df = data.frame(one, two)

knitr::kable(escape = FALSE, df, align = "l") |>
  kableExtra::kable_styling(latex_options = "hold_position")

If I add a lot of text to one of the cells, the table will exceed the page width instead of going to a new line. I want it to make it always fit the page width. I know a possible solution is to use the tabularx environment from the tabularx package, but I'm not sure how to do that with kable. I could also set full_width = TRUE in kable_styling, but that will cause vertical borders to dissapear.

How do you want it to make it fit?

Wrapping the text?

Here is a SO solution for wrapping

Or scaling the font?

Here is a SO solution for scaling.

Wrapping the text. Look at this screenshot for an explanation:

https://imgur.com/a/QMmoO05

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.