적절한 서식을 유지하면서 표 셀 내에서 강제로 줄 바꿈을 수행하는 좋은 방법은 무엇입니까?

적절한 서식을 유지하면서 표 셀 내에서 강제로 줄 바꿈을 수행하는 좋은 방법은 무엇입니까?

테이블 셀 내에서 강제 줄 바꿈을 만드는 방법에 대해 다음 스레드에서 제공되는 몇 가지 다양한 솔루션을 살펴보았습니다.

표 셀 내부에 강제 줄 바꿈을 추가하는 방법

그런 다음 이를 바탕으로 제안된 줄 바꿈 방법 중 일부로 구성된 표를 컴파일하여 해당 방법이 서문에 테이블 형식을 나열한 방법과 어떻게 상호 작용하는지 확인했습니다.

\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}

출력 측에서 어떻게 보이는지 여기서 볼 수 있습니다. 코드의 시각적 표현

m{3cm}목표 1과 2가 표시되고 서문의 설정을 칭찬하는 방식이 마음에 듭니다. 따라서 세 번째 열에서 사용할 때 숙제처럼 두 번째 열이 여전히 중앙에 위치하도록 수정될 수 있으면 이상적입니다. 7 및 8 AND는 텍스트가 셀에 제공된 제한을 초과하는 경우 자동 줄 바꿈도 수행합니다.

이러한 항목을 적절한 형식으로 최적화하거나 새로운 창의적인 방법을 생각해 낼 수 있는 방법이 있다면 여기에 공유해 주시면 매우 감사하겠습니다. :)

답변1

귀하의 문제는 수직 중앙에 고정된 크기의 여러 줄 열을 얻는 것입니다.

에서는 고정 크기를 사용할 수 있습니다 \makecell. 예:

\makecell[c{p{3cm}}]{Goal for 1 is to... \\ Goal for 2 is to...}

고정 너비를 갖는 수직 중앙 셀을 생성합니다 3cm. 귀하의 경우 너비는 열 사양에 설정되어 있으므로 다음 3cm으로 바꾸십시오 \hsize(열의 ~를 사용함). 그러나 올바른 수직 위치를 얻으려면 열 사양 대신 \makecell이를 처리하고 사용하도록 해야 합니다.p{3cm}m{3cm}

또한 추가

\renewcommand{\cellset}{\def\arraytretch{1.5}}%

내부에 더 큰 공간을 원한다면 \makecell.

전체 예:

\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}

메이크셀과 함께

어쨌든 지금은테이블 형식최신 키-값 인터페이스를 통해 테이블에 대한 다양한 사용자 정의를 제공하고 기존 테이블 패키지의 기능을 복제하는 패키지입니다. 를 사용하면 수직으로 tabularray가운데 정렬된 너비 열이 ( 는 에서 사용되는 열 지정자임 ) 3cm로 설정되고 중괄호를 사용하여 여러 줄의 셀을 간단히 얻을 수 있습니다.Q[3cm,m]Qtabularray

Week 1 & Homework 1 and 2  & {Goal for 1 is to...\\  Goal for 2 is to...} \\

예(서식과 내용을 구분하여 얼마나 더 명확해졌는지 확인하세요):

\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}

테이블 형식으로

답변2

화려한 것은 필요하지 않을 것 같습니다… \tabularnewline행을 끝내기 위해 \\마지막 열에 있기 때문입니다.

\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}

여기에 이미지 설명을 입력하세요

대체 레이아웃:

\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}

여기에 이미지 설명을 입력하세요

관련 정보