# Aligning multiple objects in individual tables cells

Trying to research this question has been difficult. Not sure what nomenclature to use when searching the web, so here is an example: would like to achieve this:

Note: the objects descriptions can span multiple lines. Some objects descriptions may only be on one line. The object on the left needs to be right justified and bold. The objects corresponding description on the right needs to be left justified. There can be rows that contain only one object and corresponding description, but some may have up to eight. Some rows may have only one object on the left and a large paragraph on the right. In that case, the object on the left should be at the top "line"(?) and again right justified and bold.

This post seems promising. A number of posts go into how to top/center/bottom align in a cell, and this one seemed promising also, however they mostly all seem to be about top/center/bottom alighnment and not how to align the object in the left cell with the one in the right (on an individual line basis). The first link gets somewhat there except it is not clear how to achieve the following:

• line wrap
• objects on the left should be bold
• objects on the left are right justified

It seems that this method could become complicated (the list to be inserted is quite large, and it needs to be in the format presented). Is there a simpler way to achieve the picture above (knowing that there will be about 30-40 rows and two columns)?

Here is the example code in tex: More research is needed, as the meaning of 20cm after \pbox is not totally clear.

\begin{tabular}{|c|c|}
\hline
\pbox{20cm}{Object 1 \\ Object 2 \\ Object 3 \\ Object 4 \\ Object 5} & \pbox{20cm}{This is object 1s description. It is on the same line, but it may be longer than one line in the cell. \\ This is object 2s description, same line as object 2. \\ Object 3 description \\ Object 4 description may be longer than one line also, but object 5 will be aligned. \\ Object 5 description} \\
\hline
\pbox{20cm}{Object 1 \\ Object 2 \\ Object 3} & \pbox{20cm}{This is a different group of objects...object 1 description \\ This is a different group of objects...object 2 description, and this one might be longer than one line. \\ This is a different group of objects...object 3 description} \\
\hline
\end{tabular}


Note the above tex does not actually work correctly. The right hand side is chopped off. It looks like:

Questions: Can this reasonably be done in TeX? Is there a simpler way to do this (in Tex)? Can the bullet point items above be remedied?

• – Marijn Dec 29 '16 at 15:03
• Tested on my code, it looses all alignment with the object1,2,3,4,5 lines. Further reading in the wikibook linked to above notes that parbox may be a better alternative. It seems manual, however. – number9 Dec 29 '16 at 15:22

Here are two solution using a tabularx and a tabular, respectively.

tabularx: The r column specifier makes the column right- and top-justified. The X specifier results in a column with a width extending as much as the first parameter of tabularx (\textwidth in this example) permits.

tabular: The p column specifier takes as argument the width of the column, 9cm in this example.

To have the first column consistently in boldface, you can use > to inject formatting commands at the beginning of each cell (\bfseries in this example). For this to work you have to load the array or tabularx package.

Finally, I recommend not to use vertical lines. In my opinion best results are obtained with the the booktabs package (providing \toprule, \midrule and \bottomrule as well as improving the interline spacing).

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{>{\bfseries}rX}
\toprule
Object 1 & This is object 1s description. It is on the same
line, but it may be longer than one line in the cell. \\
Object 2 & This is object 2s description, same line as object 2. \\
Object 3 & Object 3 description \\
Object 4 & Object 4 description may be longer than one line also,
but object 5 will be aligned.\\
Object 5 & Object 5 description \\
\midrule
Object 1 & This is a different group of objects...object 1
description \\
Object 2 & This is a different group of objects...object 2
description, and this one might be longer than
one line.\\
Object 3 & This is a different group of objects...object 3 description \\
\bottomrule
\end{tabularx}
\bigskip

\noindent
\begin{tabular}{>{\bfseries}rp{9cm}}
\toprule
Object 1 & This is object 1s description. It is on the same
line, but it may be longer than one line in the cell. \\
Object 2 & This is object 2s description, same line as object 2. \\
Object 3 & Object 3 description \\
Object 4 & Object 4 description may be longer than one line also,
but object 5 will be aligned.\\
Object 5 & Object 5 description \\
\midrule
Object 1 & This is a different group of objects...object 1
description \\
Object 2 & This is a different group of objects...object 2
description, and this one might be longer than
one line.\\
Object 3 & This is a different group of objects...object 3 description \\
\bottomrule
\end{tabular}
\end{document}

• Gernot, you are using booktabs. :-) I should have mentioned that too. Thumbs up for that nice extra feature. – Jan Dec 29 '16 at 15:31
• @Jan We have to preach the right way at all times, to save their souls ;-) – gernot Dec 29 '16 at 15:40
• You are so right. Ten extra points for this lad for aesthetical reasons! – Jan Dec 29 '16 at 15:41

This seems to be relative simple to arrange.

First, I'd advise you to use a p{width} column. That column-type will be able, to wrap the lines. Unfortunately, the p, b and m-columns will justify the text on both the left and right side.

Second: if you want to fit the width of the whole table onto your page neatly, than you should consider using the tabularx-package. This is able to stretch a column to use the maximum given width. To do so, it introduces a new column type, the X(capital X) column. It is identical to a p-column, in that it uses a stretchable width dimension.

Third: (as long, as I understood your question correct!) you want to have "Object1" to be the first entry of the first line, and "Object2" to be the beginning of the second line and so on, it would be much easier, to enter your table one row after another, instead the way you did (first to insert all entries in the first column, than all text of the second column, fiercely hoping, that they will magically align well!).

Fourth: To have the second column justified on the left (or right side) side --just in case --, you'll have to add some extra magic in the declaration of your table. In this case, use the >{\cmd} trick. The > will act everytime you enter the column, that the sign is pointing to. When the >acts, it will execute the code and commands in its braces. I used that trick, to have the first column in bold face series (command \bfseries) and in the second to get a flush left margin, by executing \RaggedRight. \RaggedRight come from the ragged2e-package. It enables a much better wrapping as the standard LaTeX \raggedright (all in small letters). The first command will break words, to have a better filled line, while the latter will only break the lines between words, which will waste space, especially in small columns.

Fifth: you were asking for a right formatted first column. Than you should use the r-column identifier for the first column. I changed that.

The following MWE will show you, how to typeset your desire in LaTeX. There is no need, to switch over to TeX. I'd say:

1. Line wrap in the second column: Check
2. objects on the left should be bold: Check
3. objects on the left are right justified: Check

Wait a moment: you asked for justification on the right but your image is justified on the left. I am unsure ... No problem. If my MWE is wrong, replace \RaggedRight in the definition of the table declaration to read RaggedLeft.

Here is my example:

\documentclass{article}

\usepackage{tabularx}           % Tables with varying column widths
\usepackage{array}              % some extra array magic you should
\usepackage{ragged2e}           % better ragged text
\usepackage{lipsum}             % blindtext

\begin{document}
\begin{tabularx}{1.0\linewidth}{|>{\bfseries}r|>{\RaggedRight\arraybackslash}X|}
\hline
Object 1 & This is object 1s description. It is on the same line,
but it may be longer than one line in the cell.\\
Object 2 & And this is even longer text.  But this time, this is a
\texttt{X}-column, it will be line wrapped nicely and
well.\\
Object 3 & To avoid the justification on both sides of the columns,
use the package \texttt{ragged2e}, which will break also
in the middle of the words.  Classical \LaTeX{} linewrap
will only break after the words, not in between.\\
\hline
Another Object 1 & \lipsum[2]\\
Object 2 & \lipsum[3]\\
\hline
\end{tabularx}
\end{document}


And the visual proof, that looks as your example image:

Just to be sure, this time using \RaggedLeft:

Also note: you used a c indicator in your example for the first row. That will centered the content of the first column. As your "Object x" do nearly all use the same width, you won't notice the difference to my r-column. Therefore, I used "Another Object 1", which is much wider. This shows the effect of column type definitions l, c and r much clearer.

Have fun.

• This response works great. Of course, sudden realization is that I need to tell tabularx to make the column width of the left side fixed and then maximize the right for a letter paper, but there should be solutions to that already. Thank you. – number9 Dec 29 '16 at 16:03
• There is one issue with this solution that cropped up. If the object on the left is very long (which has come up in this paper), it presents a problem. How to make the left split into multiple lines without changing the width of the left column? Is the solution to try to fix the width of the left? My attempt to do that has failed. : – number9 Dec 29 '16 at 20:09
• This was my attempt:\begin{tabularx}{1.0\linewidth}{|>{\bfseries\hsize=.1\hsize}r|>{\RaggedRight\arraybackslash\hsize=0.9\hsize}X|} – number9 Dec 29 '16 at 20:10
• @number9 an r column expands it width with every word you put in it. Therefore, if you have entries, that are getting longer and longer, your first column will increase and increase, while the X-column will shrink and shrink. To overcome that effect, you'll have to replace rwith a p{width}-column. In that case, you define the width of the first column. Overlong entries in that column will than also be wrapped and justified. In your case, to have it flush right formatted, you'll also have to add >{\RaggedLeft\arraybackslash\bfseries}p{2cm} in total as replacement to the r col. – Jan Dec 29 '16 at 20:31
• Excellent, it does work very well! Thank you! – number9 Dec 29 '16 at 20:49