3

I would like to underline even items or odd items in itemize environment. Is there a more convenient way than:

\newcommand{\uu}{\underline}
\begin{itemize}
\item \uu{some text}
\item some text
\item \uu{some text}
\item some text
\item \uu{some text}
\item some text
\end{itemize}
  • 1
    I think it would come at the cost of needing to wrap each entry in a macro ... so that probably isn't 'saving' you anything: not typing, and therefore certainly not time. (One obvious exception would be to use a font that is underlined by default and switch to and from that.) – jon Sep 30 '16 at 3:28
  • I agree with @jon. The problem is underline, not parity. See tex.stackexchange.com/questions/198484/… – Symbol 1 Sep 30 '16 at 4:17
6

Note that \item has be redefined to treat the text as an argument. Also this only supports one level.

\documentclass{article}

\newcommand{\myitem}[1]% text must be placed in braces
 {\olditem\stepcounter{enumi}%
  \ifodd\value{enumi}\underline{#1}
  \else{#1}\fi}

\newenvironment{myitemize}{\itemize
  \setcounter{enumi}{0}%
  \let\olditem=\item
  \let\item=\myitem}{\enditemize}
\begin{document}

\begin{myitemize}
\item{odd text}
\item{even text}
\end{myitemize}

\end{document}

demo

4

You can split the contents of an environment at every occurrence of \item which allows you to separate out the "arguments" for every list item. That way you can perform an action (conditionally) on an \item:

enter image description here

\documentclass{article}

% https://tex.stackexchange.com/a/127014/5764
\usepackage{xparse,environ,soul}

\ExplSyntaxOn
\NewEnviron{oddevenitemize}[1][]
 {
  \int_zero:N \l_odd_even
  \keys_set:nn { oddeven/itemize } { #1 }
  \seq_set_split:NnV \l_oddeven_itemize_input_seq { \item } \BODY
  \seq_pop_left:NN \l_oddeven_itemize_input_seq \l_tmpa_tl
  \tl_use:N \l_oddeven_itemize_pre_tl
  \seq_set_map:NNn
    \l_oddeven_itemize_output_seq
    \l_oddeven_itemize_input_seq
    { \exp_not:n { \__oddeven_itemize_do:n { ##1 } } }
  \seq_use:NV \l_oddeven_itemize_output_seq \l_oddeven_itemize_sep_tl
  \tl_use:N \l_oddeven_itemize_post_tl
 }

\seq_new:N \l_oddeven_itemize_input_seq
\seq_new:N \l_oddeven_itemize_output_seq
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\cs_generate_variant:Nn \seq_use:Nn { NV }
\int_new:N \l_odd_even

\keys_define:nn { oddeven/itemize }
 {
  pre  .tl_set:N = \l_oddeven_itemize_pre_tl,
  post .tl_set:N = \l_oddeven_itemize_post_tl,
  sep  .tl_set:N = \l_oddeven_itemize_sep_tl,
  action .code:n = \cs_set_eq:NN \__oddeven_itemize_do:n #1,
  action .initial:n = \use:n,
 }

\NewDocumentCommand{\normalitem}{m}{%
  \item #1
}
\NewDocumentCommand{\underlineodd}{m}{%
  \int_incr:N \l_odd_even
  \item
  \int_if_odd:nTF { \l_odd_even }
    {\ul{#1}}% Underline odd
    {#1}% Do nothing for even
}
\NewDocumentCommand{\underlineeven}{m}{%
  \int_incr:N \l_odd_even
  \item
  \int_if_odd:nTF { \l_odd_even }
    {#1}% Do nothing for odd
    {\ul{#1}}% Underline even
}
\ExplSyntaxOff

\begin{document}

\begin{oddevenitemize}[
  action=\underlineodd,
  pre=\begin{itemize},
  post=\end{itemize},
]
  \item First
  \item Second
  \item Third
  \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Sed semper eros sed quam ullamcorper iaculis tincidunt feugiat neque. 
    Donec ut risus at arcu tristique porttitor. Proin sollicitudin 
    sollicitudin aliquam. Mauris rutrum, erat et gravida ornare, odio 
    justo aliquet ipsum, at venenatis sem quam fermentum tortor. 
  \item Integer et ante non libero scelerisque hendrerit at nec dui. 
    Proin ante velit, finibus id tempor id, laoreet eu quam. 
    Suspendisse semper ex id magna luctus egestas. 
    Phasellus pharetra nibh bibendum felis commodo pulvinar.
  \item Last
\end{oddevenitemize}

\end{document}

The items are formatted using an action. In the above example, we use \underlineodd, but you can switch this to \underlineeven to underline even-numbered \items.

Underlining is done with support from soul to allow for line-breaks.

Reference:

Your Answer

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.