![Welche guten Möglichkeiten gibt es, einen erzwungenen Zeilenumbruch innerhalb einer Tabellenzelle vorzunehmen und dabei die richtige Formatierung beizubehalten?](https://rvso.com/image/475726/Welche%20guten%20M%C3%B6glichkeiten%20gibt%20es%2C%20einen%20erzwungenen%20Zeilenumbruch%20innerhalb%20einer%20Tabellenzelle%20vorzunehmen%20und%20dabei%20die%20richtige%20Formatierung%20beizubehalten%3F.png)
Ich habe mir einige der verschiedenen Lösungen im folgenden Thread zum Erzwingen von Zeilenumbrüchen innerhalb einer Tabellenzelle angesehen:
So fügen Sie einen erzwungenen Zeilenumbruch innerhalb einer Tabellenzelle hinzu
Darauf aufbauend habe ich dann eine Tabelle mit einigen der vorgeschlagenen Methoden für Zeilenumbrüche zusammengestellt, um zu sehen, wie sie mit der Art und Weise interagieren, wie ich die Formatierung meiner Tabellen in der Präambel aufgelistet habe:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{array}
\setlength{\tabcolsep}{18pt}
\setlength{\arrayrulewidth}{0.5mm}
\renewcommand{\arraystretch}{1.5}
% ________________________________
\usepackage[usestackEOL]{stackengine}
% Package for \strutlongstacks{T} forced linebreak method
\usepackage{makecell}
% Package for \makecell forced linebreak method
\begin{document}
\begin{table}[h]
\centering
\strutlongstacks{T}
\begin{tabular}{|c|c|m{3cm}|}
\hline
\multicolumn{3}{|c|}{Calender for the rest of the year} \\
\hline
\textbf{Date} & \textbf{Homework} & \textbf{Goal} \\
\hline
Week 1 & Homework 1 and 2 & \begin{tabular}[c]{@{}c@{}}Goal for 1 is to... \\ Goal for 2 is to... \end{tabular} \\ % Text exceeds cell when limit is reached
\hline
Week 2 & Homework 3 and 4 & \vtop{\hbox{\strut Goal for 3 is to...}\hbox{\strut Goal for 4 is to...}} \\ % Force elongates cell on itself when limit is exceeded
\hline
Week 3 & Homework 5 and 6 & \Centerstack{Goal for 5 is to... \\ Goal for 6 is to...} \\ % Text exceeds cell when limit is reached
\hline
Week 4 & Homework 7 and 8 & \begin{flushleft} Goal for 7 is to... \\ Goal for 8 is to... \end{flushleft} \\ % Works for long text, too, only for column types of "|m{}|", though, not for regular "|c|"
\hline
Week 5 & Homework 9 and 10 & \parbox{3cm}{Goal for 9 is to... \\ Goal for 10 is to...} \\ % Works but completely ignores the \renewcommand{\arraystretch}{1.5} in the preamble
\hline
Week 6 & Homework 11 and 12 & \makecell{Goal for 11 is to... \\ Goal for 12 is to...} \\ % Text exceeds cell when limit is reached
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}
Wie es auf der Ausgabeseite aussieht, könnt ihr hier sehen:
Mir gefällt, wie Ziel 1 und 2 angezeigt werden, und es ergänzt meine Einstellungen in der Präambel. Daher fände ich es idealerweise gut, wenn es so geändert werden könnte, dass, wenn ich es m{3cm}
in der dritten Spalte verwende, die zweite Spalte immer noch zentriert ist, wie bei den Hausaufgaben 7 und 8, UND dass auch automatische Zeilenumbrüche erfolgen, wenn der Text die für die Zelle vorgesehenen Grenzen überschreitet.
Wenn Ihnen Ideen einfallen, wie man die Formatierung dieser Dateien optimieren oder neue kreative Möglichkeiten finden kann, wäre ich Ihnen sehr dankbar, wenn Sie sie hier teilen würden:)
Antwort1
So wie ich es verstehe, besteht Ihr Problem darin, eine mehrzeilige Spalte mit fester Größe und vertikaler Mitte zu erhalten.
Sie können feste Größen in verwenden \makecell
. Beispiel:
\makecell[c{p{3cm}}]{Goal for 1 is to... \\ Goal for 2 is to...}
erstellt eine vertikal zentrierte Zelle mit einer festen Breite von 3cm
. Da die Breite in Ihrem Fall in der Spaltenspezifikation festgelegt ist, ersetzen Sie 3cm
durch \hsize
(es wird die Breite der Spalte übernommen). Um jedoch eine korrekte vertikale Position zu erhalten, müssen Sie \makecell
dies handhaben lassen und in der Spaltenspezifikation p{3cm}
anstelle von verwenden.m{3cm}
Fügen Sie auch hinzu
\renewcommand{\cellset}{\def\arraytretch{1.5}}%
wenn Sie größere Innenräume wünschen \makecell
.
Vollständiges Beispiel:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{array}
\usepackage{makecell}
\setlength{\tabcolsep}{18pt}
\setlength{\arrayrulewidth}{0.5mm}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\cellset}{\def\arraytretch{1.5}}%
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|c|c|p{3cm}|}
\hline
\multicolumn{3}{|c|}{Calender for the rest of the year} \\
\hline
\textbf{Date} & \textbf{Homework} & \textbf{Goal} \\
\hline
\hline
Week 1 & Homework 1 and 2
& \makecell[c{p{\hsize}}]{Goal for 1 is to... lot of things that take many lines \\
Goal for 2 is to... lot of things that take many lines} \\
\hline
Week 2 & Homework 3 and 4
& \makecell[c{p{\hsize}}]{Goal for 3 is to... lot of things that take many lines \\
Goal for 4 is to... lot of things that take many lines} \\
\hline
Week 3 & Homework 5 and 6
& \makecell[c{p{\hsize}}]{Goal for 5 is to... lot of things that take many lines \\
Goal for 6 is to... lot of things that take many lines} \\
\hline
\end{tabular}
\caption{With \textsf{makecell}}
\label{tab:my_label}
\end{table}
\end{document}
Jedenfalls gibt es jetzt dietabellarischesrayPaket, das viele Anpassungen für Tabellen über eine moderne Schlüssel-Wert-Schnittstelle bietet und die Funktionalitäten klassischer Tabellenpakete repliziert. Mit wird tabularray
eine vertikal zentrierte Spalte 3cm
mit der Breite festgelegt Q[3cm,m]
( Q
ist der von verwendete Spaltenspezifizierer tabularray
) und mehrzeilige Zellen werden einfach mit Klammern erhalten:
Week 1 & Homework 1 and 2 & {Goal for 1 is to...\\ Goal for 2 is to...} \\
Beispiel (sehen Sie, wie es klarer wird, wenn Formatierung und Inhalt getrennt sind):
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\centering
\begin{tblr}
{
colspec={ | Q[c,m] | Q[c,m] | Q[3cm,m] |},
hline{1-Z} = {1}{-}{0.5mm, solid},
hline{3} = {2}{-}{0.5mm, solid},
vlines={0.5mm},
row{2} = {font=\bfseries},
colsep=18pt,
stretch=1.5,
}
\SetCell[c=3]{c} Calender for the rest of the year
\\
Date & Homework & Goal
\\
Week 1 & Homework 1 and 2
& {Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines}
\\
Week 2 & Homework 3 and 4
& {Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines}
\\
Week 3 & Homework 5 and 6
& {Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines}
\end{tblr}
\caption{With \textsf{tabularray}}
\label{tab:my_label}
\end{table}
\end{document}
Antwort2
Ich glaube nicht, dass Sie irgendetwas Ausgefallenes brauchen … Abgesehen davon, \tabularnewline
um Zeilen zu beenden, wegen \\
der letzten Spalte.
\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array}
\begin{document}
\begin{table}[htp]
\centering
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|c|c|>{\raggedright}m{5cm}|}
\hline
\multicolumn{3}{|c|}{Calendar for the rest of the year} \tabularnewline
\hline
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c|}{\textbf{Goal}} \tabularnewline
\hline
Week 1 & Homework 1 and 2 &
Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines \tabularnewline
\hline
Week 2 & Homework 3 and 4 &
Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines \tabularnewline
\hline
Week 3 & Homework 5 and 6 &
Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines
Goal for 1 is to... \tabularnewline
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}
Alternatives Layout:
\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array,booktabs}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{@{}cc>{\raggedright}p{5cm}@{}}
\toprule
\multicolumn{3}{c}{Calendar for the rest of the year} \tabularnewline
\midrule
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c}{\textbf{Goal}} \tabularnewline
\midrule
Week 1 & Homework 1 and 2 &
Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 2 & Homework 3 and 4 &
Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 3 & Homework 5 and 6 &
Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines
Goal for 1 is to... \tabularnewline
\bottomrule
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}