使 tabularx 文本换行

使 tabularx 文本换行

我不知道为什么这段代码中的文本没有换行

\documentclass{article}

\usepackage{tabularx}
\usepackage{multirow, multicol}
\usepackage{array}

% column vertical space buffer in tables
\def\arraystretch{2}

% \maketable{number of columns}{table data}
\newcommand{\maketable}[2]{
    \centering
    \begin{tabularx}{\textwidth}{
        % paste |>{\raggedright\arraybackslash}X
        % #1 number of times. Finally, append a |
        % to add the right wall of table
        *{#1}{|>{\raggedright\arraybackslash}X}|
    } 
        \hline
        #2
        \hline
    \end{tabularx}
}

\newcommand{\mergecols}[2]{\multicolumn{#1}{|l|}{#2}\\\hline}

\begin{document}
\maketable{3}{
    \mergecols{3}{\textbf{Title}: Affect of Color Filtered Light on Photosynthesis and Oxygen Production}

    \mergecols{3}{\textbf{Hypothesis}: Stuff About Project}

    \mergecols{3}{\textbf{Experimental Units}: Freshwater Algae Suspended in Sodium Alginate Beads}

    \mergecols{3}{\textbf{Randomization}: The Algae Beads will be Randomly Assigned to a Specific Color Filter}

    \mergecols{3}{\textbf{Independent Variable (IV)}: The color of the light filter placed on the algae beads}

    \textbf{Treatment Groups:} & 2 & 3\\ \hline
    \textbf{Independent Trials} & 2 & 3\\ \hline

    \mergecols{3}{\textbf{Dependant Variable (DV)}: The rate of photosynthesis/the amount of oxygen found in the samples}

    \mergecols{3}{\textbf{Constants}: Stuff About Project}
}
\end{document}

我在看问题,但我无法让它工作,并希望得到任何帮助。

答案1

  • 通过使用tabularray包,直接在表中编写表格(参见下面 MWE 的第一部分)相当简单,而无需使用。
  • 然而,@Mico 的回答 (+1) 更能体现出您的表格的“专业”外观。下面 MWE 的第二部分给出了它的细微变化:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
\noindent\begin{tblr}{hlines, vlines,
                      colspec = { *{3}{X[l]} },
                      cell{1-5,8,9}{1} = {c=3}{},
                      hspan = minimal,
                      }
\textbf{Title}: Affect of Color Filtered Light on Photosynthesis and Oxygen Production
    &   &   \\
\textbf{Hypothesis}: Stuff About Project 
    &   &   \\
\textbf{Experimental Units}: Freshwater Algae Suspended in Sodium Alginate Beads
    &   &   \\
\textbf{Randomization}: The Algae Beads will be Randomly Assigned to a Specific Color Filter
    &   &   \\
\textbf{Independent Variable (IV)}: The color of the light filter placed on the algae beads
    &   &   \\
\textbf{Treatment Groups:} 
    & 2 & 3 \\ 
\textbf{Independent Trials} 
    & 2 & 3 \\
\textbf{Dependant Variable (DV)}: The rate of photosynthesis/the amount of oxygen found in the samples
    & 2 & 3 \\
\textbf{Constants}: Stuff About Project
    & 2 & 3 \\
    \end{tblr}
    
\bigskip

\noindent\begin{tblr}{colspec = {@{} X[r, font=\bfseries] *{2}{X[l]} @{}},
                      cell{1-5,8,9}{2} = {c=2}{},
                      rowsep  = 3pt,
                      hspan   = minimal,
                      }
    \toprule
Title:  &   Affect of Color Filtered Light on Photosynthesis and Oxygen Production
            &       \\
Hypothesis:
        &   Stuff About Project
            &       \\
Experimental Units:
        &   Freshwater Algae Suspended in Sodium Alginate Beads
            &       \\
Randomization:
        &   The Algae Beads will be Randomly Assigned to a Specific Color Filter
            &       \\
Independent Variable (IV):
        &   The color of the light filter placed on the algae beads
            &       \\
    \midrule
Treatment Groups:
        & 2 & 3     \\
Independent Trials:
        & 2 & 3     \\
    \midrule
Dependant Variable (DV):
        &   The rate of photosynthesis/the amount of oxygen found in the samples
            &       \\
Constants:
        &   Stuff About Project
            &       \\
    \bottomrule
    \end{tblr}
\end{document}

在此处输入图片描述

答案2

以下内容是否可能与您想要完成的事情有些接近?

请注意我不会粗体第一栏的材料。但是,如果你认为粗体显示是基本的,只需将L的最后一个参数中的第一个更改为。tabularx>{\bfseries}L

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{ragged2e} % for '\RaggedRight' macro
\usepackage{booktabs} % for well-spaced horizontal rules
\newcolumntype{L}{>{\RaggedRight}X} % suppress full justification
% handy shortcut macro (for combing columns 2 and 3):
\newcommand\mC[1]{\multicolumn{2}{%
   >{\hsize=\dimexpr2\hsize+2\tabcolsep\relax}L}{#1}}
\setlength\extrarowheight{3pt} % for a more open "look"

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{} LLL @{}}
\toprule
Title              & \mC{Effect of color filtered light on photosynthesis and oxygen production} \\
Hypothesis         & \mC{Stuff about project} \\
Experimental units & \mC{Freshwater algae suspended in sodium alginate beads} \\
Randomization      & \mC{The algae beads will be randomly assigned to a specific color filter} \\
Independent variable (IV) & \mC{The color of the light filter placed on the algae beads} \\ \midrule
Treatment groups   & 2 & 3 \\ 
Independent trials & 2 & 3 \\ \midrule
Dependent variable (DV) & \mC{The rate of photosynthesis\slash the amount of oxygen found in the samples} \\
Constants          & \mC{Stuff about project} \\
\bottomrule
\end{tabularx}

\end{document}

相关内容