3

I want to have a few columns that are aligned right. All works fine, except for the last column, which is aligned left?

\NeedsTeXFormat{LaTeX2e}
\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{N}{<{\raggedleft \arraybackslash}b{15ex}}

\begin{document}
\begin{tabular}{l NNN |}
  First & Second & Third & Fourth \\
  \hline
  First    &  1,513 &  1,194 & 1,194 \\
  Second   &  7,831 &  7,783 & 7,763 \\
\end{tabular}
\end{document}

What am I doing wrong? I tried around some time but could not make it work. It is the same if you try to center

1
  • So why the 15ex spacing? tabular* could achieve a similar effect. Commented Jan 13, 2023 at 16:44

2 Answers 2

3

Try with > instead of < so it will act on the following column:

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{N}{>{\raggedleft\arraybackslash}b{15ex}}

\begin{document}
\begin{tabular}{l NNN |}
  First & Second & Third & Fourth \\
  \hline
  First    &  1,513 &  1,194 & 1,194 \\
  Second   &  7,831 &  7,783 & 7,763 \\
\end{tabular}
\end{document}

or if you for some reason prefer it to act on the previous column, place it after your column:

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{N}{b{15ex}<{\raggedleft\arraybackslash}}

\begin{document}
\begin{tabular}{l NNN |}
  First & Second & Third & Fourth \\
  \hline
  First    &  1,513 &  1,194 & 1,194 \\
  Second   &  7,831 &  7,783 & 7,763 \\
\end{tabular}
\end{document}

enter image description here

2
  • OMG thanks a lot, looks like I mixed up what < and > mean. :(
    – Sebastian
    Commented Jan 13, 2023 at 13:04
  • @Sebastian You're welcome! (think of the <> as little arrow heads which point in the direction they act on) Commented Jan 13, 2023 at 13:05
2

With ose of recent array package, which also define w{<align>}{<width>} you can write your table as follows:

\documentclass{article}
\usepackage{array}

\begin{document}
\begin{tabular}{l *{3}{w{r}{15ex}} |}
  First     & Second    & Third     & Fourth    \\
    \hline
  First     &  1,513    &  1,194    & 1,194     \\
  Second    &  7,831    &  7,783    & 7,763     \\
\end{tabular}
\end{document}

enter image description here

1
  • Very nice, thanks a lot, will use that
    – Sebastian
    Commented Jan 18, 2023 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .