1

I'm trying to create a macro where the optional item emboldens an item in the list. I can process the list using the expl3/xparse package and can compare the two strings but I'm having trouble doing both simultaneously. Can one return compare as an encapsulated macro which accepts one argument or can one extend ProcessList to handle two arguments ? I have read other posts on here but can't seem to find anything that matches this use case.

\documentclass{article}
\ExplSyntaxOn
\newcommand{\compare}[2]{\ifthenelse{\equal{#1}{#2}}{\textbf{#1}}{\emph{#2}}}
\NewDocumentCommand\within{O{} >{\SplitList{,}}m}{%
 \def\compari{\compare{#1}{##2}}
 \ProcessList{#2}{\compari}%
}
\ExplSyntaxOff

\begin{document}
\compare{apples}{bananas}
\compare{apples}{apples}
\within{apples}{apples,bananas}
\end{document}

Expected output

bananas

apples

apples, bananas

Carel
  • 988

1 Answers1

1

The following solution uses a \@for loop, which expects a comma separated list. There is still a bug, for this code breaks down if the list contains spaces.

\documentclass{article}

\makeatletter
\newcommand*{\within}[2]{%
\def\@tempa{#1}%
\def\separator{\def\separator{,\space}}% to place the comma in the printed list only after the first iteration
\@for\x:=#2\do{\separator\ifx\@tempa\x\textbf{\x}\else\emph{\x}\fi}%
}
\makeatother

\begin{document}

\within{apples}{apples,bananas,pears,apples,strawberries} % the apples are bold (twice)

\within{pears}{apples,bananas,pears,apples,strawberries} % the pears are bold

\end{document}
campa
  • 31,130
  • Might I ask where @for is documented ? The @ commands confuse me to no end as I'm never sure I'm clobbering some other macro or whether I'm safely scoped to some local space. (I'll mask this as this as answered, until some one "one ups" you :D ) – Carel Aug 28 '15 at 16:46
  • I'm pretty sure that both \@for and \@tfor are described in Kopka&Daly. Can't give exact references right now. Like all internal macros they might change, although that's quite unlikely in this case. There is no danger in using them but you better not redefine them ;-) – campa Aug 28 '15 at 18:52
  • texdoc source2e will open the pdf file containing the source code of the latex format. @for source code is found in my copy on page 51, corresponding to File f: ltcntrl.dtx Date: 2014/04/21 Version v1.0h. There is also \@tfor. –  Aug 31 '15 at 15:24