[Tex/LaTex] How to center and flush right cells in a column

markdownpdftexr

I am trying to create a table with a numeric column number which I wish to center and flushright content as follows:

enter image description here

Using RMarkdown, I am only able to wrap LaTeX code around/inline with cell content in R using the paste0() as follows, and I am not able to modify the LaTeX tabular environment:

i.e. table$number <- paste0("\\begin{centering}", table$number, "\\end{centering})

So possible ways to approach this is to use eqmakebox or multicol or varwidth. But my surficial understanding of these commands have not made possible of what I want.

The table in LaTeX as a reproducible example:

\begin{tabu} to \linewidth {>{\centering}X>{\raggedleft}X}
\hline
\multicolumn{1}{c}{name} & \multicolumn{1}{c}{number}\\
\hline
a & 10\\           % please only make
\hline
bb & 193048\\      % edits in these 
\hline
ccc & 200\\        % lines so I can reproduce in R
\hline
\end{tabu}

SO posting with more flavours of R in it

Best Answer

mwe

---
output:
  pdf_document: 
    keep_tex: yes
header-includes:
- \usepackage{booktabs}
---

\tabcolsep3em

```{r,echo=F}
library(knitr)
library(kableExtra)
df <- data.frame(name=c("a","bb","ccc"), number=c(10,193048,200))
kable(df,"latex", align="lr", booktabs = T, linesep = "") %>%
  kable_styling() %>%
  column_spec(1, width = "3em") %>%
  column_spec(2, width = "3em")
```

```{r,echo=F,results='asis'}
library(xtable)
options(xtable.comment = F)
print(xtable(df,digits=0,align="llr"), include.rownames=F,booktabs=T)
```