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

Horizontal position adjustment ignores the right margin in PDF #122

Closed
Brunox13 opened this issue Dec 14, 2019 · 8 comments
Closed

Horizontal position adjustment ignores the right margin in PDF #122

Brunox13 opened this issue Dec 14, 2019 · 8 comments

Comments

@Brunox13
Copy link

This issue arose in the following StackOverflow question: How can you center a huxtable?

As described in the selected answer for the question above, there seems to be an issue with how huxtable proccesses (or doesn't) the right page margin. Below, I am showing different options for alignment ("left", "right", and "center", respectively); besides the R code depicted in the images, each of the following markdown files used to create these PDFs start with the same code (minus the "()"):

---
title: "Example"
output: pdf_document
---

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

Left Aligned

left-aligned

Right Aligned

right-aligned

Centered

centered

While the left-aligned table is positioned as expected, the right-aligned one is touching the edge of the page. In the centered table, you can see that the table is essentially centered if the right margin is ignored. This makes me think that this issue is caused by huxtable somehow ignoring the right margin.

Note that setting margins using geometry: in the LaTeX header does not resolve this issue either.

@hughjonesd
Copy link
Owner

Hi, thanks for this issue. My feeling is that this isn't correct. Take a look at latex.R in the source. You'll see that "center" just uses the latex \centering command. The problem is that the latex version uses tabularx which expects a given width. The default width is 0.5\textwidth. If that is wrong i.e. if your table overspills, then the centering will be wrong too. See ?position and ?"huxtable-FAQ" for the details.

Really, the whole latex code needs a rewrite from a proper expert. But beware: it is a hairy project to do a truly flexible table in TeX.

@Brunox13
Copy link
Author

Both ?position and ?"huxtable-FAQ" (and, similarly, you in the previous issue and in your StackOverflow comment) suggest to

Try adjusting width(ht).

However, setting that to pretty much any value does not help in case of this table. So are you suggesting that that's not a problem of huxtable but rather of latex itself?

@hughjonesd
Copy link
Owner

Hmm, if you're right that is interesting. So then we have two problems :-).

I wouldn't say it's a problem of either hux or TeX on its own. It's how they interact. tabularx is probably not the optimal table package because it demands that you set the width right - and there's no way to do that automatically. tabu might be a better option, but I'm not sure it's supported. A more radical approach would be to drop the ability to do all the things huxtable aims to do (arbitrary per-cell borders, colours, colspan etc.) and just focus on producing simple TeX code for a few key use cases. But that would be a new package....

@hughjonesd
Copy link
Owner

Could you actually put the full code to reproduce here? Thanks.

@Brunox13
Copy link
Author

This is the full code that I have saved in an .Rmd file:

---
title: "Example"
output: pdf_document
---

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

```{r}
iris_lm <- lm(Sepal.Width ~ ., data = iris)

ht <- huxtable::huxreg(iris_lm, iris_lm, iris_lm, iris_lm)

huxtable::position(ht) <- "center"

huxtable::width(ht) <- 0.7 # This can be varied

ht
```

Then I just knit to PDF.

@hughjonesd
Copy link
Owner

Could you try github master? I've just committed some changes to LaTeX. In particular, try with width set and unset; and wrap TRUE and FALSE. There may be new issues, in which case feel free to open them....

@hughjonesd
Copy link
Owner

hughjonesd commented May 2, 2020

The particular "right margin" issue mentioned in the title now seems to occur only if width is (wrongly) set. This might happen a lot though. One solution may be to set wrap = TRUE by default. Or, we could allow wrap = NA and then set it to be TRUE or FALSE depending on if width was set. But this would be disruptive and might affect how HTML works.

@hughjonesd
Copy link
Owner

Hopefully closed by recent changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants