1

I have a comma separated sequence of pairs

\def\sequence{0/1, 1/3, 2/2, 3/2, 4/0, 5/3, 6/3, 7/3}

I would like to use pgffor to convert this into

{(0,1) (1,3) (2,2) (3,2) (4,0) (5,3) (6,3) (7,3)}

which I plan to use along with \draw [smooth] plot coordinates to make a smooth curve.

Is there any way of doing this simply?

If not, is there a simple way to emulate the functionality of draw [smooth] plot coordinates with just a \foreach \i/\j in \sequence {...}?

MWE:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\sequence{ 0/1, 1/3, 2/2, 3/2, 4/0, 5/3, 6/3, 7/3}

\def\points{}
\foreach\x/\y in\sequence{%
  \expandafter\xdef\expandafter\mycoords\expandafter{\points (\x,\y)}%
}%

\draw [thick] plot [smooth] coordinates \points;
\end{tikzpicture}

\end{document} 
3

The following stores them in the coordinate format

\def\mycoords{}
\def\myseq{0/1,1/3,2/2,3/2,4/0,5/3,6/3,7/3}
\foreach\x/\y in\myseq{%
  \expandafter\xdef\expandafter\mycoords\expandafter{\mycoords (\x,\y)}%
}%
  • When attempting use \draw [smooth] plot coordinates \mycoords, I get an error "! Package tikz Error: Cannot parse this coordinate." – Hayden Dec 5 '16 at 15:21
  • @Hayden I get the desired results. Did you put a semicolon in the end? Otherwise it will keep looking – percusse Dec 5 '16 at 15:44
  • I've added an MWE to my question. Maybe I missed something simple. – Hayden Dec 5 '16 at 15:51
  • @Hayden You forgot to change one \mycoords to \points. – Henri Menke Dec 5 '16 at 16:28
  • @HenriMenke Well I'll be damned. It was something simple. Thanks for pointing that out! – Hayden Dec 5 '16 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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