Quantcast
Channel: List parsing with special input/optional arguments - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 5

List parsing with special input/optional arguments

$
0
0

I’d like to parse a list where the elements can be given as either [<pre>]<text>[<post>], where <pre> and <post> are optional, or (<special text>). I found a solution where everything works if <text> is a single token but not if it’s a word.

So an input like

\mylist{1,[a]2,3[b],[c]4[d],(5)}

I get and desire an output like

right result

but with

\mylist{111,[a]222,333[b],[c]444[d],(555)}

I get this

wrong result

while desiring this output

desired result

I also tried to use {<text>} instead of <text>, i.e. put the element in curly braces, but it doesn’t help in all cases. Furthermore I’d prefer to not use braces do delimit the element.

The separator printing doesn’t work well too.

Here’s what I’ve got so far

\documentclass[parskip=full]{scrartcl}\makeatletter\usepackage{etoolbox,xparse}\def\cflist@printsep{%   \ifcflist@sep\quad$\bullet$\quad\fi}\def\cflist@none#1{%   \@ifnextchar[{\cflist@printsep[#1]\cflist@last}{%      \cflist@printsep      [#1]%   }%}\def\cflist@first[#1]#2{%   \@ifnextchar[{\cflist@printsep\cflist@both[#1]{#2}}{\cflist@printsep${}_{#1}$[#2]}%}\def\cflist@last#1[#2]{%   #1${}_{#2}$%}\def\cflist@both[#1]#2[#3]{%   ${}_{#1}$[#2]${}_{#3}$%}\def\cflist@extra(#1){\cflist@printsep[#1]${}_E$}\newcounter{cflist@cnt}\newif\ifcflist@sep\def\cflist@do#1{%   \@ifnextchar({%      \cflist@extra%   }{%      \@ifnextchar[{\cflist@first}{%         \cflist@none%      }%   }#1%   \stepcounter{cflist@cnt}%   \ifnum\value{cflist@cnt}=0\relax\else      \cflist@septrue   \fi}\DeclareListParser{\cflist@parser}{,}\newcommand{\mylist}[1]{%   \let\do\cflist@do   \setcounter{cflist@cnt}{0}%   \cflist@sepfalse   \cflist@parser{#1}%}\makeatother\begin{document}Single element tokes work fine:\\\mylist{1,[a]2,3[b],[c]4[d],(5)}Multiple element tokes don't work:\\\mylist{111,[a]222,333[b],[c]444[d],(555)}\\\mylist{aaa,[a]bbb,ccc[b],[c]ddd[d],(eee)}Multiple element tokens in curly braces don't work either:\\\mylist{{111},[a]{222},{333}[b],[c]{444}[d],(555)}\\\mylist{{aaa},[a]{bbb},{ccc}[b],[c]{ddd}[d],(eee)}Desired result for multiple element tokes:\\111\quad$\bullet$\quad${}_a$222\quad$\bullet$\quad333${}_b$\quad$\bullet$\quad${}_c$444${}_d$\quad$\bullet$\quad555${}_E$\end{document}

The output format is a placeholder for other formatting macros, but I guess it doesn’t affect the problem and make the code a little shorter if I use the dummies here.

Update

I forgot \cflist@sepfalse which causes the superfluous sep at the begin of all lists after the first one

Updated 2

The real application is to send the list to \cite macros of biblatex, so the list

aaa,[a]bbb,ccc[b],[c]ddd[d],(eee)

should be processed as

\cite{aaa}, \cite[][a]{bbb}, \cite[b]{ccc}, \cite[c][d]{ddd}, eee

The (eee) part should bypass the \cite mechanism and printed as is without the parentheses.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images