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


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}
\multicolumn{1}{c}{name} & \multicolumn{1}{c}{number}\\
a & 10\\           % please only make
bb & 193048\\      % edits in these 
ccc & 200\\        % lines so I can reproduce in R

SO posting with more flavours of R in it

Best Answer


    keep_tex: yes
- \usepackage{booktabs}


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")

options(xtable.comment = F)
print(xtable(df,digits=0,align="llr"), include.rownames=F,booktabs=T)