2

I am currently using enumitem and made an environment for my lists in a document. It is the following:

\newenvironment{alternativas}[1][0]
{
    \begin{enumerate}[label={\Alph*)}, itemsep=#1cm, topsep = 0.5cm, wide=0pt,labelwidth=.5cm,leftmargin=!]
}
{
    \end{enumerate}
}

(though I don't think it matters that much for the question)

and I've been searching for a way to auto-adjust itemsep to have equal space between all items. This is what I don't want to happen:

Because of the fraction using more height, the spacing between A-B-C is different from the spacing between C-D-E, and it looks odd for me. My current fix is to use the optional parameter in the environment to manually adjust the itemsep parameter and get it to look like this:

However, it is slow to manually adjust and I need some less-experienced people to use this environment. Is there a way for itemsep to be automatically set to the biggest separation between items? I searched a lot and couldn't find it (maybe my bad english didn't help). Thanks!

New contributor
Blaz Korecic is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Welcome! Will the alternatives be just a number or expression? Or are you planning also longer text? – egreg Mar 26 at 9:29
  • Thanks :) They will be numbers, fractions, and short text (at most 4-5 words, never wider than one line) – Blaz Korecic Mar 26 at 9:35
2

My proposed solution is to measure the height and depth of each item, so to get the maximum height and depth. If necessary, the baseline skip is set to the maximum height plus the maximum depth plus 2pt.

\documentclass{article}
\usepackage{xparse,enumitem,amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\alternatives}{m}
 {
  \blaz_alternativas:n { #1 }
 }

\seq_new:N \l__blaz_alternativas_seq
\box_new:N \l__blaz_alternativas_item_box
\dim_new:N \l__blaz_alternativas_height_dim
\dim_new:N \l__blaz_alternativas_depth_dim

\cs_new_protected:Nn \blaz_alternativas:n
 {
  % first populate the sequence of items
  \seq_set_split:Nnn \l__blaz_alternativas_seq { \\ } { #1 }
  % now measure the heights and depths
  \dim_zero:N \l__blaz_alternativas_height_dim
  \dim_zero:N \l__blaz_alternativas_depth_dim
  \seq_indexed_map_inline:Nn \l__blaz_alternativas_seq
   {
    % set the box to the current item
    \hbox_set:Nn \l__blaz_alternativas_item_box { ##2 }
    % measure the height if not the first item
    \int_compare:nT { ##1 > 1 }
     {
      \dim_set:Nn \l__blaz_alternativas_height_dim
       {
        \dim_max:nn { \l__blaz_alternativas_height_dim }
                    { \box_ht:N \l__blaz_alternativas_item_box }
       }
     }
    % measure the depth if not the last item
    \int_compare:nT { ##1 < \seq_count:N \l__blaz_alternativas_seq }
     {
      \dim_set:Nn \l__blaz_alternativas_depth_dim
       {
        \dim_max:nn { \l__blaz_alternativas_depth_dim }
                    { \box_dp:N \l__blaz_alternativas_item_box }
       }
     }
   }
   \begin{enumerate}[label={\Alph*)},wide=0pt,labelwidth=.5cm,leftmargin=!,itemsep=0pt]
   % set the baselineskip
   \skip_set:Nn \baselineskip
    {
     \dim_max:nn { \baselineskip }
                 { \l__blaz_alternativas_height_dim + \l__blaz_alternativas_depth_dim }
     + 2pt
    }
   % deliver the items
   \seq_map_inline:Nn \l__blaz_alternativas_seq { \item ##1 }
   \end{enumerate}
 }

\ExplSyntaxOff

\begin{document}

\alternatives{ $1$ \\ $2$ \\ $3$ \\ $4$ \\ $5$ }

\alternatives{
  $1$ \\
  $\dfrac{3}{2}$ \\
  $\dfrac{3}{4}$ \\
  $4$ \\
  $5$
}

\end{document}

enter image description here

Here's a version that adds \hphantom{$-$} in front of each item that doesn't start with $-, provided at least one does.

\documentclass{article}
\usepackage{xparse,enumitem,amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\alternatives}{m}
 {
  \blaz_alternativas:n { #1 }
 }

\seq_new:N \l__blaz_alternativas_seq
\box_new:N \l__blaz_alternativas_item_box
\dim_new:N \l__blaz_alternativas_height_dim
\dim_new:N \l__blaz_alternativas_depth_dim

\cs_new_protected:Nn \blaz_alternativas:n
 {
  % first populate the sequence of items
  \seq_set_split:Nnn \l__blaz_alternativas_seq { \\ } { #1 }
  % now measure the heights and depths
  \dim_zero:N \l__blaz_alternativas_height_dim
  \dim_zero:N \l__blaz_alternativas_depth_dim
  \seq_indexed_map_inline:Nn \l__blaz_alternativas_seq
   {
    % set the box to the current item
    \hbox_set:Nn \l__blaz_alternativas_item_box { ##2 }
    % measure the height if not the first item
    \int_compare:nT { ##1 > 1 }
     {
      \dim_set:Nn \l__blaz_alternativas_height_dim
       {
        \dim_max:nn { \l__blaz_alternativas_height_dim }
                    { \box_ht:N \l__blaz_alternativas_item_box }
       }
     }
    % measure the depth if not the last item
    \int_compare:nT { ##1 < \seq_count:N \l__blaz_alternativas_seq }
     {
      \dim_set:Nn \l__blaz_alternativas_depth_dim
       {
        \dim_max:nn { \l__blaz_alternativas_depth_dim }
                    { \box_dp:N \l__blaz_alternativas_item_box }
       }
     }
   }
   \begin{enumerate}[label={\Alph*)},wide=0pt,labelwidth=.5cm,leftmargin=!,itemsep=0pt]
   % set the baselineskip
   \skip_set:Nn \baselineskip
    {
     \dim_max:nn { \baselineskip }
                 { \l__blaz_alternativas_height_dim + \l__blaz_alternativas_depth_dim }
     + 2pt
    }
   % check whether some items start with $-
   \cs_set_protected:Nn \__blaz_alternativas_minus:n {##1} % do nothing by default
   \seq_map_inline:Nn \l__blaz_alternativas_seq
    {
     \regex_match:nnT { \A \$\- } { ##1 }
      {
       \seq_map_break:n
        {
         \cs_set_eq:NN \__blaz_alternativas_minus:n \__blaz_alternativas_addminus:n
        }
      }
    }
   % deliver the items
   \seq_map_inline:Nn \l__blaz_alternativas_seq { \item \__blaz_alternativas_minus:n {##1} }
   \end{enumerate}
 }
\cs_new_protected:Nn \__blaz_alternativas_addminus:n
 {
  \regex_match:nnF { \A \$\- } { #1 } { \hphantom{$-$} } #1
 }

\ExplSyntaxOff

\begin{document}

\alternatives{ $1$ \\ $2$ \\ $3$ \\ $4$ \\ $5$ }

\alternatives{
  $-1$ \\
  $-\dfrac{3}{2}$ \\
  $\dfrac{3}{4}$ \\
  $4$ \\
  $5$
}

\end{document}

enter image description here

|improve this answer|||||
  • Thanks! I was hoping for an answer that I could integrate with the accepted answer of this question. However, this answers my question so I'm marking it as accepted. – Blaz Korecic Mar 26 at 22:59
  • @BlazKorecic I wouldn't do that, but you can find a version that does it. – egreg Mar 26 at 23:19
  • The fractions look like they are preceded by just a smidge more space than the "bare" digits. To deliver what I think has been asked for, I would expect the "4"s in the last list to line up, and they don't. – barbara beeton Mar 26 at 23:43
  • @barbarabeeton Ask DEK why he made them inner atoms. I guess a minus sign in front of them can be one of the reasons. Other long and expensive tests would be needed here. – egreg Mar 26 at 23:45
1

Here is an entirely different approach based on a tabular. I have used a sligthly adapted version of the automaic table row numbering in combination with the cellspace package for the spacing around the entries. Since I right aligned the column, this also seems to solve your follow-up question:

enter image description here

\documentclass{article}
\usepackage[column=0]{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{\cellspacetoplimit}

\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\Alph{magicrownumbers}}
\usepackage{amsmath}

\newenvironment{myalternatives}[1][0]
{\begin{tabular}{@{\makebox[3em][r]{\rownumber)~}}  >{$}0r<{$}}}
{\end{tabular}}

\begin{document}

\begin{myalternatives}
  1 \\
  \dfrac{3}{2} \\
  \dfrac{3}{4} \\
  4 \\
  5 \\
  -6\\
\end{myalternatives}

\end{document} 
|improve this answer|||||
  • Thank you very much for your answer! Though, I think it doesn't solve my problem as in your image the item separation is not equal between all items, in your image the separation between D-E-F is less than the separation between A-B-C – Blaz Korecic Mar 26 at 22:09

Your Answer

Blaz Korecic is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.