Generalalignment in tabular

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
radsim
Posts: 12
Joined: Mon Jun 02, 2008 10:12 am

alignment in tabular

Post by radsim »

Hi everybody,
I need a table of the following format:

X | text in two lines | Y | paragraph-like text

The problem with using {c|p{x}|c|p{x}}:
If I break the line in the second column (to create the needed second line), the text of the first line spans over the whole width of the column, which looks extremely ugly.

The problem with using \multirow for the last column:
The text of the last column often (but not always) spans over more than two lines.

Any suggestions for an elegant solution? Is it possible to align paragraph-like text in tables to the left? That would be the easiest way to go, I guess.
Thanks a lot!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

alignment in tabular

Post by Stefan Kottwitz »

Hi radsim,

you could use \raggedright inside cells or even in the column definition if you use the array package, for example

Code: Select all

>{\raggedright\arraybackslash}p{x}
Stefan
LaTeX.org admin
radsim
Posts: 12
Joined: Mon Jun 02, 2008 10:12 am

Re: alignment in tabular

Post by radsim »

Thanks a lot! I didn't find out how to make the specification in the column definition (what you suggested gives me an error), but doing it in particular cells works perfectly.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

alignment in tabular

Post by Stefan Kottwitz »

radsim wrote:I didn't find out how to make the specification in the column definition (what you suggested gives me an error)
It should work together with array, try it with

Code: Select all

\usepackage{array}
Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

alignment in tabular

Post by Juanjo »

Try this minimal example:

Code: Select all

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\raggedright}p{3cm}|c|>{\raggedright}p{3cm}|}
  \hline
  A & First line and now \\ the second line  & B
    & Fist line and also \\ a second line \tabularnewline \hline
  C & Text, text, text, text & D
    & Text, text, text, text, text \tabularnewline \hline
\end{tabular}
\end{document}
The \tabularnewline command works like \\, but it is necessary here to distinguish from a new line inside the last cell in a row and a new row. In my opinion, the code >{\raggedright\arraybackslash}p{x} does not work properly if the corresponding cell contains explicitly a newline command (i.e. \\).
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

alignment in tabular

Post by Stefan Kottwitz »

Hi,

\arraybackslash will work as expected if \\ is used to end rows and \newline to end lines inside cells, as you can see with this modification:

Code: Select all

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\raggedright}p{3cm}|c|>{\raggedright\arraybackslash}p{3cm}|}
  \hline
  A & First line and now \newline the second line  & B
    & Fist line and also \newline a second line \\ \hline
  C & Text, text, text, text & D
    & Text, text, text, text, text \\ \hline
\end{tabular}
\end{document}
In this case you don't need the longer \tabularnewline at the end of each row. But \\ would not be allowed inside cells, as Juanjo correctly mentioned, and must be replaced by \newline if needed.

Stefan
LaTeX.org admin
radsim
Posts: 12
Joined: Mon Jun 02, 2008 10:12 am

Re: alignment in tabular

Post by radsim »

Very cool, thank you both. Realized I forgot to put there ">". For a new line inside a cell, also \linebreak works well.
Post Reply