4

How to use { and } as delimiter instead of active caractere in this example ?

I'd like [360] [90] [7,2] as output of foreach command instead of this.

I need a string foreach compatible, splited on ; because arguments contain comma decimal separators.

enter image description here

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz,xstring}

\begin{document}

\StrSubstitute{360;90;7,2}{;}{\},\{}[\ListeA]
\xdef\ListeA{\{\ListeA\}}

\ListeA

\foreach \x in \ListeA {[\x] }

\end{document}
Tarass
  • 16,912
  • It's not clear what you want to achieve. – egreg Apr 28 '14 at 16:43
  • @egreg true (I deleted my first comment:-) but actually the aim is to iterate through a ; separated list which has , in the items. (\{ is a red herring) – David Carlisle Apr 28 '14 at 16:45
  • I'd like [360] [90] [7,2] as output of foreach command, what else ;-) – Tarass Apr 28 '14 at 16:48
  • @Tarass you really should have stated that in the question, I had to read your code 5 times to guess what it was trying to do to guess that was your intention. If you post code that works, as in produces no error, but does not produce what you expected, then it is hard to help if you do not say what you expect. – David Carlisle Apr 28 '14 at 16:50
  • @DavidCarlisle I'm very sorry, it was obvious in my head, please sorry for the loss of time. – Tarass Apr 28 '14 at 16:58
  • I give up with this, there is certainely a red dwarf in the machinery, I will have better chance another time. Thank you very much for your help and your time. – Tarass Apr 28 '14 at 19:22

2 Answers2

4

This produces the desired output

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\def\foo#1{\xfoo#1;\relax;}
\def\xfoo#1;{\ifx\relax#1\else\xxfoo{#1}\expandafter\xfoo\fi}
\newcommand\xxfoo[1]{[#1]}
\begin{document}

\foo{360;90;7,2}

\end{document}

If you absolutely insist on using \foreach you can construct a list in teh first loop to loop over with \foreach but this is entirely pointless, anything you do in the body of the \foreach you could have done in the first loop.

This time with french babel's ;

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{tikz}
\begin{document}


\def\foo#1{\expandafter\expandafter\expandafter\zfoo\xfoo#1;\relax;}
\def\xfoo#1;{\ifx\relax#1\else,{#1}\expandafter\xfoo\fi}
\def\zfoo#1{}

\edef\zz{\foo{360;90;7,2}}

\foreach\x in \zz{
This is [\x]

}

\end{document}
David Carlisle
  • 757,742
  • It produces [360;90;7,2] – Tarass Apr 28 '14 at 17:04
  • @Tarass Of course not. – egreg Apr 28 '14 at 17:29
  • You are right, standalone your example works fine but I tried it directly in the file in wich it has to work, and in it, the résult is what I said. Maybe there is a package cobflict ? I use it in a middle a lots of things. – Tarass Apr 28 '14 at 17:37
  • @Tarass see updated example – David Carlisle Apr 28 '14 at 19:16
  • @DavidCarlisle Please see what ment in my proposition here http://tex.stackexchange.com/questions/73173/how-to-change-the-item-separator-in-tikz-foreach-command/174129#174129. I am very sorry for the confusion I made and the loss of your time. Thank you for your help. – Tarass Apr 29 '14 at 04:43
  • @Tarass well I guessed it was something like that but as I say in my answer here having two loops is pointless, you should either fix the pgf loop to allow a different separator (as Mark Wilbrow just posted there) or use a different loop that does allow a different separator as in my or egregs answers here. It doesn't really make sense to use the more flexible loop mechanisms just so you can generate a comma separated loop for pgf. as you could do whatever you want to do in the first loop instead of building up a list. – David Carlisle Apr 29 '14 at 11:31
  • @DavidCarlisle I had to work on a list of decimal numbers, that in my country ;-) use ,. I could of course use . as decimal separator instead, and change . in , before dispay the nombers, but for homogeneity I prefer aulways use , as separator. Neverless the code is very simple and it was stimulating to find a solution with basic bricks. I never took time to learn tex programming, and now appears wonderfull expl3 language. I took one hour to understand the function and variable convention etc, but I have no clue of a list of the fonctions. I'll try again if I find a doc in french ;-) – Tarass Apr 29 '14 at 12:02
4

Here's a solution with expl3:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\addbrackets}{ m }
 {
  \tarass_addbrackets:n { #1 }
 }

\seq_new:N \l__tarass_addbrackets_seq

\cs_new_protected:Npn \tarass_addbrackets:n #1
 {
  \seq_set_split:Nnn \l__tarass_addbrackets_seq { ; } { #1 }
  [\seq_use:Nn \l__tarass_addbrackets_seq {]~[}]
 }
\ExplSyntaxOff

\begin{document}
\addbrackets{360;90;7,2}
\end{document}

Several refinements are possible.

enter image description here

If you're using the French module for babel, you have to be aware of the fact that ; is a special character, because of the peculiar French habit of having a space before the semicolon.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{xparse}

\ExplSyntaxOn
\shorthandon{;}
\NewDocumentCommand{\addbrackets}{ m }
 {
  \tarass_addbrackets:n { #1 }
 }

\seq_new:N \l__tarass_addbrackets_seq

\cs_new_protected:Npn \tarass_addbrackets:n #1
 {
  \seq_set_split:Nnn \l__tarass_addbrackets_seq { ; } { #1 }
  [\seq_use:Nn \l__tarass_addbrackets_seq {]~[}]
 }
\shorthandoff{;}
\ExplSyntaxOff

\begin{document}
\addbrackets{360;90;7,2}
\end{document}
egreg
  • 1,121,712
  • It gives me : [360;90;7,2] , I use \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} if it matters. – Tarass Apr 28 '14 at 17:13
  • @Tarass You're probably not saying the most important thing, that you're using French babel. It's very hard to help if you hide fundamental information: in French babel, the semicolon is a special character. – egreg Apr 28 '14 at 17:30
  • It is on the first line of my MWE ;-) – Tarass Apr 28 '14 at 17:32
  • @Tarass No. I can't see babel loaded. That option just issues a warning, if \usepackage{babel} doesn't appear. – egreg Apr 28 '14 at 17:34
  • Sorry again, I thaught french instead of babel, time to sleep for me. Of course it works now. But the point is not backeted space separated result, but a foreach compatible string (for a complex use), here I gave just MWE of this use. I am very sorry for all this mysundertood and loss of your booth times. – Tarass Apr 28 '14 at 17:41