我制作了一张图来展示牛顿方法的实际应用:
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);
\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);
\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);
\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);
\coordinate (x0) at (2.25, 0);
\coordinate (x1) at (3.5, 0);
\draw[<->] (y) node[left] {$f(x)$} -- (0,0) -- (x) node[below] {$x$};
\draw (sp0) .. controls (csp0) and (cep0).. (ep0);
\draw[dashed] (cep0) -- (csp1);
\draw[dotted] (ep0) -- (0, 2);
\draw[dotted] (ep0) -- (3.5, 0);
\draw (sp1) .. controls (csp1) and (cep1).. (ep1);
\draw (sp2) .. controls (csp2) and (cep2).. (ep2);
\draw (3.5,1pt) -- (3.5,-3pt) node[anchor=north] {$x_0$};
\draw (2.75,1pt) -- (2.75,-3pt) node[anchor=north] {$x_1$};
\draw (1pt, 2) -- (-3pt, 2) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\caption{Newton's method in action.}
\label{fig:newton_method}
\end{figure}
\end{document}
接近尾声时,你会看到我写了如下内容:
\draw (3.5,1pt) -- (3.5,-3pt) node[anchor=north] {$x_0$};
我正在尝试为先前定义的坐标(在本例中为)设置刻度x1
,并且我只需要它的 x 分量(3.5
),但我不知道如何单独引用它,因此我手动引用它。这样做的缺点是,如果我重新定义x1
,那么我必须仔细检查代码以找到仅使用其一个分量的任何位,然后也对其进行更改。您已经可以看到我已经开始犯错误,并且我对是否要引用x1
或感到困惑x0
。
我怎样才能以更自动化的方式仅引用一个组件?
答案1
一种方法是使用库let
中的语法calc
。在包含
\usetikzlibrary{calc}
我们可以说
\draw let \p1 = (x1) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_0$};
它将坐标设置x1
为一个点\p1
,该点的各个坐标可以在语句中使用\x1
and进行访问\y1
。这可能会造成混淆,因为所讨论的坐标是有名称的x1
,因此我们可以轻松地完成
\draw let \p2 = (x1) in (\x2,1pt) -- (\x2,-3pt) node[anchor=north] {$x_0$};
补充说明一下:我们可以使用相对坐标来仅要求一次我们的坐标位置:
\draw let \p1 = (x1) in (\x1,1pt) -- ++(0,-4pt) node[anchor=north] {$x_0$};
以下是完整代码
\documentclass{standalone}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);
\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);
\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);
\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);
\coordinate (x0) at (2.25, 0);
\coordinate (x1) at (3.5, 0);
\draw[<->] (y) node[left] {$f(x)$} -- (0,0) -- (x) node[below] {$x$};
\draw (sp0) .. controls (csp0) and (cep0).. (ep0);
\draw[dashed] (cep0) -- (csp1);
\draw[dotted] (ep0) -- (0, 2);
\draw[dotted] (ep0) -- (3.5, 0);
\draw (sp1) .. controls (csp1) and (cep1).. (ep1);
\draw (sp2) .. controls (csp2) and (cep2).. (ep2);
\draw let \p1 = (x1) in (\x1,1pt) -- ++(0,-4pt) node[anchor=north] {$x_0$};
\draw (2.75,1pt) -- (2.75,-3pt) node[anchor=north] {$x_1$};
\draw (1pt, 2) -- (-3pt, 2) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\end{document}
输出结果如下: