是否可以使用 tikz 中的角度库提供的 \pic 命令,而不使用坐标?
如果我使用类似的东西,它可以正常工作。
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,5);
\coordinate (C) at (5,0);
\node [left] at (A) {$A$};
\node [left] at (B) {$B$};
\node [right] at (C) {$C$};
\draw (A) -- (B) -- (C);
\pic [draw] {angle = A--B--C};
\end{tikzpicture}
但是,如果我使用这个,
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,5);
\coordinate (C) at (5,0);
\node [left] at (A) {$A$};
\node [left] at (B) {$B$};
\node [right] at (C) {$C$};
\draw (A) -- (B) -- (C);
\pic [draw] {angle = (0,0)--(0,5)--(5,0)};
\end{tikzpicture}
这是行不通的。
如果我能使用这个,那就太好了,这样我就可以使用点而不用命名它们。
答案1
angle
需要名称,而不是坐标。名称参数直接解析为
\pgfpointanchor
,来自tikzlibraryangles.code.tex
:
\def\tikz@lib@angle@parse#1--#2--#3\pgf@stop{%
% ...
\pgf@process{\pgfpointanchor{#2}{center}}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{\pgfpointanchor{#1}{center}}%
\pgf@xb=\pgf@x%
\pgf@yb=\pgf@y%
\pgf@process{\pgfpointanchor{#3}{center}}%
% ...
}
\pgfpointanchor
期望节点名称作为第一个参数。
因此,库也需要大量修补才能支持坐标规范。以下示例修补了 libary angles
。 的参数angle
需要用花括号括起来,以便向解析器隐藏逗号pgfkeys
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{angles}
\usepackage{etoolbox}
\makeatletter
% Helper macros
\def\tikz@lib@angle@def@coord#1{%
\ifx(#1\relax
\coordinate(tikz@lib@angle@tmp)at#1;%
\else
\coordinate(tikz@lib@angle@tmp)at(#1);%
\fi
}
\def\tikz@lib@angle@coord#1{%
\pgf@process{%
\ifx(#1\relax
\tikz@scan@one@point\@firstofone#1\relax
\else
\pgfpointanchor{#1}{center}%
\fi
}%
}
% Patching
\patchcmd\tikz@lib@angle@background{#2}{tikz@lib@angle@tmp}{}{%
\errmessage{Cannot patch \string\tikz@lib@angle@background}%
}
\patchcmd\tikz@lib@angle@foreground{#2}{tikz@lib@angle@tmp}{}{%
\errmessage{Cannot patch \string\tikz@lib@angle@foreground}%
}
\pretocmd\tikz@lib@angle@background{\tikz@lib@angle@def@coord{#2}}{}{%
\errmessage{Cannot prepend \string\tikz@lib@angle@background}%
}
\pretocmd\tikz@lib@angle@foreground{\tikz@lib@angle@def@coord{#2}}{}{%
\errmessage{Cannot prepend \string\tikz@lib@angle@foreground}%
}
\patchcmd\tikz@lib@angle@parse{%
\pgf@process{\pgfpointanchor{#2}{center}}%
}{%
\tikz@lib@angle@coord{#2}%
}{}{%
\errmessage{Cannot patch \string\tikz@lib@angle@parse}%
}
\patchcmd\tikz@lib@angle@parse{%
\pgf@process{\pgfpointanchor{#1}{center}}%
}{%
\tikz@lib@angle@coord{#1}%
}{}{%
\errmessage{Cannot patch \string\tikz@lib@angle@parse}%
}
\patchcmd\tikz@lib@angle@parse{%
\pgf@process{\pgfpointanchor{#3}{center}}%
}{%
\tikz@lib@angle@coord{#3}%
}{}{%
\errmessage{Cannot patch \string\tikz@lib@angle@parse}%
}
% Better support for the comma in the value for angle:
\tikzset{
pics/angle/.style = {
setup code = {\tikz@lib@angle@parse#1\pgf@stop},
background code = {\tikz@lib@angle@background#1\pgf@stop},
foreground code = {\tikz@lib@angle@foreground#1\pgf@stop},
},
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (0,5);
\coordinate (C) at (5,0);
\node [left] at (A) {$A$};
\node [left] at (B) {$B$};
\node [right] at (C) {$C$};
\draw (A) -- (B) -- (C);
% \pic [draw] {angle=A--B--C};
\pic [draw] {angle={(0,0)--(0,5)--(5,0)}};
\end{tikzpicture}
\end{document}
答案2
您还可以通过插入angle
自定义来采用另一种方式pic
。
\documentclass[tikz]{standalone}
\usetikzlibrary{angles}
\makeatletter
\tikzset{pics/anglecoords/.style = {
setup code = {
\def\minicoordparser##1--##2--##3\pgf@stop{
\coordinate (A-a) at ##1;\coordinate (B-b) at ##2;\coordinate (C-c) at ##3;
}
\minicoordparser#1\pgf@stop
\tikz@lib@angle@parse{A-a}--{B-b}--{C-c}\pgf@stop
},
background code = \tikz@lib@angle@background{A-a}--{B-b}--{C-c}\pgf@stop,
foreground code = \tikz@lib@angle@foreground{A-a}--{B-b}--{C-c}\pgf@stop,
},
}
\makeatother
\begin{document}
\begin{tikzpicture}[]
\draw (0,0) --(0,5) --(5,0) -- cycle;
\pic[draw]{anglecoords = {(0,0)--(0,5)--(5,0)}};
\pic[draw,fill=yellow]{anglecoords = {(5,0)--(0,0)--(0,5)}};
\end{tikzpicture}
\end{document}