[Tex/LaTex] Alignment with a LaTeX hbox

tables

Can someone please explain if it is possible to obtain centered text or right justified text within a TeX hbox? I am attempting to utilize some code I found here in order to transpose a LaTeX table.

Here is my code for reference. Note that I'm using the booktabs and array packages.

\def\Midrule{\midrule[\heavyrulewidth]}
\newcount\rowc

\makeatletter
\def\ttabular{%
~
\hbox\bgroup
\let\\\cr
\def\rulea{\ifnum\rowc=\@ne \hrule height 1.3pt \fi}
\def\ruleb{
\ifnum\rowc=1\hrule height 0pt \else%
\ifnum\rowc=2\hrule height 1.3pt \else%
\ifnum\rowc=6\hrule height \heavyrulewidth
   \else \hrule height \lightrulewidth\fi\fi\fi}
\valign\bgroup
\global\rowc\@ne
\rulea
\hbox to 10cm{\strut~~\hfill##\hfill}%
\ruleb
&&%
\global\advance\rowc\@ne
\hbox to 10cm{\strut~~\hfill##\hfill}%
\ruleb
\cr}
\def\endttabular{%
\crcr\egroup\egroup}

I don't like the effect of "hbox to 1cm", though, as it hardcodes the width of the columns of my table. I've tried replacing with "hbox spread 1cm" or just "hbox", but in both cases the text in the columns is left justified. Is there any way to obtain center or right justified text without fixed-width columns?

Thanks in advance!

EDIT:

OK, here is a minimal working example:

\documentclass{article}

\usepackage{booktabs,array}

\def\Midrule{\midrule[\heavyrulewidth]}
\newcount\rowc

\makeatletter
\def\ttabular{%
~
\hbox\bgroup
\let\\\cr
\def\rulea{\ifnum\rowc=\@ne \hrule height 1.3pt \fi}
\def\ruleb{
\ifnum\rowc=1\hrule height 0pt \else%
\ifnum\rowc=2\hrule height 1.3pt \else%
\ifnum\rowc=6\hrule height \heavyrulewidth
   \else \hrule height \lightrulewidth\fi\fi\fi}
\valign\bgroup
\global\rowc\@ne
\rulea
\hbox{\strut~~\hfill##\hfill}%
\ruleb
&&%
\global\advance\rowc\@ne
\hbox{\strut~~\hfill##\hfill}%
\ruleb
\cr}
\def\endttabular{%
\crcr\egroup\egroup}

\begin{document}

\begin{ttabular}
                    &   Item                &   Very very very very very long item  & Short item\\
Price at Original   &   Location (\$)       &   \$3.95                                  & \$9.95\\
Price at New        &   Location (\$)       &   \$3.99                                  & \$10.63 \\
\end{ttabular}

\end{document}

This produces the following:

enter image description here

I believe the comments are correct and that is not a problem within my hbox's but rather that the hbox's themselves are sized appropriately and then being left aligned within each column. However, I'd still like to know how to generate a transposed table and have the columns of the final result be either centered or right-justified.

For anyone interested, the reason I want to do this is I'm using R markdown to generate the table. The entries of each column are given by the elements of an array, and the array's size may change from one generation of the table to another. So my R markdown code looks like this:

\begin{ttabular}
                    &   Item                &   \Sexpr{paste(items, collapse=" & ")} \\
Price at Original   &   Location (\$)       &   \Sexpr{paste(site1price, collapse=" & ")} \\
Price at New        &   Location (\$)       &   \Sexpr{paste(site2price, collapse=" & ")} \\
\end{ttabular}

In the above, item, site1price, and site2price are R arrays.

Any help is appreciated!

Best Answer

Center, left and right aligned "xxx":

\documentclass{article}
\begin{document}
\hbox to 5cm{\hfil xxx \hfil}\par
\hbox to 5cm{\hfil xxx}\par
\hbox to 5cm{xxx \hfil}\par
\end{document}

But better:

\documentclass{article}
\begin{document}
\makebox[5cm][c]{x x x}\par
\makebox[5cm][r]{x x x}\par
\makebox[5cm][l]{x x x}\par
\makebox[5cm][s]{x x x}\par
\end{document}
Related Question