坐标单位为 pt,但 TikZ 中的默认单位为 cm

坐标单位为 pt,但 TikZ 中的默认单位为 cm

我们知道,在 TikZ 中,如果 TikZ 中的坐标中没有提到单位,则采用cm默认设置。

当我提取坐标时,我期望单位为cm。但是 TikZ 将坐标中的单位显示为pt

如果未指定单位,TikZ 如何确定坐标中的测量单位。

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\tikzset{zigzag/.style={decorate,decoration=zigzag}}
\begin{document}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

\begin{tikzpicture}
  \coordinate (c) at (0,-2);
  \coordinate (d) at (4,-2);
  \coordinate (e) at (2,-4);
  \draw[thick,red,zigzag,postaction={
    decoration={
        markings,
        mark=at position 0.7 with { \coordinate (x); },
        mark=at position 0.5 with { \coordinate (singularity); },
    },
    decorate
  }] (-2,0) coordinate(a) -- (2,0) coordinate(b);

  \draw[thick,fill=blue!20] (c) -- (b) -- (d) -- (e) -- cycle;
  \draw[thick,postaction={
    decoration={
        markings,
        mark = at position 0.7 with \coordinate (y);
    },
    decorate
  }] (a) -- (c);
  \draw[thick,red,dashed] (x) -- (y);

  \node[above = 10ex of singularity,red] (sn) {singularity};
  \draw[red,->] (sn) -- ($(singularity)+(0,1)$);
  \ExtractCoordinate{x};
  \node[above] at (\XCoord,\YCoord) {(\XCoord,\YCoord)};
\end{tikzpicture}

\end{document} 

在此处输入图片描述

答案1

您始终可以通过乘以比率1pt/1cm或其倒数将所有内容从 pt 转换为 cm 或转回。(如果这不是您想要的,我很乐意删除该帖子。)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\tikzset{zigzag/.style={decorate,decoration=zigzag}}
\begin{document}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

\begin{tikzpicture}
  \coordinate (c) at (0,-2);
  \coordinate (d) at (4,-2);
  \coordinate (e) at (2,-4);
  \draw[thick,red,zigzag,postaction={
    decoration={
        markings,
        mark=at position 0.7 with { \coordinate (x); },
        mark=at position 0.5 with { \coordinate (singularity); },
    },
    decorate
  }] (-2,0) coordinate(a) -- (2,0) coordinate(b);

  \draw[thick,fill=blue!20] (c) -- (b) -- (d) -- (e) -- cycle;
  \draw[thick,postaction={
    decoration={
        markings,
        mark = at position 0.7 with \coordinate (y);
    },
    decorate
  }] (a) -- (c);
  \draw[thick,red,dashed] (x) -- (y);

  \node[above = 10ex of singularity,red] (sn) {singularity};
  \draw[red,->] (sn) -- ($(singularity)+(0,1)$);
  \ExtractCoordinate{x};
  \node[above] at (\XCoord,\YCoord) {%
  (\pgfmathparse{\XCoord*1pt/1cm}\pgfmathprintnumber{\pgfmathresult}\,cm,%
  \pgfmathparse{\YCoord*1pt/1cm}\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}\,cm)};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容