TikZ:如何确定两个坐标之间的向量

TikZ:如何确定两个坐标之间的向量

给定两个坐标,比如X.south eastX.south west,是否可以确定两者之间的长度或向量?

在 MWE 中,宽度是使用传统的 TeX 技术计算的,但显然有点浪费精力,因为 PGF 可能已经完成了所有这些计算。

在此处输入图片描述

\documentclass{article}
\usepackage{pgf,tikz}
\makeatletter
\begin{document}
\def\convert@cx#1{\the#1}
\newlength\xheight@cx
\newlength\xlength@cx
\newlength\xdepth@cx
\newlength\xtotal@cx
\newsavebox{\fontbox}
%
\def\drawfontbox#1{%
  {\upshape\Huge
   \savebox{\fontbox}{#1}
   \pgfmathsetlength{\xheight@cx}{\ht\fontbox}
   \pgfmathsetlength{\xlength@cx}{\wd\fontbox}
   \pgfmathsetlength{\xdepth@cx}{\dp\fontbox}
   \pgfmathsetlength{\xtotal@cx}{\xdepth@cx+\xheight@cx}
  \begin{tikzpicture}[scale=1]
    \node[rectangle,draw,inner sep=0pt,outer sep=0pt] (X){#1};
    \draw[red, line width=0.4pt] (X.text)  circle(1pt)[fill=red] -- (X.base east);
    \draw[|<->|,>=latex] ([yshift=5pt] X.north west)
       --([yshift=5pt] X.north east) node [above=-5pt,midway,
       font={\tiny}]{width = \convert@cx{\xlength@cx}};
    % draw the xheight
    \draw[|<->|,>=latex]([xshift=-5pt]X.base west)
          --([xshift=-5pt] X.north west)
          node [left,midway,font={\tiny}] {x-height=\convert@cx{\xheight@cx}};
%   draw depth
    \draw[-|,>=latex]([xshift=-5pt]X.base west)
          --([xshift=-5pt] X.south west)
          node [left,midway,font={\tiny}] {depth=\convert@cx{\xdepth@cx}};
    \draw[<-,>=latex]([xshift=-5pt]X.south west)
          --++(0,-8pt);
%   draw total height
%
\draw[|<->|,>=latex]([xshift=5pt]X.north east)
          --([xshift=5pt] X.south east)
          node [right,midway,font={\tiny}] {height=\convert@cx{\xtotal@cx}};
  \end{tikzpicture}}
}
\drawfontbox{QWERTY}
\end{document}

答案1

let带有和 的示例veclen

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (4,2);
\draw let
  \p1 = (a),
  \p2 = (b),
  \n1 = {veclen((\x2-\x1),(\y2-\y1))}
in
  (a) to[sloped,above] node{\n1} (b);
\end{tikzpicture}

\end{document}

在此处输入图片描述

对原始代码的改编:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\drawfontbox#1{%
 \begin{tikzpicture}[%
  MyArr/.style={|<->|,>=latex},
  MyLabel/.style={font={\tiny}}]
    \node[rectangle,draw,inner sep=0pt,outer sep=0pt] (X){\Huge #1};
    \draw 
      let
        \p1 = (X.base west),
        \p2 = (X.south west),
        \p3 = (X.south east),
        \p4 = (X.north west),
        \p5 = (X.north east),
        \n1 = {veclen(\x5-\x4,\y5-\y4)}, % width 
        \n2 = {veclen(\x5-\x3,\y5-\y3)}, % height
        \n3 = {veclen(\x4-\x1,\y4-\y1)}, % x-height
        \n4 = {veclen(\x2-\x1,\y2-\y1)}  % depth
      in
        ([yshift=5pt] X.north west)
          edge[MyArr] node [above=-1pt,MyLabel]{width = \n1}
          ([yshift=5pt] X.north east)
        ([xshift=-5pt]X.base west)
          edge[MyArr] node [left,MyLabel] {x-height=\n3}
          ([xshift=-5pt] X.north west)
        ([xshift=-5pt]X.base west)
          edge[-|,>=latex] node [left,MyLabel] {depth=\n4}
          ([xshift=-5pt] X.south west)
        ([xshift=5pt]X.north east)
          edge[MyArr] node [right,MyLabel] {height=\n2}
          ([xshift=5pt] X.south east)
          ;
    \draw[red, line width=0.4pt] (X.text)  circle(1pt)[fill=red] -- (X.base east);
\end{tikzpicture}
}
\begin{document}
 \drawfontbox{QWERTY}

 \drawfontbox{Lorem Ipsum}

 \drawfontbox{What?}
\end{document}

答案2

如果您想要 TeX 框的尺寸,这个答案可能会有用

\documentclass{article}
\usepackage{tikz}

\begin{document}  
\newsavebox{\ftbox} 
\savebox{\ftbox}{\Huge QWERTY}

 \pgfmathparse{width("\usebox\ftbox")} \pgfmathresult

 \pgfmathparse{height("\usebox\ftbox")} \pgfmathresult

 \pgfmathparse{depth("\usebox\ftbox")} \pgfmathresult  
\end{document} 

在此处输入图片描述

答案3

正如 Altermundus 所评论的,还有另一个可以与任意矩形节点一起使用的方法:)根据需要添加更多自定义长度。

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\anchordistances}[1]{
\pgfpointdiff{\pgfpointanchor{#1}{base}}{\pgfpointanchor{#1}{south}}\pgfmathsetmacro{\mydepth}{veclen(\pgf@x,\pgf@y)}
\pgfpointdiff{\pgfpointanchor{#1}{north west}}{\pgfpointanchor{#1}{north east}}\pgfmathsetmacro{\mywidth}{veclen(\pgf@x,\pgf@y)}
\pgfpointdiff{\pgfpointanchor{#1}{south west}}{\pgfpointanchor{#1}{north west}}\pgfmathsetmacro{\myheight}{veclen(\pgf@x,\pgf@y)}
\pgfpointdiff{\pgfpointanchor{#1}{base}}{\pgfpointanchor{#1}{north}}\pgfmathsetmacro{\myxheight}{veclen(\pgf@x,\pgf@y)}
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[draw,inner sep=0,outer sep=0] (X) {\upshape\Huge QWERTY};
\draw[red, line width=0.4pt] (X.text)  circle(1pt)[fill=red] -- (X.base east);
\anchordistances{X}

    % draw the xheight
    \draw[|<->|,>=latex]([xshift=-5pt]X.base west)
          --([xshift=-5pt] X.north west)
          node [left,midway,font={\tiny}] {x-height=\myxheight};
    \draw[-|,>=latex]([xshift=-5pt]X.base west)
          --([xshift=-5pt] X.south west)
          node [left,midway,font={\tiny}] {depth=\mydepth};
    \draw[<-,>=latex]([xshift=-5pt]X.south west)
          --++(0,-8pt);

\draw[|<->|,>=latex] ([yshift=5pt] X.north west)
       --([yshift=5pt] X.north east) node [above,midway,
       font={\tiny}]{width = \mywidth};
\draw[|<->|,>=latex]([xshift=5pt]X.north east)
          --([xshift=5pt] X.south east)
          node [right,midway,font={\tiny}] {height=\myheight};            
\end{tikzpicture}
\end{document}

在此处输入图片描述

嗯,我忘了pt单位。

答案4

\documentclass{article}
\def\getVLength(#1,#2)(#3,#4){%
  \directlua{tex.print(math.sqrt((#3-#1)^2+(#4-#2)^2))}}

\begin{document}
\getVLength(0,0)(4,2)  
\end{document}

运行lualatex

相关内容