1

I'm trying to create a table with mailing adresses to print so what I need is to match my table dimensions to the pre-cut sticker paper. So far I've managed to control column width with p{\dimexpr 0.25\linewidth-2\tabcolsep} (I tried p{5cm} which is the exact measure of the sticker but it goes too wide off the page for some reason I just don't understand). However, I don't know how to properly adjust the vertical height of my table.

What I'm doing via Rmarkdown is breaking my data matrix in groups of 4 in order to create a one-row table. Then the next four are in a new table below the previously created one. (This is what I could came up with if someone knows a better way I would really appreciate it)

My problem is all tables adjust to the full size of the page and I want them flushed to the top of the page for printing.

Can someone help me??

This is the latex code produced by R:

\begin{table}[t]
\begin{flushleft}
\begingroup\footnotesize
\begin{tabular}{p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}}
  Name & Name & Name & Name \\ 
  Address & Address & Address & Address \\ 
  town & town & town & town \\ 
  city & city & city & city \\ 
  \end{tabular}
\endgroup
\end{flushleft}
\end{table}
\begin{table}[t]
\begin{flushleft}
\begingroup\footnotesize
\begin{tabular}{p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}p{\dimexpr 0.25\linewidth-2\tabcolsep}}
  Name & Name & Name & Name \\ 
  Address & Address & Address & Address \\ 
  town & town & town & town \\ 
  city & city & city & city \\ 
  \end{tabular}
\endgroup
\end{flushleft}
\end{table}

This is my curent R code

data <- structure(c("Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city"), dim = c(4L, 8L))

rows <- ncol(data)%/%4

for (i in 1:rows)
{
  print(xtable::xtable(data[,(4*(i-1)+1):min(4*i,ncol(data))],
        align=rep("p{\\dimexpr 0.25\\linewidth-2\\tabcolsep}",5)),
        table.placement = "t",
        latex.environments = "flushleft",
        hline.after=NULL,
        include.rownames=FALSE,
        include.colnames=FALSE,
        comment=FALSE,
        sanitize.text.function = identity,
        size=c("\\footnotesize"))
}
1
  • 2
    If you use p{5cm}, you would have to put @{} before, after and between the columns. Like \begin{tabular}{@{}p{5cm}@{}p{5cm}@{}p{5cm}@{}p{5cm}@{}}, or just easier, set \tabcolsep=0pt. Oct 5, 2022 at 7:46

1 Answer 1

2

Just remove the option table.placement = "t" to allow the default [ht] float options,or use more flexible options, or better, change it to table.caption = FALSE to not use floats at all.

p{5cm}probably was too much because you must take into account\tacolsep, as you did using .25\linewidth`.

BTW, I will do the data base with a data frame with four columns/variable (name, address,town,city). To print as columns will be just t(df[1:4,]) for the first table

mwe

---
output:
  pdf_document: 
    keep_tex: yes
---

```{r,results='asis',echo=F}

data <- structure(c("Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city", "Name", "Address", "town", "city", "Name", "Address", 
"town", "city"), dim = c(4L, 8L))

rows <- ncol(data)%/%4

for (i in 1:rows)
{
  print(xtable::xtable(data[,(4*(i-1)+1):min(4*i,ncol(data))],
        align=rep("p{\\dimexpr 0.25\\linewidth-2\\tabcolsep}",5)),
        table.caption = FALSE,
        latex.environments = "flushleft",
        hline.after=NULL,
        include.rownames=FALSE,
        include.colnames=FALSE,
        comment=FALSE,
        sanitize.text.function = identity,
        size=c("\\footnotesize"))
}

```
5
  • 1
    Thank you so much! This solved my tables being spread throughout the page. But I still have the problem of needing to fix the table height...
    – wernor
    Oct 5, 2022 at 7:57
  • @wernor Without an example where that "height problem" is obvious, or a clear explanation of what's wrong sometimes with the height, I don't know how I could help.
    – Fran
    Oct 5, 2022 at 8:08
  • I need to size the table to the specific measurements of my sticker paper for proper printing. I got the width figured out but the height not so much. I need the height of the table to always be 4cm so the stickers always fit on their proper spaces. I haven't been able to find much on controling the height of the table or it was too advanced for me.
    – wernor
    Oct 5, 2022 at 8:17
  • 1
    With a fixed width, the height depend mainly of the contents of the table and size font. Just use and appropriate smaller font. Another option is use a \resizebox (there a lot of examples here) but note that with this approach you will obtain different font sizes for each label.
    – Fran
    Oct 5, 2022 at 8:31
  • Great, thank you so much for your time!
    – wernor
    Oct 5, 2022 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.