我使用description
在中定义的环境枚举项。首先我的代码是:
\documentclass{article}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{environ}
\usepackage{lipsum}
\newlist{descleft}{description}{1}
\setlist[descleft]{align=parleft, leftmargin=!, labelwidth=4cm, labelsep=1.5em, font=\normalfont\sffamily}
\newlength\widest
\makeatletter
\NewEnviron{descleftind}{%
\vbox{%
\global\widest=0pt%
\def\item[##1]{%
\settowidth\@tempdima{\textsf{\begin{array}[t]{@{}l@{}}##1\end{array}}}%
% \settowidth\@tempdima{\textsf{##1}}%
\ifdim\@tempdima>\widest\relax
\global\widest=\@tempdima\fi%
}%
\setbox0=\vbox{\BODY}%
}
\begin{description}[
align=parleft,
labelsep=1.5em,
leftmargin=\dimexpr\widest+2em+0.05\columnwidth\relax,
labelindent=0.05\columnwidth,
labelwidth=\dimexpr\widest+0.5em\relax,
% topsep=0pt,
% parsep=0pt,
% partopsep=0pt,
% itemsep=0pt,
font=\normalfont\sffamily]
\BODY
\end{description}%
}
\makeatother
\begin{document}
\subsubsection{Test 1}
\begin{descleft}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleft}
\subsubsection{Test 2}
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleftind}
\subsubsection{Test 3}
Test line
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleftind}
\end{document}
descleft
环境是通过使用提供的宏来定义的枚举项,而在宏descleftind
中定义\NewEnviron
环境包。由于这两个定义在环境中都没有任何垂直间距选项description
,我猜结果至少在垂直间距上没有区别。部分包含whileTest 1
的结果并具有的结果。此时奇怪的是部分文本和列表之间有意外的垂直间距。但是,它们之间的文本不会影响间距,如所示。这段代码中发生了什么,我应该怎么做才能消除间距?我猜宏有关键。descleft
Test 2
Test 3
descleftind
Test 2
Test 3
\vbox
编辑:我目前正在使用一种临时方法,使用以下方法分配手动间距\vspace
:
\NewEnviron{descrightind}[1][0pt]{%
\vspace{#1}
\vbox{
\global\widest=0pt%
\def\item[##1]{%
\settowidth\@tempdima{\textsf{\begin{tabular}[t]{@{}l@{}}##1\end{tabular}}}%
\ifdim\@tempdima>\widest\relax
\global\widest=\@tempdima\fi%
}%
\setbox0=\vbox{\BODY}%
}
\begin{description}[
align=parright,
labelsep=1.5em,
leftmargin=\dimexpr\widest+2em+0.05\columnwidth\relax,
labelindent=0.05\columnwidth,
labelwidth=\dimexpr\widest+0.5em\relax,
font=\normalfont\sffamily]
\BODY
\end{description}%
}
例如,
\subsubsection{Test 3}
Test line
\begin{descleftind}[-2em]
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleftind}
答案1
您正在排版\vbox
。不要。
\documentclass{article}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{environ}
\usepackage{lipsum}
\newlist{descleft}{description}{1}
\setlist[descleft]{
align=parleft,
leftmargin=!,
labelwidth=4cm,
labelsep=1.5em,
font=\normalfont\sffamily
}
\newlength\widest
\makeatletter
\NewEnviron{descleftind}{%
\setbox0=\vbox{%
\global\widest=0pt%
\def\item[##1]{%
\settowidth\@tempdima{\textsf{\begin{tabular}{@{}l@{}}##1\end{tabular}}}%
% \settowidth\@tempdima{\textsf{##1}}%
\ifdim\@tempdima>\widest\relax
\global\widest=\@tempdima\fi%
}%
\BODY
}
\begin{description}[
align=parleft,
labelsep=1.5em,
leftmargin=\dimexpr\widest+2em+0.05\columnwidth\relax,
labelindent=0.05\columnwidth,
labelwidth=\dimexpr\widest+0.5em\relax,
% topsep=0pt,
% parsep=0pt,
% partopsep=0pt,
% itemsep=0pt,
font=\normalfont\sffamily]
\BODY
\end{description}%
}
\makeatother
\begin{document}
\subsubsection{Test 1}
\begin{descleft}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleft}
\subsubsection{Test 2}
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really \\ long label] \lipsum[1-7][3-4]
\end{descleftind}
\subsubsection{Test 3}
Test line
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleftind}
\end{document}
避免在一次性文件中排版主体的版本\vbox
:
\documentclass{article}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{lipsum}
\newlist{descleft}{description}{1}
\setlist[descleft]{
align=parleft,
leftmargin=!,
labelwidth=4cm,
labelsep=1.5em,
font=\normalfont\sffamily
}
\ExplSyntaxOn
\NewDocumentEnvironment{descleftind}{O{}+b}
{
\hermis_descleftind:nn { #1 } { #2 }
}
{}
\dim_new:N \l__hermis_descleftind_label_dim
\seq_new:N \l__hermis_descleftind_items_seq
\cs_new_protected:Nn \hermis_descleftind:nn
{
% let's measure the labels
\dim_zero:N \l__hermis_descleftind_label_dim
\seq_set_split:Nnn \l__hermis_descleftind_items_seq { \item } { #2 }
% the first item would be empty
\seq_pop_left:NN \l__hermis_descleftind_items_seq \l_tmpa_tl
% measure the widest label
\seq_map_function:NN \l__hermis_descleftind_items_seq \__hermis_descleftind_measure:n
% now typeset
\begin{description}[
align=parleft,
labelsep=1.5em,
leftmargin=\dim_eval:n { \l__hermis_descleftind_label_dim+2em+0.05\columnwidth },
labelindent=0.05\columnwidth,
labelwidth=\dim_eval:n { \l__hermis_descleftind_label_dim+0.5em },
% topsep=0pt,
% parsep=0pt,
% partopsep=0pt,
% itemsep=0pt,
font=\normalfont\sffamily,
#1 % you may want to override some parameters
]
#2
\end{description}
}
\cs_new_protected:Nn \__hermis_descleftind_measure:n
{
\__hermis_descleftind_measure:w #1 \q_stop
}
\cs_new_protected:Npn \__hermis_descleftind_measure:w [ #1 ] #2 \q_stop
{
\hbox_set:Nn \l_tmpa_box { \sffamily \begin{tabular}{@{}l@{}}#1\end{tabular} }
\dim_set:Nn \l__hermis_descleftind_label_dim
{
\dim_max:nn { \l__hermis_descleftind_label_dim } { \box_wd:N \l_tmpa_box }
}
}
\ExplSyntaxOff
\begin{document}
\subsubsection{Test 1}
\begin{descleft}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleft}
\subsubsection{Test 2}
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really \\ long label] \lipsum[1-7][3-4]
\end{descleftind}
\subsubsection{Test 3}
Test line
\begin{descleftind}
\item[Short] \lipsum[1-7][1-2]
\item[A really really long label] \lipsum[1-7][3-4]
\end{descleftind}
\end{document}