While writing macros to manipulate the items of a list with a
\@for
loop, I have found that braced items behave differently depending on whether they have a leading space or not:
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\makeatletter
\def\uselist#1{%
\@for\temp:=#1\do{\meaning\temp\par}}
\verb|\listwithcommas{Lima,Alpha,{Delta,Oscar},Tango,{Whisky,Echo,Romeo},Xray}|
\def\listwithcommas{Lima,Alpha,{Delta,Oscar},Tango,{Whisky,Echo,Romeo},Xray}
\uselist{\listwithcommas}
\verb|\listwithcommasandspaces{ {Lima}, Alpha, {Delta,Oscar}, Tango, {Whisky,Echo,Romeo}, Xray}|
\def\listwithcommasandspaces{ {Lima}, Alpha, {Delta,Oscar}, Tango, {Whisky,Echo,Romeo}, Xray}
\uselist{\listwithcommasandspaces}
\end{document}
I would like to maintain the braces, as when there is a space before the item, after applying the macros.
Is it possible to do that inside the \@for
loop?
The intention is that, however the list is provided (through a macro), the output list will maintain the braces.
That is, both \listwithcommas
and \listwithcommasandspaces
should give the second output.
Another question is why the behaviour is different when there are spaces or not.
EDIT
Here is an example to clarify the intention.
Suppose I have a macro \subtractlist
and that it is used two times.
First I subtract Lima
from the list and then {Delta,Oscar}
from the resulting list.
Since I have lost the braces after the first use, the second one will not work.
I can enclose all the items of the new formed list in braces.
In that case, it will work with \listwithcommas
but not with \listwithcommasandspaces
, because there are double braces and spaces.
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\makeatletter
\def\listwithcommas{{Lima},Alpha,{Delta,Oscar},Tango,{Whisky,Echo,Romeo},Xray}
\def\listwithcommasandspaces{ {Lima}, Alpha, {Delta,Oscar}, Tango, {Whisky,Echo,Romeo}, Xray}
\newif\if@isinlist
\def\subtractlist#1#2{% #1:original list, #2:remove list
\gdef\@subtractlist{}%
\@for\@tempa:=#1\do{%
\@isinlistfalse%
\@for\@tempb:=#2\do{\ifx\@tempa\@tempb\@isinlisttrue\fi}%
\if@isinlist%
\else%
\ifx\@subtractlist\empty%
\expandafter\gdef%
\expandafter\@subtractlist%
% \expandafter{\@tempa}%
%%% extra pair of braces added
\expandafter{\expandafter{\@tempa}}%
\else%
\expandafter\g@addto@macro%
\expandafter\@subtractlist%
% \expandafter{\expandafter,\@tempa}%
%%% extra pair of braces added
\expandafter{\expandafter,\expandafter{\@tempa}}%
\fi%
\fi%
}%
\let\currentlist\@subtractlist}
\def\removelista{Bravo,Lima}
\def\removelistb{Bravo,{Delta,Oscar}}
\subtractlist{\listwithcommas}{\removelista}
\verb|subtract Lima:| \meaning\currentlist
\subtractlist{\currentlist}{\removelistb}
\verb|subtract {Delta,Oscar}:|\meaning\currentlist
\bigskip
\subtractlist{\listwithcommasandspaces}{\removelista}
\verb|subtract Lima:| \meaning\currentlist
\subtractlist{\currentlist}{\removelistb}
\verb|subtract {Delta,Oscar}:|\meaning\currentlist
\end{document}
Without extra braces:
With extra braces: