
Я пытаюсь создать большую таблицу, в которой каждое поле имеет несколько строк текста друг под другом. Я пробовал \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
.
\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.
Видетьэта темачтобы иметь пример его использования.