tikz 中错误计算(或显示)的 x 坐标

tikz 中错误计算(或显示)的 x 坐标

我想要绘制下图:

在此处输入图片描述

但仅在节点 x_1、x_2、x_3 中显示点的正确坐标。相应的 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\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}

我还希望它们被四舍五入,因此,我尝试使用 \pgfmathroundtozerofill,但结果如下:

在此处输入图片描述

相应的 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}

我无法弄清楚哪里出了问题,因为即使我使用了 \pgfextracty 而不是 \pgfextractx,也只有第一个坐标显示为 0;其他两个坐标显示为一些看似无关的数字。因此,任何帮助/建议都将不胜感激。

答案1

返回的尺寸\pgfextract是绝对坐标,但我猜你想获取相对坐标,这很容易提取。只需将点相对于原点的坐标除以 (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$};

这里($ (X) - (O) $)分别是 的相对坐标(1,1)($ (A1) - (O)$)的相对坐标(A1)\x1以及\x2它们各自的x分量和\n1分量的比例。

\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}

在此处输入图片描述

相关内容