3

Problem:

I am trying to get the function y = |x| to work with data visualization library in TikZ but without success. It seems the issue is with the operator |.

Minimal Working Example (MWE):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}

\begin{tikzpicture}
    \datavisualization [school book axes,
                        visualize as smooth line,
                        y axis={label},
                        x axis={label}]

    data [format=function] {
          var x : interval [-5:5];
          func y = |x|;
    };
\end{tikzpicture}

\end{document}

Desired output:

To get the function to work with the operator |.

4

The vertical bars are just notation, i.e. a common way to typeset the absolute value. According to section 94.3 Syntax for Mathematical Expressions: Functions of the pgf manual (version 3.1.3) the function is abs (and you forgot a \value).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}

\begin{tikzpicture}
    \datavisualization [school book axes,
                        visualize as smooth line,
                        y axis={label},
                        x axis={label}]

    data [format=function] {
          var x : interval [-5:5];
          func y = abs(\value x);
    };
\end{tikzpicture}

\end{document}

enter image description here

ADDENDUM: Can one declare new math operators? Yes. Can one define an operator |? Yes. Does it work as you want it and is it safe? Maybe not.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\pgfmathdeclareoperator{|} {abs}   {1}{infix} {600}
\begin{document}
\begin{tikzpicture}
    \datavisualization [school book axes,
                        visualize as smooth line,
                        y axis={label},
                        x axis={label}]

    data [format=function] {
          var x : interval [-5:5];
          func y = | \value x ;
    };
\end{tikzpicture}

\end{document}
  • Thanks - is there a way to get the operators to work with the datavisualization? If not, how do you find the name of the functions to use? – kexxcream Jul 13 at 11:36
  • @kexxcream The functions are given in section 94.3 Syntax for Mathematical Expressions: Functions of pgfmanual v 3.1.3. I added this information to the asnwer. And yes, you could make the operator work but this will be quite a hack, and most likely fragile. One of the problems is that | is already used in || for or, which can be used in ifthenelse. I strongly advise not to hack this. – user121799 Jul 13 at 11:37
  • 1
    @kexxcream I added a way to declare a math operator. It is a compromise between very hacky and the precise syntax you wanted. In this version there is only one | in the beginning. Whether or not this breaks anything I do not know. – user121799 Jul 13 at 12:20

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.