0

Is there a command that allows you to import only a few items from an enumerate environment?

\documentclass{article}
\usepackage{enumitem}

\begin{document}


\begin{enumerate}
\item A
\item B
\item C
\item D
\end{enumerate}


Desired output :
% with some commande such that : \just{1,2,4}
\begin{enumerate}
\item A
\item B
\item D
\end{enumerate}

\end{document}

enter image description here

1

1 Answer 1

1

MODIFIED after the comment. This is my first program in expl3, we should be able to do much better.

    \documentclass{article}        
    \ExplSyntaxOn
    \newcommand{\theelement}[1]{
        % outputs the #1-th token in \l_element_group_seq
        \seq_item:Nn \l_element_group_seq {#1}
    }
    \newcommand{\thejust}[1]{
        % outputs the #1-th token in \l_just_tl
        \clist_item:Nn \l_just_tl {#1}
    }

    \clist_new:N \l_just_tl
    \newcommand{\Ljust}[1]{
        \clist_set:Nn \l_just_tl {#1}
    }
    

    \seq_new:N \l_element_group_seq
    \NewDocumentCommand\just{ O{\item} m }
    % #1 is \item 
    % #2 the list of items in enumerate
    {
        %
        \seq_set_split:Nnn \l_element_group_seq {#1} {#2}
        \seq_remove_all:Nn \l_element_group_seq {}% remove the empty before the first item
        \clist_if_empty:NTF \l_just_tl 
        {
        % if Ljust is empty
        \int_step_inline:nn {\seq_count:N \l_element_group_seq}
        {%
            \item \theelement{##1}}% all item
        }
        { 
        \int_step_inline:nn {\clist_count:N \l_just_tl}
        {%
            \item \theelement{\thejust{##1}}% item of Ljust
        }
        }
    }
    
    \ExplSyntaxOff
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{document}
    \Ljust{1,2,4}

    Many items
    \begin{enumerate}
    \just{%
    \item text A
    \item $5\times3$
    \item text C
    \item text D
    }%
    \end{enumerate}

    \Ljust{4,2,1,3}

    Some text in 'not order"
    \begin{enumerate}
    \just{%
    \item text A
    \item $5\times3$
    \item text C
    \item text D
    }%
    \end{enumerate}

    \Ljust{}

    All items in the order
    \begin{enumerate}
    \just{%<--- you can also comment
    \item text A
    \item $5\times3$
    \item text C
    \item text D
    }%<--- you can also comment
    \end{enumerate}
    \end{document}      
8
  • Thanks for this proposition but storing item contents before the enumerate environment is too complicated to set up.
    – jowe_19
    May 19, 2022 at 16:37
  • New (latest) version.
    – pascal974
    May 21, 2022 at 11:54
  • Thanks, but is it mandatory to define the enumerate in the preamble? My goal is to use this to import some questions only from an exercise (so defined in the body of the tex document)
    – jowe_19
    May 22, 2022 at 16:14
  • A new latest version
    – pascal974
    May 23, 2022 at 11:11
  • Thanks again, it's almost perfect :) I'd like to leave the enumerate command as it is, and put just before the \begin{enuemerate} (like in the MWE). Indeed, I would need to import sometimes all the questions of an exercise (if the just command is missing), sometimes only some questions.
    – jowe_19
    May 23, 2022 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .