1

I would like to plot the following figure:

enter image description here

but only that in the nodes x_1,x_2,x_3 the right coordinates of the points displayed. The corresponding MWE is the following - I have included the entire preamble, since this is a part of a larger document:

\documentclass[11pt,a4paper]{article} \usepackage[english,greek]{babel} \usepackage[iso-8859-7]{inputenc} \usepackage[table]{xcolor} \usepackage{amsmath,amsthm,enumitem,amssymb,tikz,graphicx,float,mathtools,mdframed,changepage,ifthen} \usetikzlibrary{decorations.pathreplacing} \usetikzlibrary{decorations.pathmorphing} \usetikzlibrary{arrows.meta} \usetikzlibrary{patterns} \usetikzlibrary{intersections} \usetikzlibrary{calc} \begin{document} \begin{figure} \centering \begin{tikzpicture} \draw[thick,->,name path=x axis] (-4,0) -- (4,0)node[pos=1,below]{$x$}; \draw[thick,->,name path=y axis] (0,-3) -- (0,3)node[pos=1,left]{$y$}; \node[left,yshift=-8pt](O) at (0,0){$O$}; \foreach \i in {-3,-2,...,3}{ \ifthenelse{\i=-3 \OR \i=3}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; } { \ifthenelse{\NOT \i=0}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; \draw (-.1,\i) -- (.1,\i) node[pos=0,left]{}; }{} } } \draw[thick, name path=curve] (-3,-1.5) .. controls (-1.9,3) and (2.6,-3) .. (3,2); \foreach \i in {1,2,3}{ \node[circle, fill=black, inner sep=1pt, name intersections={of={curve and x axis}}] (A\i) at (intersection-\i){}; } \newdimen\mydim \pgfextractx{\mydim}{(A1)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A1)+(0,.5)$){$x_1\approx\res$}; \pgfextractx{\mydim}{(A2)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A2)+(.4,.5)$){$x_2\approx\res$}; \pgfextractx{\mydim}{(A3)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A3)+(0,-.5)$){$x_3\approx\res$}; \end{tikzpicture} \caption{Οι ρίζες της εξίσωσης $f(x)=0$} \label{fig:zeros} \end{figure} \end{document} 

I would also like them to be rounded, so, I tried to use \pgfmathroundtozerofill, but the result was as follows:

enter image description here

And the corresponding MWE:

\documentclass[11pt,a4paper]{article} \usepackage[english,greek]{babel} \usepackage[iso-8859-7]{inputenc} \usepackage[table]{xcolor} \usepackage{amsmath,amsthm,enumitem,amssymb,tikz,graphicx,float,mathtools,mdframed,changepage,ifthen} \usetikzlibrary{decorations.pathreplacing} \usetikzlibrary{decorations.pathmorphing} \usetikzlibrary{arrows.meta} \usetikzlibrary{patterns} \usetikzlibrary{intersections} \usetikzlibrary{calc} \begin{document} \begin{figure} \centering \begin{tikzpicture} \draw[thick,->,name path=x axis] (-4,0) -- (4,0)node[pos=1,below]{$x$}; \draw[thick,->,name path=y axis] (0,-3) -- (0,3)node[pos=1,left]{$y$}; \node[left,yshift=-8pt](O) at (0,0){$O$}; \foreach \i in {-3,-2,...,3}{ \ifthenelse{\i=-3 \OR \i=3}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; } { \ifthenelse{\NOT \i=0}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; \draw (-.1,\i) -- (.1,\i) node[pos=0,left]{}; }{} } } \draw[thick, name path=curve] (-3,-1.5) .. controls (-1.9,3) and (2.6,-3) .. (3,2); \foreach \i in {1,2,3}{ \node[circle, fill=black, inner sep=1pt, name intersections={of={curve and x axis}}] (A\i) at (intersection-\i){}; } \newdimen\mydim \pgfextractx{\mydim}{(A1)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A1)+(0,.5)$){$x_1\approx\pgfmathroundtozerofill{\res}\pgfmathresult$}; \pgfextractx{\mydim}{(A2)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A2)+(.4,.5)$){$x_2\approx\pgfmathroundtozerofill{\res}\pgfmathresult$}; \pgfextractx{\mydim}{(A3)} \pgfmathsetmacro{\res}{\mydim*0.03515} \node at ($(A3)+(0,-.5)$){$x_3\approx\pgfmathroundtozerofill{\res}\pgfmathresult$}; \end{tikzpicture} \caption{Οι ρίζες της εξίσωσης $f(x)=0$} \label{fig:zeros} \end{figure} \end{document} 

I cannot figure out what is going wrong, since even when I used \pgfextracty instead of \pgfextractx, only the first coordinate was displayed as 0; the other two where some, seemingly, irrelevant numbers. So, any help/suggestion would be very much appreciated.

1
  • 1
    The problem is that \pgfextractx should be used with PGF (low level) points not with TikZ ones. You can ckeck that if you replace \pgfextractx{\mydim}{(A1)} by \pgfextractx{\mydim}{(A2)} the result is not changing (probably you get the x coordinate of the last used point). You can check the answers of this question to see different methods how to obtain the coordinates of a point. Commented Jun 20, 2018 at 18:38

1 Answer 1

1

The dimensions returned by \pgfextract are absolute coordinates, but I guess you want to get relative coordinates, which are easy to extract. One only has to divide the coordinates of the points relative to the origin by the coordinates of the point at (1,1) relative to the origin. In the code below this is accomplished by

\path let \p1=($ (X) -(O) $),\p2=($ (A1) - (O)$),\n1={\x2/\x1} in node at ($(A1)+(0,.5)$){$x_1\approx\pgfmathroundtozerofill{\n1}\pgfmathresult$}; 

Here ($ (X) - (O) $) are the relative coordinates of (1,1) and ($ (A1) - (O)$) the relative coordinates of (A1), \x1 and \x2 their respective x components and \n1 the ratio of the components.

\documentclass[11pt,a4paper]{article} \usepackage[english,greek]{babel} \usepackage[iso-8859-7]{inputenc} \usepackage[table]{xcolor} \usepackage{amsmath,amsthm,enumitem,amssymb,tikz,graphicx,float,mathtools,mdframed,changepage,ifthen} \usetikzlibrary{decorations.pathreplacing} \usetikzlibrary{decorations.pathmorphing} \usetikzlibrary{arrows.meta} \usetikzlibrary{patterns} \usetikzlibrary{intersections} \usetikzlibrary{calc} \begin{document} \begin{figure} \centering \begin{tikzpicture} \draw[thick,->,name path=x axis] (-4,0) -- (4,0)node[pos=1,below]{$x$}; \draw[thick,->,name path=y axis] (0,-3) -- (0,3)node[pos=1,left]{$y$}; \node[left,yshift=-8pt](O) at (0,0){$O$}; \foreach \i in {-3,-2,...,3}{ \ifthenelse{\i=-3 \OR \i=3}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; } { \ifthenelse{\NOT \i=0}{ \draw (\i,-.1) -- (\i,.1) node[pos=0,below]{}; \draw (-.1,\i) -- (.1,\i) node[pos=0,left]{}; }{} } } \draw[thick, name path=curve] (-3,-1.5) .. controls (-1.9,3) and (2.6,-3) .. (3,2); \foreach \i in {1,2,3}{ \node[circle, fill=black, inner sep=1pt, name intersections={of={curve and x axis}}] (A\i) at (intersection-\i){}; } \coordinate (O) at (0,0); \coordinate (X) at (1,1); \path let \p1=($ (X) -(O) $),\p2=($ (A1) - (O)$),\n1={\x2/\x1} in node at ($(A1)+(0,.5)$){$x_1\approx\pgfmathroundtozerofill{\n1}\pgfmathresult$}; \path let \p1=($ (X) -(O) $),\p2=($ (A2) - (O)$),\n1={\x2/\x1} in node at ($(A2)+(.4,.5)$){$x_2\approx\pgfmathroundtozerofill{\n1}\pgfmathresult$}; \path let \p1=($ (X) -(O) $),\p2=($ (A3) - (O)$),\n1={\x2/\x1} in node at ($(A3)+(0,-.5)$){$x_3\approx\pgfmathroundtozerofill{\n1}\pgfmathresult$}; \end{tikzpicture} \caption{Οι ρίζες της εξίσωσης $f(x)=0$} \label{fig:zeros} \end{figure} \end{document} 

enter image description here

2
  • That was really helpful! Also, why do we use X and O, since X-O is the same as X? Commented Jun 20, 2018 at 18:32
  • @ΒασίληςΜάρκος In this case, yes, but not in general. I want to post an answer for those who put the origin at an arbitrary place, and also have different x and y units. But yes, for your example you could just use ($ (1,0) $), I think. Commented Jun 20, 2018 at 18:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.