4

How can I align first column to the left, other two columns to the right?

\begin{tabular}{p{2.5cm} | p{2cm} | p{2.5cm}}
\hline
$ 2 \, 2 \, I \, I $       & 64   & 0   \\ 
$ 2 \, 2 \, I \, I \, I $  & 358  & 31  \\
$ 3 \, I \, I $            & 21   & 0  \\
$ 3 \, 3 \, I \, I $       & 356  & 46  \\
$ 3 \, 2 \, 2 \, I \, I$   & 7344 & 23046 \\
$ 2 \, 2 \, 3 \, I \, I$   & 1308 & 359 \\
$ 4 \, 4 \, I \, I     $   & 3522 & 7983 \\
\hline 
\end{tabular}

I tried r after p{2cm}, but it vertical line is removed. I want to keep vertical lines, just want to align last two columns to the right.

5

With array package you can add >{\raggedleft} in front of p{2cm} to align contents of column to the right

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{p{2.5cm} |>{\raggedleft} p{2cm} |>{\raggedleft\arraybackslash} p{2.5cm}}
        \hline
        $ 2 \, 2 \, I \, I $       & 64   & 0   \\ 
        $ 2 \, 2 \, I \, I \, I $  & 358  & 31  \\
        $ 3 \, I \, I $            & 21   & 0  \\
        $ 3 \, 3 \, I \, I $       & 356  & 46  \\
        $ 3 \, 2 \, 2 \, I \, I$   & 7344 & 23046 \\
        $ 2 \, 2 \, 3 \, I \, I$   & 1308 & 359 \\
        $ 4 \, 4 \, I \, I     $   & 3522 & 7983 \\
        \hline 
\end{tabular}
\end{document} 
  • somehow I got error. seems clashed with other packages – alim Feb 5 '17 at 6:04
2

You can get better results with siunitx, when numeric tables are concerned.

I also added a trick for simplifying the input, using collcell and a suitable macro for splitting the input at spaces and deliver it separated by \,.

I removed the vertical rules and used booktabs horizontal rules; you can reinsert them if you really want.

\documentclass{article}
\usepackage{amsmath,xparse,siunitx,booktabs,collcell}

\ExplSyntaxOn
\NewDocumentCommand{\alimleft}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
  $\seq_use:Nn \l_tmpa_seq { \, }$
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{
  >{\collectcell\alimleft}l<{\endcollectcell}
  S[table-format=4.0]
  S[table-format=5.0,group-digits=false]
}
\toprule
2 2 I I   &   64 &     0 \\ 
2 2 I I I &  358 &    31 \\
3 I I     &   21 &     0 \\
3 3 I I   &  356 &    46 \\
3 2 2 I I & 7344 & 23046 \\
2 2 3 I I & 1308 &   359 \\
4 4 I I   & 3522 &  7983 \\
\bottomrule 
\end{tabular}

\end{document}

There's no need to use the \alimleft trick (use a more sensible macro name). Also

\begin{tabular}{
  l
  S[table-format=4.0]
  S[table-format=5.0,group-digits=false]
}
\toprule
$ 2 \, 2 \, I \, I $       & 64   & 0   \\ 
$ 2 \, 2 \, I \, I \, I $  & 358  & 31  \\
$ 3 \, I \, I $            & 21   & 0  \\
$ 3 \, 3 \, I \, I $       & 356  & 46  \\
$ 3 \, 2 \, 2 \, I \, I$   & 7344 & 23046 \\
$ 2 \, 2 \, 3 \, I \, I$   & 1308 & 359 \\
$ 4 \, 4 \, I \, I     $   & 3522 & 7983 \\
\bottomrule 
\end{tabular}

would give the same result.

enter image description here

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.