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.

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}
share|improve this question
    
It's not clear what you want to achieve. –  egreg Apr 28 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 at 16:45
    
I'd like [360] [90] [7,2] as output of foreach command, what else ;-) –  Tarass Apr 28 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 at 16:50
    
@DavidCarlisle I'm very sorry, it was obvious in my head, please sorry for the loss of time. –  Tarass Apr 28 at 16:58

2 Answers 2

up vote 4 down vote accepted

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}
share|improve this answer
    
It produces [360;90;7,2] –  Tarass Apr 28 at 17:04
    
@Tarass Of course not. –  egreg Apr 28 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 at 17:37
    
@Tarass see updated example –  David Carlisle Apr 28 at 19:16
    
@DavidCarlisle Please see what ment in my proposition here tex.stackexchange.com/questions/73173/…. I am very sorry for the confusion I made and the loss of your time. Thank you for your help. –  Tarass Apr 29 at 4:43

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}
share|improve this answer
    
It gives me : [360;90;7,2] , I use \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} if it matters. –  Tarass Apr 28 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 at 17:30
    
It is on the first line of my MWE ;-) –  Tarass Apr 28 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 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 at 17:41

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.