In simple cases, you can use LaTeX's \@removeelement
:
\documentclass{article}
\newcommand*{\citylist}{derby,leicester,nottingham}
\makeatletter
\@expandtwoargs\@removeelement{leicester}\citylist\citylist
\makeatother
\typeout{\citylist}
\begin{document}
\end{document}
Result:
derby,nottingham
Simple means, the list is normalized without optional spaces around the comma.
The list elements does not contain commas and macros.
A variant with package kvsetkeys
:
\documentclass{article}
\usepackage{kvsetkeys}
\makeatletter
\newcommand*{\addcity}[1]{%
\ifx\citylist\@empty\else\g@addto@macro\citylist{,}\fi
\g@addto@macro\citylist{#1}
}
\newcommand*{\removecity}[1]{%
\def\unwanted@city{#1}%
\edef\citylist{\expandafter}%
\expandafter\comma@parse\expandafter{\citylist}\@removecity
}
\newcommand*{\@removecity}[1]{%
\def\param@city{#1}%
\ifx\param@city\unwanted@city
\else
\addcity{#1}%
\fi
}
\makeatother
\newcommand*{\citylist}{derby,leicester,nottingham}
\removecity{leicester}
\typeout{\citylist}
\begin{document}
\end{document}
Then spaces do not disturb, e.g.:
\newcommand*{\citylist}{derby, leicester, nottingham}
The city names may contain commands and fragile macros, they are not
expanded.
With the following definition for \addcity
, the city names might even contain commas:
\newcommand*{\addcity}[1]{%
\ifx\citylist\@empty\else\g@addto@macro\citylist{,}\fi
\g@addto@macro\citylist{{#1}}
}
Example for a list:
{derby},{leicester},{berlin, west},{berlin, east}