(PDF) How to Insert a Line Break in a Table?

I can have a simple table like this:

1 | 2 | 3
:- |:-: | -:
4 | 5 | 6
7 | 8 | 9

1 2 3
4 5 6
7 8 9

Let's say I want another number directly below 6 but without it being counted as a new line (a line break). If my output was html_document, I could simply add <br> after 6. But since my output is pdf_document, that doesn't work. What is the equivalent for pdf_document?

If you still plan to write Markdown table for that, you need to use a format that allows multiline (block element).

Currently, you are using pipe table (see Pandoc - Pandoc User’s Guide) which does not allow that. Other formats like Grid table (Pandoc - Pandoc User’s Guide) or multiline table (Pandoc - Pandoc User’s Guide) should help with that.

Using RStudio IDE visual editor should help you write the correct markdown syntax from the visual edition of the table. Otherwise, you can use a R Package to create the table which should write the correct syntax for you

Hope it helps

I want all my tables to be labeled and to be referencable. So I use this R code to name the table so it will show as "Table 1: Example Table" and I can reference it with @ref(tab:example) (If I'm using bookdown):

```{r, results='asis'}
cat('Table: (\#tab:example)Example Table

| example | table
| :- | -:
| text | more text
')
```

Will produce this: https://imgur.com/QI9GPIe

If I write tables that way, only pipe and multiline tables work, grid tables don't work. If I write the gridline tables in plain text, they will render properly. But if I do it the way I like to do it, in an R code block, it won't render properly. This is unfortunate because multiline tables are even more annoying to write than grid tables. I want to know how to write a grid table and also make it show "Table 1: Example Table" and make it possible to reference it.

Do you need to write table in markdown directly ?

You could use knitr::kable() to write the markdown for you from a table in R. It has a caption argument also. There are other R package for this too, that should also work with bookdown. See chapter in the cookbook linked above.

Also for writing markdown directly in R markdown, why do you need to cat() in a R chunk if the content is just markdown ? Can't you write just Markdown directly without the cat() ?

I am trying to understand the use case. From my current point of view, you encounter problem because of your specific workflow, but I may be missing something.

Also, did you try the visual editor to write Markdown table ?

I found an efficient way to make tables using kable and kableExtra. I can add line breaks too. I only have one issue with it, if I add a line break, the row color ends where the line break is. It's difficult to explain, so here is an image. As you can see, the color of the row should go to the very end, but it doesn't because of the line break. Do you know how I can fix this? Here is the code for this table:

library(knitr)
library(kableExtra)

name <- linebreak(c("Norman", "Gilda", "Wilma", "Wompa"))
animal <- linebreak(c("Hamster", "Flamingo", "Duck", "Wompa"))
food <- linebreak(c("Crumbs\nOther", "Pink lemonade", "Bread", "Tears of his enemies"))

animals <- data.frame(name, animal, food)
kable(animals, "pipe", caption="Example Table", col.name=linebreak(c("Name", "Animal", "Food")))

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.