2

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!

5
  • 3
    a "normal" \hbox (i.e. one without to) is always as wide as the content you put inside. Therefore, you have to give it a fixed width.
    – Lupino
    Nov 30, 2018 at 12:56
  • If you use \hbox to 30mm {\hss stuff\hss} then stuff will be centered in an \hbox of width 30mm. Similarly, \hbox to 30mm {\hss stuff} is right justified and \hbox to 30mm {stuff\hss} is left justified. I am not sure that it makes sense to center, left or right justify the contents of a box if you don't know the width by some mechanism since the width of \hbox{stuff} is just enough to accommodate stuff.
    – user30471
    Nov 30, 2018 at 13:04
  • @Andrew ... I understand what you are saying and I agree in the case in which I only have one row of hbox's, but the code I have can be used to construct a table with many rows of hbox's. So "stuff" in row1 may be much wider than "stuff" in row3, for example. The result is that each column of hbox's takes the largest width of any hbox in that column and therefore alignment makes sense.
    – NateA
    Nov 30, 2018 at 13:49
  • Can you provide a complete (yet short) working code, beginning with \documentclass, to show the issue? Nov 30, 2018 at 14:23
  • @StevenB.Segletes ... I've added a working example, thanks!
    – NateA
    Nov 30, 2018 at 19:48

2 Answers 2

1

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

I've accepted the answer above because it technically answered the question about alignment within an hbox, although it didn't really help me accomplish what I wanted to do.

For anyone interested, I did find a rather ugly work around for my problem. Instead of using \valign to build my transposed table from the arrays, I used minipages. This, of course, only worked because I knew the number of columns that I wanted and had a relatively good guess as to the approximate widths I wanted those columns to be. Here is the code I ended up with:

\begin{minipage}{.5\textwidth}
\centering
\hrule height 1.3pt
\strut ~\\
\strut Item\\
\hrule height 1.3pt
\strut \Sexpr{paste(items, collapse="\\\\strut \\\\\\\\ \\\\hrule ")}\strut
\hrule height 1.3pt
\end{minipage}%
\begin{minipage}{.2\textwidth}
\centering
\hrule height 1.3pt
\strut\hfill Price at Original\\
\strut\hfill Location (\$)\\
\hrule height 1.3pt
\strut \hfill \Sexpr{paste(site1price, collapse="\\\\strut \\\\\\\\ \\\\hrule \\\\hfill")}\strut
\hrule height 1.3pt
\end{minipage}%
\begin{minipage}{.2\textwidth}
\centering
\hrule height 1.3pt
\strut\hfill Price at New\\
\strut\hfill Location (\$)\\
\hrule height 1.3pt
\strut \hfill \Sexpr{paste(site2price, collapse="\\\\strut \\\\\\\\ \\\\hrule \\\\hfill")}\strut
\hrule height 1.3pt
\end{minipage}

This produces something like this: enter image description here

1
  • Maybe is that I do not understand well... buto this to make a simple table with R outputs? Without the R code is hard to say, but I suspect that it will be a lot easier (1) In R construct the table as a simple matrix or data frame and the (2) print with xtable packable, where you could have control over the type of columns (and you could also use booktabs rules, with a more elegant look).
    – Fran
    Dec 1, 2018 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .