[Tex/LaTex] Align ordinal numbers

align

\documentclass[10pt]{article}

\usepackage{ulem,amsmath}

\newcommand{\unline}[1]{\rule[0pt]{#1}{0.6pt}}

\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}

\begin{document}

\begin{tabular}{llll}
  1$^{\text{st}}$&  first  &  17$^{\text{th}}$ &  seventeenth  \\ 
  2$^{\text{nd}}$&  \unline{2cm} &  18$^{\text{th}}$ &  \unline{2cm} \\ 
  3$^{\text{rd}}$&  \unline{2cm} & 19$^{\text{th}}$  &  \unline{2cm} \\ 
  4$^{\text{th}}$&   \unline{2cm}&  20$^{\text{th}}$ &  \unline{2cm} \\ 
  5$^{\text{th}}$&   \unline{2cm}&  21$^{\text{st}}$ &  \unline{2cm} \\ 
  6$^{\text{th}}$&  \unline{2cm} &  22$^{\text{nd}}$ &  \unline{2cm} \\ 
  7$^{\text{th}}$&   seventh&   23$^{\text{rd}}$&  \unline{2cm}\\ 
  8$^{\text{th}}$&    \unline{2cm}&  24$^{\text{th}}$ &  twenty$-$fourth \\ 
  9$^{\text{th}}$&   \unline{2cm}&  25$^{\text{th}}$ &  twenty$-$fifth \\ 
  10$^{\text{th}}$&   \unline{2cm}&  26$^{\text{th}}$ &  twenty$-$sixth \\ 
  11$^{\text{th}}$&   \unline{2cm}&  27$^{\text{th}}$ &  \unline{2cm} \\ 
  12$^{\text{th}}$&  \unline{2cm} &  28$^{\text{th}}$ &  twenty$-$eighth \\ 
  13$^{\text{th}}$&   \unline{2cm}&  29$^{\text{th}}$ &  \unline{2cm} \\ 
  14$^{\text{th}}$&   fourteenth&  30$^{\text{th}}$ &  \unline{2cm} \\ 
  15$^{\text{th}}$&  \unline{2cm} &  31$^{\text{st}}$ &  \unline{2cm} \\ 
  16$^{\text{th}}$&   sixteenth&  &  \\ 
\end{tabular}

\end{document}

How can I align the numbers, st, nd, rd and th? As you can see they aren't aligned.

enter image description here

I'm trying to recreate this:

enter image description here

(For some reason the image is flipped.)

Best Answer

Your font choice AlegreyaSans uses lining figures with variable widths by default. That means the glyph width of digits are not constant but varies according the shape of the digit glyph.

The package defines the font \AlegreyaSansLTF for lining figures in tables with constant width for better vertical alignments in tables.

The following example uses \textsuperscript for raising the suffix letters. Also, the example uses package array to smuggle in a macro at the start of the cell for formatting the ordinal number.

\documentclass[10pt]{article}

\usepackage{array}
\usepackage{ulem}

\newcommand{\unline}[1]{\rule[0pt]{#1}{0.6pt}}

\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}

\begin{document}

\newcommand*{\myordnum}[5]{%
  #1% \ignorespaces, automatically added at the begin of a tabular cell
  \AlegreyaSansTLF
  #2#3%
  \textsuperscript{#4#5}%
}
\begin{tabular}{>{\myordnum}ll>{\myordnum}ll}
  {}1st & first        &  17th &  seventeenth   \\
  {}2nd & \unline{2cm} &  18th &  \unline{2cm}  \\
  {}3rd & \unline{2cm} &  19th &  \unline{2cm}  \\
  {}4th & \unline{2cm} &  20th &  \unline{2cm}  \\
  {}5th & \unline{2cm} &  21st &  \unline{2cm}  \\
  {}6th & \unline{2cm} &  22nd &  \unline{2cm}  \\
  {}7th & seventh      &  23rd &  \unline{2cm}  \\
  {}8th & \unline{2cm} &  24th &  twenty-fourth \\
  {}9th & \unline{2cm} &  25th &  twenty-fifth  \\
  10th  & \unline{2cm} &  26th &  twenty-sixth  \\
  11th  & \unline{2cm} &  27th &  \unline{2cm}  \\
  12th  & \unline{2cm} &  28th &  twenty-eighth \\
  13th  & \unline{2cm} &  29th &  \unline{2cm}  \\
  14th  & fourteenth   &  30th &  \unline{2cm}  \\
  15th  & \unline{2cm} &  31st &  \unline{2cm}  \\
  16th  & sixteenth    \\
\end{tabular}

\end{document}

Result

Variant with right aligned digits:

\newcommand*{\myordnum}[5]{%
  #1% \ignorespaces, automatically added at the begin of a tabular cell
  \leavevmode
  \AlegreyaSansTLF
  \ifx\\#2\\% first digit is empty?
    \hphantom{0}%
  \fi
  #2#3%
  \textsuperscript{#4#5}%
}

Result

Related Question