4

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

I've been trying to align the items ignoring the negative sign, if the item starts with one. Images will explain it a lot better than me:

enter image description here

enter image description here

I've searched for similar questions and I've only found people with this problem on a table, and solutions usign align environments and stuff like that. I even searched for something to solve this in TeX books (and accidentally learned a lot while doing that haha) but I haven't been successful.

Many solutions for these similar problems also make use of some manual adjustment, like using a \phantom in the items without a minus sign so that the spacing looks equal (I produced the wanted result using \hspace with a negative number). But I have been searching for a solution that automatizes this for me, because I share the documents with less experienced people and want it to be easy for them to use. Thank you very much!

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.
  • Wat should happen with two-digit numbers? – leandriis Mar 26 at 10:22
  • The same should happen with any-digit numbers, better if it works with fractions too. Align with the first non minus sign character – Blaz Korecic Mar 26 at 11:05
  • Are you sure you want a list for this? The advantages of a list is that the contents can break across pages and you can nest them. The main disadvantage is that the text after \item is NOT an argument and cannot be parsed easily. – John Kormylo Mar 26 at 13:46
  • I actually was looking for a way for my lists to not break across pages, so that may be really helpful too – Blaz Korecic Mar 26 at 22:49
5

What i do is to redefine \item inside the environment to examine the first token of \item (following an optional argument). If it is a - token, I \mathllap it and then continue, else I just typeset it directly.

\documentclass{article}
\usepackage{enumitem,mathtools}
\let\svitem\item
\newenvironment{alternativas}[1][0]
{
  \renewcommand\item[2][\relax]{%
    \ifx\relax##1\def\next{}\else\def\next{[##1]}\fi
    \ifx-##2\def\nnext{$\mathllap{##2}$}\else\def\nnext{##2}\fi
    \expandafter\svitem\next\nnext}
    \begin{enumerate}[label={\Alph*)}, itemsep=#1cm, topsep = 0.5cm, 
      wide=0pt, labelwidth=.5cm,leftmargin=!,labelsep=10pt]
}
{
    \end{enumerate}
}
\begin{document}
\begin{alternativas}
\item -1
\item 4
\item -5
\item 6
\end{alternativas}
\end{document}

enter image description here

ADDENDUM

OP clarified in comments that all numbers are entered in math mode (via leading $). However, not all items begin with math. So, some revisions were required:

\documentclass{article}
\usepackage{enumitem,mathtools}
\let\svitem\item
\newenvironment{alternativas}[1][0]
{
  \renewcommand\item[3][\relax]{%
    \ifx\relax##1\def\next{}\else\def\next{[##1]}\fi
    \ifx$##2\ifx-##3\def\nnext{##2\mathllap{##3}}\else\def\nnext{##2##3}\fi
      \else\def\nnext{##2##3}\fi
    \expandafter\svitem\next\nnext}
    \begin{enumerate}[label={\Alph*)}, itemsep=#1cm, topsep = 0.5cm, 
      wide=0pt, labelwidth=.5cm,leftmargin=!,labelsep=10pt]
}
{
    \end{enumerate}
}
\begin{document}
\begin{alternativas}
\item $-1$
\item $4$
\item $-\frac{5}{7}$
\item $\frac{6}{11}$
\item text
\end{alternativas}
\end{document}

enter image description here

|improve this answer|||||
  • Thanks a lot for you answer! I have a problem with it, though: when I insert numbers in math mode in an \item, it doesn't work (i.e \item $-13$ won't work) so if I need to use a fraction, it won't work because I have to put it in math mode – Blaz Korecic Mar 26 at 22:21
  • 1
    @BlazKorecic So, sometimes the \item may begin with a number, other times with a minus, and still other times with a $-? – Steven B. Segletes Mar 26 at 22:29
  • 1
    @BlazKorecic An obvious workaround on your part is to place the minus before you enter math mode. However, my prior comment is an effort to nail to all possible uses. If there is some regularity, I might be able to develop something that works. But you need to tell me all the ways it could be used. – Steven B. Segletes Mar 26 at 22:30
  • 1
    Sometimes there will be items without math mode. However, they will have no numbers and the mathllap will never be needed in that case – Blaz Korecic Mar 26 at 22:33
  • 1
    @BlazKorecic Please see ADDENDUM – Steven B. Segletes Mar 26 at 22:41
3

You can load mathtools (needless to load amsmath in this case) to use its \mathllap command and slightly increase \labelsep:

\documentclass{article}

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

\begin{document}

 \begin{alternativas}
    \item $\mathllap{-}1 $
    \item $ 4 $
    \item $ \mathllap{-}5 $
    \item $ 6 $
    \end{alternativas}

\end{document} 

enter image description here

|improve this answer|||||

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.