![删除表格环境中“itemize”列表上方的空间](https://linux22.com/image/410092/%E5%88%A0%E9%99%A4%E8%A1%A8%E6%A0%BC%E7%8E%AF%E5%A2%83%E4%B8%AD%E2%80%9Citemize%E2%80%9D%E5%88%97%E8%A1%A8%E4%B8%8A%E6%96%B9%E7%9A%84%E7%A9%BA%E9%97%B4.png)
我想itemize
在table
-environment 中包含一个 -list,以紧凑的方式节省空间。但是,在表格中时,列表上方有一个很大的间隙,但在普通文本中的段落之后则没有:
\documentclass[12pt]{scrbook}
%\usepackage[autooneside=false, headsepline, plainheadsepline, automark]{scrlayer-scrpage}
\linespread{1}
\usepackage{paralist}
\usepackage{enumitem} % enumerate?
\usepackage{tabularx}
\begin{document}
some text
\begin{compactitem} \item point1 \item point2 \end{compactitem}
\begin{table}[ht!]
\setlength{\leftmargini}{7pt}
\begin{tabularx}{\linewidth}{l | X | X }\hline \hline
& ECB& FED\\ \hline
M1 & \begin{itemize}[noitemsep,topsep=0pt] \item point1 \item point2 \end{itemize}
& \begin{compactitem} \item point1 \item point2 \end{compactitem}
\end{tabularx}
\end{table}
\end{document}
如您所见,我测试了compactitem
fromparalist
包以及itemize
fromenumitem
及其选项[noitemsep,topsep=0pt]
。它们产生的高度略有不同。这在普通段落之间有效,但在表格中无效。我希望“point1”与“M1”处于同一水平。
这个,在换行时可以看到轻微的缩进(“Assets”这个词比“during”这个词更靠右一点),是
\begin{minipage}[t]{\hsize}\begin{compactitem}
\item Assets can be \textbf{difficult to value} during periods of hyperinflation
\end{compactitem}\end{minipage}
比较两种提出的解决方案:下面的 MWE 直接比较了这两种解决方案,分别定义了新的环境,名为compactitemt
和compactitemm
。
\documentclass%[12pt,abstracton,titlepage,parskip=false, no, no,twoside=true,open=right]
{scrbook}
%\usepackage[autooneside=false, headsepline, plainheadsepline, automark]{scrlayer-scrpage}
%\usepackage{listings}
\usepackage{paralist}
%\usepackage{newtxtext}%antpolt mathpazo %\usepackage{mathptmx} %\usepackage{newtxtext}
\usepackage{enumitem} % enumerate?
\newenvironment{compactitemm}{\begin{minipage}[t]{\hsize}\begin{compactitem}}{\end{compactitem}\end{minipage}} %this solution requires enumitem package
\newenvironment{compactitemt}{\begin{itemize}[nosep, wide=0pt,
%left=0pt %for neat, wide=0pt %for flush left to save space
before={\begin{minipage}[t]{\hsize}},
after ={\end{minipage}} ] }{ \end{itemize} }
\renewcommand{\familydefault}{\sfdefault}
\usepackage{tabularx}
\begin{document}
\begin{table}
\setlength{\leftmargini}{7pt} %this impacts the left margin of itemize package, i.e. ``stuff 2'' in table. 7pt aligns with the other solution.
\begin{tabularx}{\linewidth}{X | X }\hline
\multicolumn{2}{c}{stuff 1}\\ \hline
\begin{compactitemt}
\item x This line should break and we see where the next begins
\item y
\item z
\end{compactitemt}
& \begin{compactitemt}
\item a This line should break and we see where the next begins
\item b
\end{compactitemt}\\ \hline
\multicolumn{2}{c}{stuff 2}\\ \hline
\begin{compactitemm}
\item x This line should break and we see where the next begins
\item y
\item z This line should break and we see where the next begins
\end{compactitemm}
& \begin{compactitemm}
\item a This line should break and we see where the next begins
\item b
\end{compactitemm}\\ \hline
\end{tabularx}
\caption{MWE}
\end{table}
\end{document}
答案1
- 如果您使用
compactitem
环境,则需要将其括在\begin{minipage}[t]{\hsize}
and\end{minipage}
语句中。 如果您
itemize
与enumitem
包一起使用环境,则可以添加语句before={\begin{minipage}[t]{\hsize}}, after ={\end{minipage}}
添加到随附的选项列表中
\begin{itemize}
。
\documentclass[12pt]{scrbook}
\usepackage{paralist,enumitem,tabularx}
\begin{document}
\begin{table}[ht!]
\setlength\extrarowheight{2pt} % optional, for a slighly more open "look"
\setlength{\leftmargini}{7pt}
\begin{tabularx}{\linewidth}{l | X | X }
\hline \hline
& ECB & FED\\
\hline
M1 & \begin{itemize}[nosep, left=0pt,
before={\begin{minipage}[t]{\hsize}},
after ={\end{minipage}} ]
\item point1
\item point2
\end{itemize}
& \begin{minipage}[t]{\hsize}
\begin{compactitem}
\item point1
\item point2
\end{compactitem}
\end{minipage}\\
\hline
\end{tabularx}
\end{table}
\end{document}
答案2
对于上面的空间,让 LaTeX 相信单元格是小页面就足够了。为此,我借用了\compress
此网站问题答案中的一个命令。对于下面的空间,您必须在末尾添加一个负数 \baselineskip。
\documentclass[12pt]{scrbook}
%\usepackage[autooneside=false, headsepline, plainheadsepline, automark]{scrlayer-scrpage}
\linespread{1}
\usepackage{paralist}
\usepackage{enumitem} % enumerate?
\usepackage{tabularx}
\makeatletter
\newcommand{\compress}{\@minipagetrue}
\makeatother
\begin{document}
some text
\begin{compactitem} \item point1 \item point2 \end{compactitem}
\begin{table}[ht!]
\setlength{\leftmargini}{7pt}
\begin{tabularx}{\linewidth}{l | >{\compress}X |>{\compress} X }\hline \hline
& ECB& FED\\ \hline
M1 & \begin{itemize}[nosep, after=\vspace{-\baselineskip}] \item point1 \item point2 \end{itemize}\\
\hline
& & \begin{compactitem} \item point1 \item point2 \end{compactitem}\leavevmode\vspace*{-\baselineskip}
\\
\hline
\end{tabularx}
\end{table}
\end{document}