TransWikia.com

Label function based x coordinate of a given function

TeX - LaTeX Asked on March 13, 2021

Consider the following MWE:

enter image description here

documentclass[letterpaper,11pt]{article}
usepackage{tikz}
usetikzlibrary{arrows.meta}

begin{document}
begin{tikzpicture}[domain=-2:2,samples=100]
    draw[font=small,-Stealth] (-3,0)  -- (3,0) node[right]{$x$} ;
    draw[font=small,-Stealth] (0,-2)  -- (0,2) node[above]{$y$};
        foreach x in {-1,0,...,1}
            draw (x, -2pt) -- ++ (0,4pt);%[shift={(2,1.5)}]
        foreach y in {-1,0,...,1}
            draw (-2pt, y) -- ++ (4pt, 0);%[shift={(2,1.5)}]
    clip[rounded corners=10pt] (-3,-2) rectangle (3,2);
    node[label=left:{$f(x) =x^2$}] at (-1.5,1.25) {};
    draw[color=red,thick] plot (x,{(x)^2-1});
end{tikzpicture}
end{document}

Is it possible to have a node definition that labels the function at a specified x coordinate?

Say I want the label of the function to be at the x coordinate x=-1.5, then it would automatically calculate the correct coordinates for such label; in this case y=(-1.5)^2-1.

One Answer

This is the purpose of declare function. That is, you can declare a function via

declare function={f(x)=(x)^2-1;}

and then use it e.g. in

node[label={[font=footnotesize]left:{$f(x) =x^2$}}] at (-1.5,{f(-1.5)}) {};

which now knows the value of the function at x=-1.5, or in

draw[color=red,thick] plot (x,{f(x)});

which plots the function. (It is better to use an odd number of samples for symmetric plots, and 100 samples is rather generous, you may want to reduce the sample number and use the smooth key.)

Full code:

documentclass[letterpaper,11pt]{article}
usepackage{tikz}
usetikzlibrary{arrows.meta}

begin{document}
begin{tikzpicture}[domain=-2:2,samples=101,declare function={f(x)=(x)^2-1;}]
    draw[font=small,-Stealth] (-3,0)  -- (3,0) node[right]{$x$} ;
    draw[font=small,-Stealth] (0,-2)  -- (0,2) node[above]{$y$};
        foreach x in {-1,0,...,1}
            draw (x, -2pt) -- ++ (0,4pt);%[shift={(2,1.5)}]
        foreach y in {-1,0,...,1}
            draw (-2pt, y) -- ++ (4pt, 0);%[shift={(2,1.5)}]
    node[label={[font=footnotesize]left:{$f(x) =x^2$}}] at (-1.5,{f(-1.5)}) {};
    clip[rounded corners=10pt] (-3,-2) rectangle (3,2);
    draw[color=red,thick] plot (x,{f(x)});
end{tikzpicture}
end{document}

Note that in pgfplots 1-dimensional plots (of this type) are paths, so you can add labels by specifying the position along the path.

documentclass[letterpaper,11pt]{article}
usepackage{pgfplots}
pgfplotsset{compat=1.17}

begin{document}
begin{tikzpicture}[declare function={f(x)=(x)^2-1;}]
 begin{axis}[axis lines=middle,ymax=2.2,ymin=-1.2,
    xlabel={$x$},ylabel={$y$}]
  addplot[color=red,domain=-2:2,smooth,semithick] {f(x)}
    node[pos=0.275,below left] {$f(x)$};
 end{axis}
end{tikzpicture}
end{document}

enter image description here

Correct answer by user228539 on March 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP