2

I use an description environment defined in enumitem. First, my code is:

\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 environment is defined by using the macro provided by enumitem, whereas descleftind is defined in \NewEnviron macro of the environ package. As both of the definitions do not have any vertical spacing option in the description environment, I guessed that the results would have no difference at least in vertical spacing. The section Test 1 contains the result of descleft while Test 2 and Test 3 have the results of descleftind. What is weird at this point is that Test 2 has unintended vertical spacing between the section text and the list. However, a text between these does not influence the spacing as shown in Test 3. What's happening in this code and what should I do to remove the spacing? I guess the \vbox macro has the key.

enter image description here

Edit: I am currently using a makeshift that assigns a manual spacing using \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}%
}

For example,

\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}
Hermis14
  • 423

1 Answers1

2

You are typesetting a \vbox. Don't.

\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}

enter image description here

A version that avoids typesetting the body in the disposable \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}

egreg
  • 1,121,712
  • Thank you very much :) The code is originated from Gonzalo Medina. I've been using it with some minor retouch not knowing the principle in it. But I am not really familiar with the syntax of your original code. Is it standard syntax supported by LaTeX? Would you recommend some material needed to follow your answer? – Hermis14 Oct 16 '21 at 22:52
  • In fact, you've answered my question before with the syntax egreg. But it didn't work with multiple errors on overleaf. That's why I didn't select your answer. Surprisingly, I noticed that it works really well in my TeXstudio. I don't know why there's a difference between overleaf and TeXstudio. – Hermis14 Oct 16 '21 at 23:04
  • Your original approach seems much better in that it allows Go-to-source functionality that TeXstudio provides because it does not involve environ package, whereas my code doesn't. – Hermis14 Oct 16 '21 at 23:15