Rows with same height in table in Latex

Asked

Viewed 8,916 times

8

I need to play in Latex a table with 3 columns and 5 rows. It should use the entire paragraph width, center the cell contents both horizontally and vertically, and auto-adjust longer texts. But most of all, you need to have same height for all lines.

I used the tabularx (to more easily have the table in paragraph width), I reset the type of column X to center the contents of the cells horizontally and vertically and I arrived at this result:

Code with Minimal Compilable Example

\documentclass[10pt,a4paper]{article}

\usepackage[portuguese]{babel}
\usepackage[latin1]{inputenc}
\usepackage{tabularx}
\usepackage{ragged2e}

\begin{document}

% Redefine a coluna do tipo X para centralizar horizontal e verticalmente seu conteúdo
\renewcommand\tabularxcolumn[1]{>{\Centering}m{#1}} 

\begin{table}[!h]
    \begin{center}
        \begin{tabularx}{\textwidth}{|X|X|X|}
            \hline
            Linha 1 & Lorem ipsum dolor sit amet. & Aenean suscipit, nunc ac sodales bibendum, massa lacus iaculis augue, ut suscipit mauris libero in velit. \\
            \hline

            Linha 2 & Vivamus quis justo ac elit condimentum molestie. & Etiam ultrices a libero sed semper. \\
            \hline

            Linha 3 & Morbi ultrices sodales justo, et dictum quam sodales eget. & Cras tortor libero, volutpat eget erat eu, blandit tempus nunc. \\
            \hline              

            Linha 4 & Pellentesque gravida, odio sed aliquet tempus, metus lectus sodales sapien. & Fusce fermentum malesuada eros. \\
            \hline              

            Linha 5 &  Etiam commodo interdum dictum. In sit amet semper leo. In non auctor mauris. & Vivamus ultrices augue non enim pulvinar feugiat. \\
            \hline                                      
        \end{tabularx}
    \end{center}
\end{table}

\end{document}

Resulting Table inserir a descrição da imagem aqui

I just haven’t been able to leave all the lines at the same height yet (ideally the first line, since it was the least possible to include the contents of the rightmost cell). I’ve tried to specify additional spacing at the end of each line, but this solution requires a lot of trial and error and the end result is not good because it adds spaces only in the last column. I also tried use the package easytable, but it does not allow to fix the width of the paragraph and ends up extrapolating the page limits, and also does not make the automatic breaking of long texts. And finally I tried to do it too the change in vertical spacing (or arraystretch), but the result was not expected because apparently this command only increases the internal margin of the cells and does not guarantee that the lines have the same height.

Does anyone have a solution to this problem?

  • There is a website for Tex.

  • @Lucashenrique I know (so much so that I even referenced in the question itself some information directly from there). I just wanted to post the question here to stimulate Tex content on Sopt.

1 answer

3


As Voce also used, the package tabularx and better to handle tables with large lines, as it makes the line break alone.

To get all the cells of the same height I used the command \parbox. This command creates a paragraph within a box and the general syntax and:

\parbox[pos][height][contentpos]{width}{text}

pos can be b,c or t and controls the alignment of the box, relative to the base line of the text. height and height, which I used to control the height of each cell. contentpos controls the position of the text, which I know I used to leave centralized with the c. width and the size of the box. As we are inside the table, \linewidth and 1/3 of the row, since we have 3 columns. Then comes the text.

A parbox and basically a minipage.

His example table was quite large, however :P

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{.85\linewidth}{|X|X|X|}
\hline
\parbox[b][\linewidth][c]{\linewidth}{\centering Linha 1}
& \parbox[b][\linewidth][c]{\linewidth}{\centering  Lorem ipsum dolor sit amet.}
& \parbox[b][\linewidth][c]{\linewidth}{\centering  Aenean suscipit,
  nunc ac sodales bibendum, massa lacus iaculis augue, ut suscipit
  mauris libero in velit.}\\
\hline
\parbox[b][\linewidth][c]{\linewidth}{\centering Linha 2}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Vivamus quis justo ac elit condimentum molestie.}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Etiam ultrices a
  libero sed semper.}\\
\hline
\parbox[b][\linewidth][c]{\linewidth}{\centering Linha 3}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Morbi ultrices sodales justo, et dictum quam sodales eget.}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Cras tortor libero,
  volutpat eget erat eu, blandit tempus nunc.}\\
\hline
\parbox[b][\linewidth][c]{\linewidth}{\centering Linha 4}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Pellentesque
  gravida, odio sed aliquet tempus, metus lectus sodales sapien.}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Fusce fermentum
  malesuada eros.}\\
\hline
\parbox[b][\linewidth][c]{\linewidth}{\centering Linha 5}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Etiam commodo interdum dictum. In sit amet semper leo. In non auctor mauris.}
& \parbox[b][\linewidth][c]{\linewidth}{\centering Vivamus ultrices
  augue non enim pulvinar feugiat.}\\
\hline
\end{tabularx}
\end{document}
\end{document}

Gera:

Tabela gerada com o codigo acima

References about the parbox on wikipedia.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.