This question led to part of a new package:
menukeys
I build a command to iterate over a list with changable seperators and it works like expected but I wonder if there is a faster or more elegant way to do it?
\documentclass{article}
\makeatletter
\RequirePackage{etoolbox}
\RequirePackage{xparse}
\def\tw@first@element#1{\fbox{\strut#1 (first)}}
\def\tw@middle@element#1{\fbox{\strut#1 (mid)}}
\def\tw@last@element#1{\fbox{\strut#1 (last)}}
\def\tw@single@element#1{\fbox{\strut#1 (single)}}
\def\tw@seperator{\fbox{\strut sep}}
\def\tw@precode{\fbox{\strut pre}}
\def\tw@postcode{\fbox{\strut post}}
\newcounter{tw@list@i}
\newcounter{tw@list@last}
\def\tw@input@seperator{,}
\DeclareListParser{\tw@do@menu}{,}
\DeclareListParser{\tw@do@list@count}{,}
\NewDocumentCommand{\setmenuseperator}{v}{%
\let\tw@do@menu\relax%
\let\tw@do@list@count\relax%
\DeclareListParser{\tw@do@menu}{#1}%
\DeclareListParser{\tw@do@list@count}{#1}%
}
\newcommand{\menu}[1]{%
\setcounter{tw@list@i}{1}%
\setcounter{tw@list@last}{0}%
\renewcommand*{\do}[1]{\stepcounter{tw@list@last}}%
\tw@do@list@count{#1}%
\tw@precode%
\renewcommand*{\do}[1]{%
\ifnumequal{1}{\value{tw@list@last}}{%
\tw@single@element{##1}%
}{%
\ifnumequal{1}{\value{tw@list@i}}{%
\tw@first@element{##1}%
}{%
\ifnumequal{\value{tw@list@i}}{\value{tw@list@last}}{%
\tw@seperator\tw@last@element{##1}%
}{%
\tw@seperator\tw@middle@element{##1}%
}%
}%
}%
\stepcounter{tw@list@i}%
}%
\tw@do@menu{#1}%
\tw@postcode%
}
\makeatother
\setlength{\parskip}{1cm}
\begin{document}
\menu{1,2,3,4}
\menu{Single Element}
\menu{A,B,C,D,E}
\setmenuseperator{/}
\menu{C:/Nutzer und Einstellungen/Desktop/Test}
% doesn’t work yet
%\setmenuseperator{\}
%\menu{C:\Nutzer und Einstellungen\Desktop\Test}
\end{document}
The requirements for the \menu
command are the following
- Special treatment for single item lists
- Treat first and last list item other than the mid ones
- Insert a separator only between the elements
- add some code before and after the list
Extra
As you can imagine such commands should be used to format paths automatically, so it would be great if a \
could be recognized as list separator.