带有合并单元格、多索引和文本换行的表格

带有合并单元格、多索引和文本换行的表格

我想要一张像这样的桌子。以下是在另一个选项卡中打开图像的链接。

桌子

对我来说重要的是结构。格式只是为了展示我想以某种方式脱颖而出的东西。

我如何能:

  1. 当数据超过特定长度时,确保文本换行。
  2. 如图所示,对右下方单元格使用多列吗?

以下是我所拥有的:

\documentclass[11pt]{article}
\usepackage{multirow}
\begin{document}
 % Text data placeholder. May be long enough that it needs to be wrapped.
\begin{table}[ht]
    \begin{center}
        \begin{tabular}{cccc}
            \hline
            \multirow{2}{*}{Outer index 1} & Inner index 1.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & Inner index 1.2 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\ 
            \hline
            \multirow{2}{*}{Outer index 2} & Inner index 2.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & Inner index 2.2 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            \hline
            \multirow{6}{*}{Outer index 3} &  Inner index 3.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.2 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.3 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.4 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.5 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.5 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            &Inner index 3.6 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            \hline
        \end{tabular}
    \end{center}
\end{table}

\end{document}

欢迎使用更好的格式。请随意添加您自己的风格。

答案1

其中包括:

  • tabularx对于X类型列和适合文本宽度的表格
  • \multicolumn 对于跨 2 列的单元格
  • 调整值以\multirow使文本在第一列垂直居中
  • 替换center环境,\centering因为前者在表格周围添加了额外的空白
  • 使用包装中的水平线booktabs来改善间距。

    \documentclass[11pt]{article}
    
    \usepackage{multirow}
    \usepackage{geometry}
    \usepackage{tabularx}
    \usepackage{booktabs}
    \usepackage{ragged2e}
    \begin{document}
    
    \begin{table}[ht]
        \centering
            \begin{tabularx}{\textwidth}{cc>{\RaggedRight\arraybackslash}X>{\RaggedRight\arraybackslash}X}
                \toprule
                \multirow{6}{*}{Outer index 1} & Inner index 1.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
                & Inner index 1.2 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\ 
                \midrule
                \multirow{6}{*}{Outer index 2} & Inner index 2.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
                & Inner index 2.2 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
                \midrule
                \multirow{9}{*}{Outer index 3} &  Inner index 3.1 & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
                &Inner index 3.2 & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
                &Inner index 3.3 & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{ Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
                \bottomrule
            \end{tabularx}
    \end{table}
    
    \end{document}
    

在此处输入图片描述


这里,我还使用了multirow第二列的内容:

\documentclass[11pt]{article}

\usepackage{multirow}
\usepackage{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{ragged2e}
\begin{document}

\begin{table}[ht]
    \centering
        \begin{tabularx}{\textwidth}{cc>{\RaggedRight\arraybackslash}X>{\RaggedRight\arraybackslash}X}
            \toprule
            \multirow{6}{*}{Outer index 1} & \multirow{3}{*}{Inner index 1.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \multirow{3}{*}{Inner index 1.2} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\ 
            \midrule
            \multirow{6}{*}{Outer index 2} & \multirow{3}{*}{Inner index 2.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \multirow{3}{*}{Inner index 2.2} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            \midrule
            \multirow{9}{*}{Outer index 3} &  \multirow{3}{*}{Inner index 3.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \multirow{3}{*}{Inner index 3.2} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
            & \multirow{3}{*}{Inner index 3.3} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{ Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
            \bottomrule
        \end{tabularx}
\end{table}

\end{document}

在此处输入图片描述


为了使第二列的文本垂直居中,您还可以将该makecell包与垂直居中X类型的列结合使用,如下面的 MWE 所示:

\documentclass[11pt]{article}

\usepackage{multirow}
\usepackage{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{ragged2e}
\usepackage{makecell}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{document}

\begin{table}[ht]
    \centering
        \begin{tabularx}{\textwidth}{cc>{\RaggedRight\arraybackslash}X>{\RaggedRight\arraybackslash}X}
            \toprule
            \multirow{6}{*}{Outer index 1} & \makecell[cc]{Inner index 1.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \makecell[cc]{Inner index 1.2} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\ 
            \midrule
            \multirow{6}{*}{Outer index 2} & \makecell[cc]{Inner index 2.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \makecell[cc]{Inner index 2.2} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            \midrule
            \multirow{9}{*}{Outer index 3} &  \makecell[cc]{Inner index 3.1} & Text data placeholder. May be long enough that it needs to be wrapped. & Text data placeholder. May be long enough that it needs to be wrapped.\\
            & \makecell[cc]{Inner index 3.2} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
            & \makecell[cc]{Inner index 3.3} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\RaggedRight\arraybackslash}X}{ Text data placeholder. May be long enough that it needs to be wrapped.  Text data placeholder. May be long enough that it needs to be wrapped.}\\
            \bottomrule
        \end{tabularx}
\end{table}

\end{document}

相关内容