Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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 @{}.)

share|improve this answer
    
This is great. Now I just need to be able to determine how many columns to skip. So what I really want is to be able to add the following lines to the table and get the Number aligned on the right: Seven& Eight \MagicMacro{Number 3}\` and Ten \MagicMacro{Number 4}` which basically need to be an appropriate \multicolumn command to get me to the last column, but without having to manually count the columns. –  Peter Grill Apr 26 '11 at 4:57

You can have a look at the tabbing environment to obtain the same effect. Define a command \tabright that will fill up the rest of the line

\documentclass{article}
\makeatletter
\newlength\tdima
\newcommand\tabright[1]{%
      \setlength\tdima{\linewidth}%
      \addtolength\tdima{\@totalleftmargin}%
      \addtolength\tdima{-\dimen\@curtab}%
      \makebox[\tdima][r]{#1}}
\makeatother
\begin{document}
\begin{tabbing}
\hspace*{2cm}\=\hspace{2cm}\=\hspace{2cm}\=\hspace{2cm}\=\kill

One                      \>\tabright{Number 1}\\
One \>Two                \>\tabright{Number 2}\\
One \>Two \>Three        \>\tabright{Number 3}\\
One \>Two \>Three \>Four \>\tabright{Number 4}\\
\end{tabbing}
\end{document}

The output is then enter image description here

Note that this is a very usefull macro. If you change the line \makebox[\tdima][r]{#1} to \parbox[t]{\tdima}{#1\ifhmode\strut\fi} you have paragraph box that fills up the rest of the tabs to the right margin.

share|improve this answer
    
Yes, this is what I am looking for, but one that works in a tabular environment. –  Peter Grill Apr 26 '11 at 7:29
    
@Peter Grill: I do not think you will get one that automatically counts the number of columns in a tabular. Maybe in a plain TeX table setup. Why the strict requirement for a tabular? –  Danie Els Apr 26 '11 at 7:48
    
Not stuck on tabular, just that it is mostly math that I want to align and have been using many of the features from the {array} package to define column formats. Tabbing seems to be more text oriented. The custom environments that I have set up will be used many times with a large number of align points, and I'd prefer not to have to count how many columns I have used so far, and add adequate number of alignment characters. Thanks for your help though. –  Peter Grill Apr 26 '11 at 8:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.