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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's a follow-up to a previous question. If I could have a macro out of the solution over there, it would have been great but then I couldn't make it.

  1. The following code (to be added to the one in Paul Gaborit's answer in the previous thread) is not working yet, how to fix it?

    % one + two + three
    \foreach \c in {0,...,100} {
       \path[name path=line] (\x,0) -- (\x,6);
       \path[name intersections={of=one and line,name=inter}];
       % How to initialize sum?
       \foreach \curve in {two,three}{ 
          \path[name intersections={of=curve and line,name=newinter}];
          \path let \p1=(inter-1), \p2=(newinter-1) in 
          (\x1,\y1+\y2) coordinate (sum-\c);
    (sum-0) \foreach \x in {1,...,100}{-- (sum-\x)} node[right]{one + two + three};
  2. How to pass the number of samples (here 100) as input? A better way would be to be able to pass a list of x-coordinates (the kink location) where the vertical intersecting lines would be located.

In case of piecewise linear curves, an even better way would be indeed a way to merge all the x-coordinates of all the curves, sort them, and just intersect at those points.

share|improve this question
up vote 4 down vote accepted

Here an enhanced version of my answer with a generic method to add several curves (named paths).

You will notice the slowness of the whole! For these calculations, TeX is really not the right tool.


enter image description here



  % samples, xmin, xmax, ymin, ymax, list of paths, prefix
  \foreach \c in {0,...,\samples} {
    \pgfmathsetmacro{\x}{\c/\samples * (\xmax-\xmin)+\xmin}
    % verticale line
    \path[name path=line] (\x,\ymin) -- (\x,\ymax);
    % initialize sum (y=0)
    \coordinate (\prefix-\c) at (\x,0);
    % add each path
    \foreach \curve in \listofpaths {
      \path[name intersections={of=line and \curve,name=inter}];
      \path let \p1=(inter-1), \p2=(\prefix-\c) in
      (\x2,\y1+\y2) coordinate (\prefix-\c);

\begin{tikzpicture}[line width=1pt]

  % a grid
  \draw[help lines] (0,-.5) grid (10,10);
  % x axis
  \draw[-latex,thick] (0,0) -- (10,0) node[right]{$x$};

  % one (red line)
  \foreach \point[count=\c] in \lineone {%
    \coordinate[at=\point] (one-\c);%
    %\fill[red] (one-\c) circle (0.1);%
  \draw[red,name path=one] (one-1)
  \foreach \i in {2,...,\c}{-- (one-\i)} node[right]{one};

  % two (blue line)
  \foreach \point[count=\c] in \linetwo {%
    \coordinate[at=\point] (two-\c);%
    %\fill[blue] (two-\c) circle (0.1);%
  \draw[blue,name path=two] (two-1)
  \foreach \i in {2,...,\c}{-- (two-\i)} node[right]{two};

  % one + two
  (sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + two};

  % three (a green function)
  \draw[green!50!black,name path=three]
  (\x,{sin(3*\x r)+2}) node[right]{three};

  % one + three
  (sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + three};

  % four (orange function)
  \draw[orange,name path=four]
  (\x,{cos(1*\x r)+4}) node[right]{four};

  % four + three
  (sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{three + four};

  % one + two + three
  \draw[red!50!green!50!black] (sum-0)
  \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + two + three};

share|improve this answer
Great! It's slow as it treats the general case with a huge number of intersections to be computed. I'd like to have a specific \addpiecewiselinear macro with just a list of paths as a mandatory argument and the "min/max" coordinates as optional arguments (clipping the range where the computations take place if any) – green diod Sep 19 '12 at 14:23

Your Answer


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.