hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Native way to handle line breaks within cells #100

Closed tripartio closed 5 years ago

tripartio commented 5 years ago

I've been trying to add line breaks within huxtable cells. The only response I've been able to find (on StackOverflow) is to disable escape_contents. I tried this, but it doesn't work for me. Here's a MRE:

ht <- hux(data.frame(a = c("Line 1: 1% Line 2: 2%", "Line 1: 3% Line 2: 4%"), b = c(3, 7))) Suppose I want render this with each line "Line 1" and "Line 2" on a different table line. In other words, I want a line break to separate them. The problem is that I don't have one solution that works for both HTML and Latex.

For HTML, I can insert <br> and then set_escape_contents(ht, TRUE). No big deal.

For Latex, if I insert \newline and then set_escape_contents(ht, TRUE), then my code bugs out because % means everything that follows is commented out. (I'm new to Latex, so I had to discover that the hard way.) If I try to escape the % with \% for Latex, then the code no longer works for HTML (that is, it literally prints out "\%", which is not what I want).

So, huxtable needs a native way to handle newlines that can then be transparently rendered regardless of target (HTML, latex, Word, etc.).

Proposed solution: hux_nl() function A simple (for us users) solution would be to create a function named something like hux_nl() that inserts the appropriate code to render the final output as a line break. The trick is that hux_nl would need to work regardless of whether the cell has escaped contents or not. That is, it needs to be aware of the escaped contents setting of the cell into which it is inserted, and then appropriately insert the correct code, either escaped or not as necessary.

Target audience: all huxtable users This would be useful to all huxtable users, or at least those who:

tripartio commented 5 years ago

Sorry, I forgot to say: thanks for the great huxtable package! I've struggled with a lot of table renderers for R, and what sold me to switch was 1) huxreg; and 2) rendering into both Latex and Word. Thanks for all the work you've put into this!

hughjonesd commented 5 years ago

It's a nice idea, but I think it belongs not with the huxtable package, but with a more general package for writing formatted text that can output HTML, TeX etc... I would suggest writing your own function and using e.g. knitr::is_latex_output() to translate newlines to TeX or HTML as appropriate.