Составление таблицы с несколькими строками в каждом поле

Составление таблицы с несколькими строками в каждом поле

Я пытаюсь создать большую таблицу, в которой каждое поле имеет несколько строк текста друг под другом. Я пробовал \newlineи , \linebreakчтобы перейти на новую строку, но ни то, ни другое не работает. Команда \\создает новую строку, но она завершает всю строку во всей таблице.

Может ли кто-нибудь подсказать, как мне получить новую линию или, может быть, более удобный пакет для использования?

решение1

Используйте parbox{}{}как в:

\documentclass{standalone}

\begin{document}

    \begin{tabular}{ll}
        \hline
        row 1, column 1, line 1 & \parbox{5cm}{row 1, column 2, line 1 \\ row 1, column 2, line 2 \\ row 1, column 2, line 3} \\ \hline
        \parbox{5cm}{row 2, column 1, line 1 \\ row 2, column 1, line 2} & row 2, column 2, line 1 \\ 
        \hline
    \end{tabular}

\end{document}

введите описание изображения здесь

решение2

Есть несколько возможных решений. Ниже показаны 2 подхода. Первый основан только на стандартных командах. Хотя я использовал их booktabsпо эстетическим причинам, это не является существенным для решения. Второй использует tabularx.

2 способа получить <code>\newline</code> в табличных данных

\documentclass{article}
\usepackage{tabularx,booktabs}
\begin{document}

One method involves using the standard \verb|p{<width>}| column specifier. This requires knowing how wide you want the columns but it allows you to use \verb|\newline| and does not require additional packages. Table \ref{tab:standard} does use commands from \verb|booktabs| to improve the tabular's appearance but you could replace with \verb|\hline| etc.\ if preferred.
\begin{table}
\centering
\begin{tabular}{*{2}{p{.285\linewidth}}}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabular}
\caption{Tabular with standard commands}\label{tab:standard}
\end{table}
\bigskip

If you don't know how wide the columns should be and don't wish to figure it out, but you can specify the overall width of the tabular, \verb|tabularx| can be used. This supports the \verb|X| column specifier which figures out the width based on the overall tabular width. It also allows \verb|\newline|. Table \ref{tab:tabularx} again uses \verb|booktabs| but that is for merely aesthetic reasons.
\begin{table}
\centering
\begin{tabularx}{.65\linewidth}{XX}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabularx}
\caption{Tabular with tabularx}\label{tab:tabularx}
\end{table}

\end{document}

решение3

Не упомянутый в предыдущих ссылках, makecellпакет определяет команды \makecell, thead, diagheadи \multirowcell, которые допускают использование \\и могут быть дополнительно настроены (выбор шрифта, поворот, вертикальные пробелы и т. д.). Можно изменить толщину линий hlines и clines.

Видетьэта темачтобы иметь пример его использования.

Связанный контент