6

this is my first question, so any help is appreciated.

I want to achieve an indentation effect as shown in the image:

itemize approach

Which can be obtained as follows:

\begin{itemize}
  \item $a$
  \begin{itemize}
    \item $b1$
    \item $b2$
  \end{itemize}
  \item $c$
\end{itemize}

The point is that I want to create an xparse \DocumentCommand that receives the item arguments without $ signs and separated by the . character and recreates the previous itemize structure.

For example, lets say the named function is called \tab. Then,

\tab{a.\tab{b1.b2}.c}

should give the same result as above.

Hope you understand what I'm looking for.


Edit: Finally got it thanks to \@ifnextchar:

\usepackage{xparse}
\NewDocumentCommand{\tab}{>{\SplitList{.}}m}
{\begin{itemize}\ProcessList{#1}{\entry}\end{itemize}}
\makeatletter
\def\test{\@ifnextchar\tab{\relax}{\item $}}
\makeatother
\newcommand{\entry}[1]{\test#1 \ifmmode $ \fi }

Thanks for your help.

1
  • I think I have resolved the math mode issue. Apr 10, 2014 at 20:20

2 Answers 2

2

With just xparse it's too complicated. Using \@ifnextchar is possible, but a jump to expl3 is perhaps better:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\tab}{m}
 {
  % start an itemize (that also provides grouping for nested calls)
  \begin{itemize}
  \azpillaga_tab:n { #1 }
  % finish off
  \end{itemize}
 }

\seq_new:N \l__azpillaga_tab_input_seq

\cs_new_protected:Npn \azpillaga_tab:n #1
 {
  % split the input into components at the periods
  \seq_set_split:Nnn \l__azpillaga_tab_input_seq { . } { #1 }
  % process one item at a time
  \seq_map_inline:Nn \l__azpillaga_tab_input_seq
   {
    % check if the item starts with \tab
    \peek_meaning:NF \tab
     {% it doesn't: add \item and $
      \item $
     }
    % the item
    ##1
    % add $ if in \item
    \mode_if_math:T { $ }
   }
 }
\ExplSyntaxOff

\begin{document}
\section{Small}
\tab{a.\tab{b1.b2}.c}

\section{Big}
\tab{a.\tab{b1.\tab{b11.\tab{b112.b113}}.b2}.c.\tab{1.2}.d}

\end{document}

enter image description here

2

Here I make the . active inside \tab and restore it when I am done. Inside \tab, the . can become \item if the next token is a not a nested \tab and it becomes \relax if the next token is a nested \tab. I keep track of the nesting level with a simple counter, and only restore the catcode 12 definition of . when the nesting level reaches back to zero.

I have EDITED to allow switching between text mode and math mode processing.

In my RE-EDIT, I have resolved the issue of the stray $$ lines which popped up in math mode (requiring a countermanding \vspace), so that no \vspaces are presently required, and the spacing in math mode matches that of a true itemize environment.

In my MWE, I have placed text and math versions side-by-side in minipages for comparison.

\documentclass{article}
\newcounter{nestlevel}
\def\mathitemize{\def\DOL{$}}
\def\textitemize{\def\DOL{}}
\textitemize
\let\svdot.
\catcode`.=\active
\newcommand\tab{\stepcounter{nestlevel}\catcode`.=\active\tabhelper}
\newcommand\tabhelper[1]{%
  \def\tabdone{F}%
  \let.\dotparse%
  \begin{itemize}%
    \item\DOL#1\if F\tabdone\DOL\fi\gdef\tabdone{T}%
  \end{itemize}%
  \addtocounter{nestlevel}{-1}%
  \if0\thenestlevel\catcode`.=12\let.\svdot\fi%
}
\makeatletter
\def\dotparse{\if F\tabdone\DOL\fi\def\tabdone{F}\@ifnextchar\tab\relax\xitem}
\makeatother
\def\xitem{\item\DOL}
\catcode`.=12
\parskip 1em
\begin{document}
\begin{minipage}{.4\textwidth}
\textitemize\tab{a.\tab{b1.\tab{b11.\tab{b112.b113}}.b2}.c.\tab{1.2}.d}
\end{minipage}
\begin{minipage}{.4\textwidth}
\mathitemize\tab{a.\tab{b1.\tab{b11.\tab{b112.b113}}.b2}.c.\tab{1.2}.d}
\end{minipage}
\end{document}

enter image description here


I would add in passing that, as is, the OP has prohibited the use of the dot as part of his argument, since his requested syntax will interpret the . as a call for a new \item or nested itemize. If one really does need a dot as part of the data stream, it would make sense to choose a different character than . as the parsing separator, which is easy enough to accomplish by way of substitution in the MWE.

2
  • The extra lines issue is the actual reason that caused me to make this question. You made a hard work but the problem isn't still totally solved. If you make a 4 nestlevel call, such as \tab{a.\tab{b1.\tab{b11.\tab{b112.b113}}.b2}.c.\tab{1.2}.d} the linebreaking fails. Any possible workaround? I appreciate your effort. Apr 10, 2014 at 20:36
  • @MartinAzpillaga After a bit of struggle, the problem is resolved in math mode, as now shown in my MWE. Apr 11, 2014 at 13:10

You must log in to answer this question.

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