Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tabularx and xltabular #2138

Merged
merged 13 commits into from
Jun 29, 2022
Merged

Support for tabularx and xltabular #2138

merged 13 commits into from
Jun 29, 2022

Conversation

amarakon
Copy link
Contributor

@amarakon amarakon commented Jun 9, 2022

So far kable only supports tabular and longtable.
But tabularx and xltabular are popular among LaTeX users.
I added support for them: all you have to do is use tabularx = TRUE or xltabular = TRUE.
I also added an option called header, which allows you to specify things to put before the body of the table.

@CLAassistant
Copy link

CLAassistant commented Jun 9, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of three boolean arguments, how about using one character argument like tabular.env? We can keep the longtable argument for backward compatibility.

@amarakon
Copy link
Contributor Author

amarakon commented Jun 17, 2022

Instead of three boolean arguments, how about using one character argument like tabular.env? We can keep the longtable argument for backward compatibility.

tabularx and xltabular require different LaTeX code than tabular and longtable. This is why I have to create distinct options for them. If you take a tabular or longtable table and replace the environment with tabularx or xltabular, it will not work.
However, there are some people that might want to use neither tabular, longtable, tabularx, nor xltabular, which is why I openend issue #2134.
See this comment.

@amarakon
Copy link
Contributor Author

Instead of three boolean arguments, how about using one character argument like tabular.env? We can keep the longtable argument for backward compatibility.

tabularx and xltabular require different LaTeX code than tabular and longtable. This is why I have to create distinct options for them. If you take a tabular or longtable table and replace the environment with tabularx or xltabular, it will not work. However, there are some people that might want to use neither tabular, longtable, tabularx, nor xltabular, which is why I openend issue #2134. See this comment.

Edit: Or you could add tabular.env but make it act differently for different arguments. It should work with longtable, tabularx, and xltabular.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you could add tabular.env but make it act differently for different arguments.

Yes, that's what I meant. The current tabularx argument would be equivalent to using tabular.env == 'tabularx'. In my code suggestions, I was using tabular instead of tabular.env. Are you okay with the suggested changes? (I didn't test them but just typed them directly on Github)

Thanks!

R/table.R Outdated Show resolved Hide resolved
R/table.R Outdated Show resolved Hide resolved
R/table.R Outdated Show resolved Hide resolved
R/table.R Outdated Show resolved Hide resolved
R/table.R Outdated Show resolved Hide resolved
@amarakon
Copy link
Contributor Author

Or you could add tabular.env but make it act differently for different arguments.

Yes, that's what I meant. The current tabularx argument would be equivalent to using tabular.env == 'tabularx'. In my code suggestions, I was using tabular instead of tabular.env. Are you okay with the suggested changes? (I didn't test them but just typed them directly on Github)

Thanks!

I tested your changes and tried to make the following table:

Lorem = c("Ipsum")
Dolor = c("Sit")

knitr::kable(data.frame(Lorem, Dolor), escape = FALSE, booktabs = TRUE, linesep = "\\addlinespace", tabular = "xltabular", caption = "Caption")

But it threw this error:

Error in paste(c(if (!tabular %in% c("longtable", "xltabular")) c(env1,  :
  object 'xltabular' not found
Calls: <Anonymous> ... eval -> <Anonymous> -> do.call -> kable_latex -> paste

R/table.R Outdated Show resolved Hide resolved
amarakon and others added 6 commits June 24, 2022 13:34
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: Yihui Xie <xie@yihui.name>
@amarakon
Copy link
Contributor Author

I committed the suggestions.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a couple of changes to your code and I hope you are okay with them:

  1. I removed the table.length argument. If you need to specify the length for tabularx or xltabular, you may use the existing valign argument (despite the seemingly inappropriate name; technically the two arguments are doing essentially the same thing).
  2. I removed the header argument to keep the number of arguments small. If you need it, you can pass it to midrule.

I'll merge for now. Please feel free to let me know if these changes don't make sense and we can amend this PR later. Thanks!

@yihui yihui merged commit fe562fa into yihui:master Jun 29, 2022
@amarakon
Copy link
Contributor Author

Thanks, your changes are perfect.

  1. The valign argument is satisfactory so there is no need for table.length.
  2. Same goes for the midrule argument.

@cderv cderv linked an issue Jul 4, 2022 that may be closed by this pull request
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants