# Align ordinal numbers

\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.

I'm trying to recreate this:

(For some reason the image is flipped.)

• Well, they are different widths. How do you want to align them? Right now, they are aligned left. If you use r rather than l to specify the column, they will be aligned right. If you use c, they will be aligned centre. – cfr Aug 5 '15 at 2:54

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}


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}%
}


You are doing a lot of manual work, when there's no need for that. Let LaTeX do the work for you: using some counters, the fmtcount package and the >{...} syntax from the array package, the table can be almost completely filled in automatically for you: the only manual adjustment is to provide the ordinal as strings (and this can also be highly automated). Using the \AlegraSansTLF font for the columns with ordinal numbers gives an alignment similar to the one in the image:

\documentclass[10pt]{article}
\usepackage{ulem,amsmath}
\usepackage{array}
\usepackage{fmtcount}
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}

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

\newcounter{mycounteri}
\newcounter{mycounterii}
\setcounter{mycounterii}{16}

\newcommand\FormatNumber[1]{%
\multicolumn{1}{l}{\ordinalstring{#1}}%
}

\begin{document}

\begin{tabular}{
>{\stepcounter{mycounteri}\AlegreyaSansTLF\ordinal{mycounteri}}l
>{\unline{2cm}}l
>{\stepcounter{mycounterii}\AlegreyaSansTLF\ordinal{mycounterii}}l
>{\unline{2cm}}l
}
& \FormatNumber{mycounteri} & & \FormatNumber{mycounterii}  \\
& & &   \\
& & &   \\
& & &   \\
& & &   \\
& & &   \\
& \FormatNumber{mycounteri} & &  \\
& & & \FormatNumber{mycounterii} \\
& & & \FormatNumber{mycounterii} \\
& & & \FormatNumber{mycounterii} \\
& & &  \\
& & & \FormatNumber{mycounterii} \\
& & &  \\
& \FormatNumber{mycounteri} & &   \\
& & &  \\
& \FormatNumber{mycounteri} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}  \\
\end{tabular}

\end{document}


The result:

• this is still using a font with proportionally spaced numbers, rather than monospaced/tabular numbers. unless tabular numbers are used, the situation is hopeless. – barbara beeton Aug 5 '15 at 13:51
• @barbarabeeton True. I've updated with \AlegraSansTLF and the result is now better, I think. – Gonzalo Medina Aug 5 '15 at 14:15

It mostly depends on what you mean by “alignment”. Here are two similar solutions: in the first example the digits have proportional width, so the digit 1 is less wide than the others; in the second solution “tabular” digits are chosen.

Instead of $-$ you should use --, for an en-dash (assuming you really want an en-dash).

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}

\usepackage{ulem,amsmath,array}

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

\begin{document}

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

\bigskip

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

\end{document}


As an exercise, here's a simpler way to input the second form:

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}

\usepackage{ulem,amsmath,array,collcell}

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

\newcount\ordinalcnt
\newcommand{\alignordinal}[1]{\doalignordinal#1\doalignordinal}
\def\doalignordinal{\afterassignment\typesetordinal\ordinalcnt=}
\def\typesetordinal#1\doalignordinal{%
\begingroup
\AlegreyaSansTLF
\ifnum\ordinalcnt<10 \phantom{0}\fi
\the\ordinalcnt
\endgroup
\textsuperscript{#1}%
}
\newcolumntype{O}{>{\collectcell\alignordinal}l<{\endcollectcell}}

\begin{document}

\begin{tabular}{Ol Ol}
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}


The misalignment you are seeing is, I think simply a function of the font you are using. TeX aligns the boxes containing the glyphs. It trusts the font to tell it how large those boxes need to be. Compare this example (obviously the mix of sans and serif is dire):

\documentclass[10pt]{article}
\usepackage{ulem,amsmath}
\newcommand{\unline}[1]{\rule[0pt]{#1}{0.6pt}}
\usepackage[T1]{fontenc}
\usepackage{cfr-lm}
\begin{document}

\sffamily\tlstyle%
\begin{tabular}{|@{}ll|@{}ll}
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}


The vertical rules are just to demonstrate the alignment.

Incidentally, if the ordinals should be text, use \textsuperscript{}. If not, the numbers should be included in maths mode.

• Yes, I've tried that. I've uploaded an image in my question, please take a look and compare the column of the second image with the column of your result. (I've used l but numbers don't look quite aligned as I expect.) – Alejandro Aug 5 '15 at 3:04
• @Oshnaj So you want them left aligned? – cfr Aug 5 '15 at 3:05
• Yes, I've used l. Can you compare the column of the second image I uploaded? – Alejandro Aug 5 '15 at 3:07
• @Oshnaj See edit. It is just the font choice. If the font lies about how big the boxes should be, things don't work out because everything, TeX thinks, is a box. Nothing but boxes. – cfr Aug 5 '15 at 3:17