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.

I like to define function which represent binary data, but i stuck in this rather simple task ... my NWME is:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\pgfmathdeclarefunction{data}{2}{%
    \pgfmathifthenelse{(or(\x<#1, \x>#2))}{1}{0}%
                                }
   \begin{tikzpicture}
\draw [red,  thick] (0,0) --
    plot [domain= 0:80, samples=160, smooth]  (\x,data(20,40));
    \end{tikzpicture}
\end{document}

This example gives error: "giving up on this path. ..." It seems that I do not understand howto use pgfmath for drawing such a simple function.

Drawing of this data function by hand is simple, but latter i like add a noise to this function.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Here, I complete the excellent cmhuges's answer to show how to use the declare function key:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \tikzset{declare function={
      data(\a,\b,\x)=or(\x<\a, \x>\b)?1:0;
    }}
  \draw [red,thick] (0,0) -- plot [domain= 0:80,samples=160,smooth]  (\x,{data(20,40,\x)});
\end{tikzpicture}
\end{document}
share|improve this answer

Your pgfmath is ok, but you need to use

\draw [red,  thick] plot [domain= 0:80, samples=160, smooth]  (\x,{data(20,40)});

Note the {} in (\x,{data(20,40)}). Here's a complete MWE:

% arara: pdflatex
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\pgfmathdeclarefunction{data}{2}{%
    \pgfmathifthenelse{(or(\x<#1, \x>#2))}{1}{0}%
}
\begin{tikzpicture}
    \draw [red,  thick] plot [domain= 0:80, samples=160, smooth]  (\x,{data(20,40)});
\end{tikzpicture}
\end{document}

As a side note, you might like to explore the pgfplots package (which is based on tikz) as it eases some of the notation.

share|improve this answer
    
Huh, how simple, if know how. this sketch i will add to block scheme of communication channel, so I like to stay with pure tikz. And one more question, I need function data different from picture to picture. Is possible to include it definition inside tikzpicture? –  Zarko Jul 4 at 11:11
    
@Zarko yes; see, for example, PGF Math Error: Could not parse input –  cmhughes Jul 4 at 12:28
    
thank you for the link. The similar solution also gives Poul in his answer. But now i thinking, how to combine pgfplots and pure tikz picture. Graph given in my MWE is actually inside callout node, which described signals in scheme. I will experiment in my first spare time. –  Zarko Jul 4 at 15:04

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.