GeneralSplitting a string at the last comma

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
gkamel
Posts: 25
Joined: Thu Oct 30, 2008 1:53 pm

Splitting a string at the last comma

Post by gkamel »

I have the following macro:

Code: Select all

\def\MyMacro{{a,b,c},5}
and I would like to separate the 'a,b,c' part into one macro, say \MyMacroLeft, and the '5' into another macro, \MyMacroRight. Could anyone give me some hints as to the most efficient way of achieving this.

Many thanks.
George Kamel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Splitting a string at the last comma

Post by josephwright »

If you know that there will be a comma in \MyMacro:

Code: Select all

\def\SplitMyMacro{%
  \expandafter\SplitMyMacroAux\SplitMyMacro\stop
}
\def\SplitMyMacroAux#1,#2\stop{%
  \def\MyMacroLeft{#1}%
  \def\MyMacroRight{#2}%
}
Joseph Wright
gkamel
Posts: 25
Joined: Thu Oct 30, 2008 1:53 pm

Splitting a string at the last comma

Post by gkamel »

Joseph, many thanks for your reply.. after a small correction, it worked perfectly --- the second line should be:

Code: Select all

\expandafter\SplitMyMacroAux\MyMacro\stop
Thanks again.
George Kamel
Post Reply