Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

It's late, so I may not be thinking about this clearly enough, but I could have sworn that there exists a macro (possibly not part of a package, but just something that I picked up somewhere along the way) that is able to take some sort of inline list separated by a certain character and replace that character with another string. For example, something like

\ThisImaginaryMacro{apples;oranges;peaches;pears}

would produce something like

apples $\cdot$ oranges $\cdot$ peaches $\cdot$ pears

because somewhere I've specified that semicolons should be replaced with $\cdot$.

I have been searching through all the .tex files I've made in recent memory, and I just can't seem to find this, and it's driving me nuts, so any help is greatly appreciated.

share|improve this question
    
Welcome to TeX.sx! –  Kurt Jan 18 '13 at 12:11
add comment

3 Answers

You can do it in many ways. For instance with xstring:

\usepackage{xstring}
\newcommand{\RealMacro}[1]{\StrSubstitute{#1}{;}{$\cdot$}}

However this requires you to be very strict in inputting the argument without spaces around the semicolons.

A quite robust solution uses xparse and the LaTeX3 packages:

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\RealMacro}{ O{~$\cdots$~} m }
 {
  \ryan_change_semicolons:nn { #1 } { #2 }
 }

 \seq_new:N \l_ryan_input_seq

 \cs_new_protected:Npn \ryan_change_semicolons:nn #1 #2
  {
   \seq_set_split:Nnn \l_ryan_input_seq { ; } { #2 }
   \seq_use:Nnnn \l_ryan_input_seq { #1 } { #1 } { #1 }
  }
 \ExplSyntaxOff

With this definition you don't have to worry if you input either one of

\RealMacro{apples;oranges;peaches;pears}
\RealMacro{apples ; oranges; peaches;pears}

because leading and trailing spaces will be removed. The macro allows also for a call such as

\RealMacro[, ]{apples;oranges;peaches;pears}

and the output would be

apples, oranges, peaches, pears

The default value of the optional argument is <space>$\cdot$<space> as requested.

share|improve this answer
add comment

Just to offer an alternative to looping over the list:

enter image description here

\documentclass{article}

\begingroup\lccode`\~`\;\lowercase{\endgroup
\def\ThisImaginaryMacro#1{\gdef~{\cdot}$\mathcode`\;"8000 \mathrm{#1}$}}
\begin{document}

\ThisImaginaryMacro{apples;oranges;peaches;pears}

\end{document}
share|improve this answer
add comment

This kind of task is conveniently done by a recursive macro:

%recursive ``low-level'' macro
\def\sToC#1;#2\nil{#1\ifx\empty#2\empty\else\space$\cdot$\space\sToC#2\nil\fi}

%user-friendly ``driver'' macro
\def\semicToCdot#1{\expandafter\sToC#1;\nil}

\semicToCdot{apples;oranges;peaches;pears}

\bye
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.