在數學方面

在數學方面

我應該指出,我已經熟悉 TikZ 並且我正在尋找技術答案,就好像 TikZ 技術不夠一樣。

這個問題歸結為以下問題,這些東西之間有什麼區別:

  • \def\pointA{\pgfpoint{0cm}{0cm}}
  • \coordinate (A) at (current page.north east);
  • \coordinate (A) at (1,1);
  • (current page.north east)
  • \node [anchor=east] {}<-- 錨? :-p(只是證明術語重疊)
  • \pgfpointanchor<-- 但這確實是個錨點,還是一個點? :-p

我認為我的理解缺乏,而且它幹擾了我如下所示的嘗試。

在數學方面

A觀點定義為:

點是平面上的精確位置或位置。重要的是要理解點不是一個東西,而是一個地方。

A協調定義為:

座標是定義點位置的一組有序數字。

錨點是:

在此輸入影像描述

我想用實際例子來澄清這一點。

讓我們定義一個座標:

\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library

有許多隱式座標(它們應該被稱為座標、點、錨點還是其他什麼?),例如:

  • (當前頁.西北)
  • (目前頁.北)
  • (目前頁.東北)
  • (當前頁.east)
  • (目前頁.東南)
  • (目前頁.南)
  • (目前頁.西南)
  • (目前頁.中心)

假設我想計算兩個事物之間的垂直距離(我故意選擇“事物”一詞)並儲存此變數以在當前範圍之外使用。

例如,兩件事是(目前頁。東北)和(右中)

為了進行算術,我知道以下選項(這是全部嗎?):

  1. 我可以用來\pgfmathparse評估算術

  2. 我可以使用\path let \p1=語法。

使用選項 1,我可以設定\pgfxa\pgfya\pgfx\pgfy進行計算並使用 取得結果\pgfmathresult

然後我可以定義一個唯一的長度(全域),例如\setlength{\mylength}{2cm}。然後我可以用來\mylength檢索它。這不允許我在獨立的巨集中編寫所有內容,因為\setlength可能只能定義一次。另一種選擇是使用\global\let\mylength{2cm},在我看來這更適合這項工作(如果我錯了,請糾正我。這對值+單位/維度有影響嗎?)?

把它們放在一起

這是一個實驗不編譯基於幾個答案:

\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{calc}

% A4 Paper
\pdfpagewidth=210mm \pdfpageheight=297mm % for PDF output
\paperwidth=210mm \paperheight=297mm     % for TikZ

\newcommand{\getdistance}[3]{
\makeatletter
% Syntax: <length variable/macro> <coordinate1> <coordinate2>
  \pgfpointdiff{\pgfpointanchor{#2}{center}} % https://tex.stackexchange.com/a/39325/13552
               {\pgfpointanchor{#3}{center}} % https://tex.stackexchange.com/a/39325/13552
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgfmathparse{veclen(\pgf@xa,\pgf@ya)} 
  \global\let#1\pgfmathresult % <-- I want the pgfmathresult to be a dimension like 510 pt
\makeatother
}

\begin{document}

\begin{tikzpicture}[overlay]
\coordinate (rightmidtest) at ($ (current page.north east) + (0,-10cm) $); % use tikz calc library
\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
\end{tikzpicture}

\getdistance{\globalresult}{rightmidtest}{rightmid}
\getdistance{\globalresult}{current page.north east}{rightmid}
\getdistance % <-- Should expand to last distance measured
\end{document}

更新碼 2016-02-25在職的

經過分析和從打擊樂的答案中得到新的理解後,我想出了別的東西。

記住:

  • (current page)是隱含的
  • 錨點 .center 在 TikZ 中是隱式的,但在 PGF 中不是隱式的\pgfpointanchor{<ref>}{<anchor e.g. center>}

我使用xparse巨集語法是因為它更容易閱讀。

\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xparse}

\makeatletter
\NewDocumentCommand{\getdistance}{ m m O{center} m O{center} }{%
    %Syntax: {\<macro>} {<ref1>} [<anchor>] {<ref2>} [<anchor>]
    \pgfpointdiff{\pgfpointanchor{#2}{#3}}% https://tex.stackexchange.com/a/39325/13552
                 {\pgfpointanchor{#4}{#5}}% https://tex.stackexchange.com/a/39325/13552
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgfmathparse{veclen(\pgf@xa,\pgf@ya)} % result as number (floating point)
    %\pgfmathveclen{\pgf@xa}{\pgf@ya} % alternate syntax to pgfmathparse: result as number (floating point)
    %\global\let#1\pgfmathresult pt % does not recognize pt.
    \global\edef#1{\pgfmathresult\ pt}
    %\pgfmathprintnumber{\pgfmathresult}%
  }    
\makeatother    

 \begin{document}
 
\begin{tikzpicture}[remember picture,overlay]
    % (current page) is implicit
    % .center is implicit in TikZ but not in PGF \pgfpointanchor{<ref>}{<anchor e.g. center>}
    \coordinate (referral1) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
    \node [minimum height=2cm] (referral2) {};
\end{tikzpicture}
    
\begin{tikzpicture}[remember picture,overlay]
  \getdistance{\globalresultA}{current page}{referral1}
  \getdistance{\globalresultB}{current page}[north east]{referral1}
  \getdistance{\globalresultC}{current page}[north east]{referral2}[north]
  \getdistance{\globalresultD}{current page}[north east]{referral2}[south]
\end{tikzpicture}

\globalresultA

\globalresultB

\globalresultC

\globalresultD
\end{document}

答案1

我認為這種混亂來自於 TikZ 使用引用而不是物件。當a提到某個節點名稱時。它通常假設使用者可能指的是其中心座標。或者,如果在兩個事物之間繪製事物,如果其中一個是節點,那麼它會神奇地計算其邊界上的點,這樣使用者就不會注意到。

但這一切都是內部完成的。沒有什麼與節點或其他相關。節點有一個您可以引用的名稱以及它可以理解的預定義位置。

這些都沒有被儲存。它們只是被檢查是否存在或只是被執行。換句話說,如果你寫shape=duck,那麼它不會遍歷所有可能的形狀名稱,它只是進行某種存在性檢查(我對實際巨集使用無意義的名稱)

\pgfutil@ifdefined\pgf@Shape@#1@...{<if it exists use>}{<if not err>}

錨點也是同樣的情況,如果你說,它會通過一些等等anchor=heel要求該名稱。\pgf@sh@#1@anchor@#2

那麼這些是從哪裡來的呢?它們是透過\savedanchor等在形狀聲明中定義的\anchor

因此,它們就在那裡並經過精心排列,以便當您引用它時,從文字框寬度高度到形狀路徑的所有內容都經過痛苦的手工編碼。這就是為什麼定義新形狀是一項非常乏味的工作。當您引用它們時,它們的排列方式是將錨點位置寫入(全域!)長度暫存器內\pgf@<x,y>

無論如何,長話短說,當您引用節點錨點時,實際上有一個非常複雜的過程來從中獲取座標。

此外,座標\pgf@<x,y>和 座標類型節點之間是有區別的\coordinate

最後,當您引用節點時,TikZ 會嘗試透過假設您指的是其中心錨點來幫助您,以便您可以節省一些擊鍵,但有時您需要進行更精細的操作,例如距離測量。

您無法擺脫推薦名稱。您需要實際座標(同樣不是\coordinates)。正如 Mark Wibrow 評論的那樣,您必須以某種方式理解上下文,這是通過\pgf@process回答以下問題來完成的:它是文字坐標嗎(1,1)? 它是節點名稱嗎?可以使用產生的\pgf@<x,y>暫存器。

然後你就可以對它們做任何你想做的事。您的範例let語法在簡化此方面也做了出色的工作,但它仍然執行您所描述的操作。實際上你的問題基本上是為什麼 TikZ 存在於 PGF 之上。它是一個設計精良的前端,具有非常冗長但功能強大的 PGF 語法。

答案2

\def\pointA{\pgfpoint{0cm}{0cm}}

僅當您熟悉 PGF(Ti 的基礎)時才使用此選項kZ)。無論如何, \pgfpointTeX 中的作用並不重要;重要的是。這個指令永遠不會單獨存在。通常它是語法的一部分,例如

\coordinate (A) at (current page.north east);

給一個點一個名字。實際的 x 和 y 座標是什麼並不重要。我們知道(A)它在哪裡,這就是我們所需要知道的。

\coordinate (A) at (1,1);

與上一篇類似。通常我們命名的目的是(A)減少程式碼長度/複雜性和(B)提高易讀性。例如

(current page.north east)

帶有括號,它發出一個搬去行動。它很像你,拿著筆,將手移動到下一個你想畫東西的地方。例如

這段程式碼讓 TikZ 在畫布周圍移動筆並聲明節點/座標。

\node [anchor=east] {}

在筆的目前位置「附近」放置一個節點。我所說的「靠近」是指您給出的錨點應該與筆的位置一致。這很像你在船上,可以將錨拋到你想要的地方,然後錨就會固定船。


實踐部分

  • 你需要\makeatletter
  • 你可能想要\pgfmathsetlength而不是\pgfmathsetmacrosor \pgfmathparse
    • 但請記住,所有尺寸都必須在使用之前聲明。
  • 使用節點時要小心(current page)。通常你需要[overlay].
  • 你需要在一個tikzpicture環境中才能檢索你在tikzpicture環境中所做的任何事情。
    • 如果您不在同一tikzpicture環境中完成這項工作,則需要[remember picture].

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\newcommand{\getdistance}[3]{
% Syntax: <length variable/macro> <coordinate1> <coordinate2>
  \pgfpointdiff{\pgfpointanchor{#2}{center}} % http://tex.stackexchange.com/a/39325/13552
               {\pgfpointanchor{#3}{center}} % http://tex.stackexchange.com/a/39325/13552
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgfmathparse{veclen(\pgf@xa,\pgf@ya)} 
  \global\let#1\pgfmathresult % <-- I want the pgfmathresult to be a dimension like 510 pt
}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\coordinate (rightmidtest) at ($ (current page.north east) + (0,-10cm) $); % use tikz calc library
\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
\end{tikzpicture}

\tikz[remember picture,overlay]{
    \getdistance{\globalresulta}{rightmidtest}{rightmid}
    \getdistance{\globalresultb}{current page}{rightmid}
}

\globalresulta

\globalresultb

\end{document}

答案3

  1. \def\pointA{\pgfpoint{0cm}{0cm}}

    這基本上與\coordinate (A) at (0,0);

  2. \coordinate (A) at (current page.north east);

    在這裡,您只需將頁面的右上角命名為A。從某種意義上說,該位置已經命名是毫無意義的,但不必輸入所有內容並只寫A.

  3. \coordinate (A) at (1,1);

    TikZ 中的座標基本上是具有coordinate形狀的節點,它們不需要標題,而且特別重要的是,它們沒有sep.所以這是一點。儘管如此,寫作\node[coordinate] (a) at (0,0) {};還是等於\coordinate (a) at (0,0);

    也可以看看TikZ:\node 和 \coordinate 之間的差異?

  4. (current page.north east)

    這表示目前“頁面”的右上角。通常這將是 A4 頁面,但它可以是任何尺寸。在這種情況下,頁面不一定是通常的紙張,但它可以是任何指定的區域。

  5. \node [anchor=east] {}<-- 錨?

    不,這不是錨。它是一個節點,其定位是透過將設計的錨點放置在座標處來確定的。因此將放置具有與座標一致的錨點\node [anchor=east] at (4,0) {};的節點。east(4,0)

答案4

我不知道如何編輯我的舊答案。我就簡單地寫一篇新的。

鈦中kZ 指定點的方法有很多種

  • 給出數字座標。
    例如(1,2)(3mm,4in,5pt)(6:7)(axis cs:x=8)

  • 給予預定義的節點名稱和錨點名稱。
    例如(current page.south east)(current bounding box.north west)

  • 給出預定義的節點名稱。
    例如(current page)。這相當於(current page.center)

  • 給出預定義的coordinate.
    例如\coordinate(X)at(1,2);\path(X);。 Acoordinate其實是節點,其中的錨點「消失」:(X)(X.center)(X.south)、 且(X.34)都給出相同的結果。

他們的 PGF 對應部分是

  • \pgfpointxy\pgfpointxyz\pgfpointpolar, 等等
  • \pgfpointanchor{#1}{#2}
  • \pgfpointanchor{#1}{center}
  • \pgfpointanchor{#1}{center}

現在你看到在 的定義中\getdistance,只能填入一個節點名稱,而錨點center是強制選擇的。所以

\getdistance{\globalresultb}{current page.north east}{rightmid}

無法編譯,因為current page.north east不再是節點。但

\coordinate (A) at (current page.north east);
\getdistance{\globalresultb}{A}{rightmid}

確實可以編譯,因為(A)是節點的名稱。

什麼樣的輸入取決於你\getdistance。例如在鈦kZ,\tikz@parse@node可以處理(X)(Y.Z)。但不是(1,2)。更一般地說,\tikz@@scan@@no@calculator和 處理(X), (Y.Z), 和(1,2)。甚至更一般的\tikz@@@scan@@absolute都能應付($ (current page.north east) + (0,-10cm) $)。您需要選擇一個。

相關內容