![¿Cuáles son buenas formas de realizar un salto de línea forzado dentro de una celda de una tabla manteniendo el formato adecuado?](https://rvso.com/image/475726/%C2%BFCu%C3%A1les%20son%20buenas%20formas%20de%20realizar%20un%20salto%20de%20l%C3%ADnea%20forzado%20dentro%20de%20una%20celda%20de%20una%20tabla%20manteniendo%20el%20formato%20adecuado%3F.png)
Analicé algunas de las diferentes soluciones proporcionadas en el siguiente hilo sobre cómo realizar saltos de línea forzados dentro de una celda de una tabla:
Cómo agregar un salto de línea forzado dentro de una celda de una tabla
En base a eso, compilé una tabla que consta de algunos de los métodos de salto de línea sugeridos para ver cómo interactúan con la forma en que enumeré el formato de mis tablas en el preámbulo:
\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}
Puedes ver aquí cómo se ve en el lado de salida:
Me gusta cómo se muestran los objetivos 1 y 2 y complementan mi configuración en el preámbulo y, por lo tanto, idealmente me gustaría que se pudiera modificar de manera que cuando uso m{3cm}
en la tercera columna, la segunda columna todavía esté centrada como para la tarea. 7 y 8 Y también realiza saltos de línea automáticos cuando el texto excede los límites proporcionados a la celda.
Si se les ocurre alguna forma de optimizar cualquiera de estos para que tenga el formato adecuado o se les ocurren nuevas formas creativas que lo hagan, les agradeceríamos mucho que las compartieran aquí :)
Respuesta1
Según tengo entendido, su problema es obtener una columna de varias líneas de tamaño fijo, centrada verticalmente.
Puede utilizar tamaños fijos en \makecell
. Ejemplo:
\makecell[c{p{3cm}}]{Goal for 1 is to... \\ Goal for 2 is to...}
creará una celda centrada verticalmente, con un ancho fijo de 3cm
. En su caso, dado que el ancho se establece en la especificación de la columna, reemplácelo 3cm
con \hsize
(tomará el con de la columna). Pero para obtener una posición vertical correcta, debes dejar \makecell
manejarlo y usarlo p{3cm}
en lugar de m{3cm}
en la especificación de la columna.
Añadir también
\renewcommand{\cellset}{\def\arraytretch{1.5}}%
si quieres espacios más grandes en el interior \makecell
.
Ejemplo completo:
\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}
De todos modos, ahora existe elrayo tabularpaquete, que proporciona muchas personalizaciones para tablas a través de una moderna interfaz clave-valor y replica las funcionalidades de los paquetes de tablas clásicos. Con , se establece tabularray
una columna de ancho centrada verticalmente con ( es el especificador de columnas utilizado por ) y la celda de varias líneas simplemente se obtiene con llaves:3cm
Q[3cm,m]
Q
tabularray
Week 1 & Homework 1 and 2 & {Goal for 1 is to...\\ Goal for 2 is to...} \\
Ejemplo (mira cómo queda más claro, con una separación entre formato y contenido):
\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}
Respuesta2
No creo que necesites nada sofisticado... Aparte de \tabularnewline
para finalizar las filas, debido a que está \\
en la última columna.
\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}
Diseño alternativo:
\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}