可以在表中创建表

可以在表中创建表

是否可以在 LaTeX 中像这样在表格内创建表格?有人可以指导我如何实现这一点吗?谢谢

在此处输入图片描述

答案1

这是一种嵌套的可能性tabular

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage[table]{xcolor}

\begin{document}

\begingroup
\renewcommand\arraystretch{1.3}
\noindent\begin{tabular}{
  | >{\ttfamily\raggedright}p{2cm}
  | >{\sffamily\raggedright}p{2.5cm}
  | >{\sffamily}p{\dimexpr\textwidth-6\tabcolsep-4\fboxsep-4.5cm\relax} |
}
\hline
\rowcolor{gray!20}\multicolumn{1}{|l|}{\bfseries\sffamily Name} 
  & \multicolumn{1}{l|}{\bfseries\sffamily Type} 
  & \multicolumn{1}{l|}{\bfseries\sffamily Description} \\
\hline
& & \\[-2ex]
d\_super & struct disk\_superblock &
  \begin{tabular}[t]{
    | >{\ttfamily\raggedright}p{1.5cm}
    | >{\sffamily\raggedright}p{1.5cm}
    | >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax} |
  }
  \firsthline
  \multicolumn{1}{|l|}{\cellcolor{gray!20}\bfseries Name} 
    & \multicolumn{1}{l|}{\cellcolor{gray!20}\bfseries Type} 
    & \multicolumn{1}{l|}{\cellcolor{gray!20}\bfseries Description} \\
  \hline
  magic & char array & magic number used to indicate if file system is generated by our OS \\
  \hline
  magic & char array & magic number used to indicate if file system is generated by our OS \\
  \hline
  \end{tabular} \\[15ex]
\hline
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
\hline
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
\hline
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
\hline
\end{tabular}
\endgroup

\end{document}

在此处输入图片描述

并取消垂直规则并使用booktabs

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\noindent\begin{tabular}{
   >{\ttfamily\raggedright}p{2cm}
   >{\sffamily\raggedright}p{2.5cm}
   >{\sffamily}p{\dimexpr\textwidth-6\tabcolsep-4\fboxsep-4.5cm\relax}
}
\toprule
\multicolumn{1}{l}{\bfseries\sffamily Name} 
  & \multicolumn{1}{l}{\bfseries\sffamily Type} 
  & \multicolumn{1}{l}{\bfseries\sffamily Description} \\
\midrule
& & \\[-2ex]
d\_super & struct disk\_superblock &
  \vspace{-15pt}\begin{tabular}[t]{
     >{\ttfamily\raggedright}p{1.5cm}
     >{\sffamily\raggedright}p{1.5cm}
     >{\sffamily}p{\dimexpr\textwidth-12\tabcolsep-5\fboxsep-7.5cm\relax}
  }
  \toprule
  \multicolumn{1}{l}{\bfseries\sffamily Name} 
    & \multicolumn{1}{l}{\bfseries\sffamily Type} 
    & \multicolumn{1}{l}{\bfseries\sffamily Description} \\
  \midrule
  d\_super & char array & magic number used to indicate if file system is generated by our OS \\
  magic & char array & magic number used to indicate if file system is generated by our OS \\
  \bottomrule
  \end{tabular} \\[16.5ex]
\midrule  
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
ibmap & void\textsuperscript{*} & Pointer to l-Nodes bitmap \\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

答案2

您可以tabularx毫无问题地嵌套。我这样做是为了创建一个 RPG 角色表,并在 tex.SE 上的好心人的帮助下添加了许多小细节……例如,从文档中摘录一些源代码,翻译和评论以便于阅读……不过截图是原始的德语。而且我现在没有一个独立的、可编译的示例,这是 500 多页的卷的一部分,仅序言就有 12k……

原本的

% In the preamble... you *probably* need some packages in addition to
% tabularx for the below to work, but it's inconsequential to the subject
% of tabularx nesting, and I don't have the leisure to turn this into
% a compilable stand-alone

\newcommand{\Top}{\rule{0pt}{2.6ex}}       % strut for \hline above
\newcommand{\Bot}{\rule[-1.2ex]{0pt}{0pt}} % strut for \hline below

% Create an underline as long as the given text would be
\newcommand*{\srule}[2][0pt]{\setbox0\hbox{#2}\rule[#1]{\wd0}{0.4pt}}

% New tabularx column type: C is like X, just centered.
\newcolumntype{C}{>{\centering\arraybackslash}X}

% Making \cellwidth the width of the tabularx cell we are currently in
\makeatletter
\newcommand{\cellwidth}{\TX@col@width}
\makeatother

% --------------------------------------
% In the document...
% --------------------------------------

% Full-width table with three cells, each containing a table in turn
% @{} suppressing padding -- the sub-tables take care of that.
\noindent\begin{tabularx}{\textwidth}{@{}X@{}X@{}X@{}}
{
% Cell 1 -- Character description
\begin{tabularx}{\cellwidth}{|lX}
\hline
\multicolumn{2}{|c}{\scshape\textbf{Character}\Top{}\Bot{}}\\
\textbf{Name:}        & \hrulefill\Top{}\\
\textbf{Race:}        & \hrulefill\\
\textbf{Height:}      & \hrulefill\\
\textbf{Build:}       & \hrulefill\\
\textbf{Hair:}        & \hrulefill\\
\textbf{Eyes:}        & \hrulefill\\
\textbf{Outlook:}     & \hrulefill\\
\textbf{Age:}         & \hrulefill\Bot{}\\
\hline
\end{tabularx}
} & {
% Cell 2 -- Header, Basic stats.
\begin{tabularx}{\cellwidth}{@{}C@{}}
{
% Actually setting up a sub-sub-table here for the header...
\begin{tabularx}{\cellwidth}{|@{}C@{}|}
\hline
% Inserting a graphical header here, fudging a bit with \rule to get the padding right
\rule{0pt}{29.25pt}\includegraphics[width=0.95\cellwidth]{pic/CharacterSheet.pdf}\\
\scshape\textbf{Player}\\
\Top{}\Bot{}\\
\hline
\end{tabularx}
}\\
{
% ...and another sub-sub-table for the basic stats.
\begin{tabularx}{\cellwidth}{lX}
\textbf{Profession:}   & \hrulefill\Top{}\\
\textbf{Power Realm:}  & \hrulefill\\
\textbf{Experience:}   & \hrulefill\\
\textbf{Level:}        & \hrulefill\Bot{}\\
\hline
\end{tabularx}}\\
\end{tabularx}
} & {
% Cell 3 -- Languages
\begin{tabularx}{\cellwidth}{|Xcc|}
\hline
\multicolumn{3}{|c|}{\scshape\textbf{Languages}}\Top{}\Bot{}\\
\textbf{Language} & \textbf{Spoken} & \textbf{Written}\Top{}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\\
\hrulefill       & \srule{Spoken}  & \srule{Written}\Bot{}\\
\hline
\end{tabularx}
}\\  
\end{tabularx}

相关内容