![如何使表格单元格内的列表更靠近边框?](https://linux22.com/image/470588/%E5%A6%82%E4%BD%95%E4%BD%BF%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E5%86%85%E7%9A%84%E5%88%97%E8%A1%A8%E6%9B%B4%E9%9D%A0%E8%BF%91%E8%BE%B9%E6%A1%86%EF%BC%9F.png)
我想制作一个表格,并在单元格中包含一个列表。
经过一番学习,我编写了这样的代码。
\documentclass{article}
\usepackage{array}
\usepackage{enumitem}
\begin{document}
\begin{table}
\centering
\setlist[itemize]{noitemsep}
\begin{tabular}{m{2cm}<{\raggedright}|m{2cm}<{\raggedright}}\hline
a & \begin{itemize}
\item B
\item C
\item D
\end{itemize}\\\hline
e & \begin{itemize}
\item F
\item G
\item H
\end{itemize}\\\hline
\end{tabular}
\end{table}
\end{document}
它看起来像这样输出。
这个表格已经足够好了。但让我不满意的是列表似乎不是垂直居中,而是有点向顶部倾斜。而且有些空间被浪费了。我想让列表更靠近边框。对于第一个问题,我可以使用让列表\setlist[itemize]{noitemsep,topsep=xpt}
更居中,但这会使空白更多。那么我怎样才能删除列表上方和下方的空白,同时仍使其他单元格的内容垂直居中呢?
答案1
\documentclass{article}
\usepackage{array}
\usepackage{enumitem}
\newlist{tabular-list}{itemize}{1}
\setlist[tabular-list]
{
nosep,
topsep = 2pt ,
partopsep = 0pt ,
leftmargin = 1em ,
label = $\bullet$ ,
after = \vspace{-\baselineskip}
}
\begin{document}
\begin{table}
\centering
\setlist[itemize]{noitemsep}
\begin{tabular}{m{2cm}|m{2cm}}\hline
a & \begin{tabular-list}
\item B
\item C
\item D
\end{tabular-list}\\\hline
e & \begin{tabular-list}
\item F
\item G
\item H
\end{tabular-list}\\\hline
\end{tabular}
\end{table}
\end{document}
答案2
使用tabularray
包装很简单:
\documentclass{article}
\usepackage{tabularray}
\usepackage{enumitem}
\begin{document}
\begin{table}[ht]
\begin{tblr}{hlines, vline{2},
colspec = { *{2}{Q[l, m, wd=22mm]} },
stretch = -1,%<--- remove extra space above and below lists
% with nosep option; doc p.51 tabularray
rowsep = 3pt
}
a & \begin{itemize}[nosep, leftmargin=*]
\item B
\item C
\item D
\end{itemize} \\
e & \begin{itemize}[nosep, leftmargin=*]
\item F
\item G
\item C
\end{itemize} \\
\end{tblr}
\end{table}
\end{document}
附录}:
在tabular
表格中您还可以获得期望的结果:
\documentclass{article}
\usepackage{array}
\usepackage{enumitem}
\AtBeginEnvironment{table}{% global settings for itemize in table floats
\setlist[itemize]{nosep,
leftmargin=*,
label=\textbullet,
before={\begin{minipage}[t]{\hsize}},%https://tex.stackexchange.com/questions/531952/
after ={\end{minipage}}
}
}% end of \AtBeginEnvironment
\begin{document}
\begin{table}[ht]
\begin{tabular}{m{22mm}|m{22mm}}
\hline
a & \begin{itemize}
\item B
\item C
\item D
\end{itemize} \\
\hline
e & \begin{itemize}
\item F
\item G
\item C
\end{itemize} \\
\hline
\end{tabular}
\end{table}
\end{document}
结果与以前类似