[Tex/LaTex] Right Align Equation Number with hfill in Custom Environment

alignarrayshorizontal alignmenttables

I have a couple of custom environments that I want to be able to use \hfill to right align the last column (similar to what {align} environment does with equation labels}. I attempted to use \hfill but that seems to be ignored within an align*, and tabular environments.

In the case of the tabular, or array like environment (with possibly a large number of columns), I have been adding \multicolumn{5}{r}{} for the case where the last 5 columns are unused to skip over them (and adjusting appropriately depending on the number of columns). This mostly works, but I have to count how many columns have been used, and if I decide to remove a column I have to adjust this. It would be preferable to not have to do that, and instead use \hfill to fill up the rest of the line and add the last column.

So, my question is there an easy way to use \hfill in an environment like tabular, to either get to the lat column, or preferably to the right hand side of the page.

Best Answer

You can use tabular*, with an \extracolsep{\fill} to move the last column to the right margin, as in:

\documentclass[12pt]{article}

\begin{document}

Here's some text. Here's some text. Here's some text. Here's some
text. Here's some text. Here's some text. Here's some text. Here's
some text. Here's some text. Here's some text. Here's some
text. Here's some text.
\begin{center}
  \begin{tabular*}{1.0\linewidth}{@{}ccc@{\extracolsep{\fill}}r@{}}
    One& Two& Three& Number 1\\
    Four& Five& Six& Number 2
  \end{tabular*}
\end{center}

\end{document}

That gives you:enter image description here

(If you don't want it flush with the left margin, you can add an \hskip to that initial @{}.)

Related Question