我正在编写一个 Python 脚本来使用 TikZ 绘制大型图表。这些图表需要一个 (d+1) 维坐标系,坐标为 a0、a1、...、ad。
如何在 TikZ 中定义任意大尺寸的坐标系?
以下是我想要实现的 M(not)WE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\define@key{meshkeys}{r}{\def\myr{#1}}
\define@key{meshkeys}{a0}{\def\mya0{#1}}
\define@key{meshkeys}{a1}{\def\mya1{#1}}
\tikzdeclarecoordinatesystem{mesh}%
{%
\setkeys{meshkeys}{#1}%
\pgfpointadd{\pgfpointxy{\myr*(\mya0+\mya1)*cos(45)}%
{\myr*(\mya0+\mya1)*sin(45)}}%
}
\begin{document}
\begin{tikzpicture}
\node at (mesh cs:a0=0,a1=0,r=1){X};
\end{tikzpicture}
\end{document}
但是 pdflatex 返回以下错误:
ERROR: Use of \mya doesn't match its definition.
--- TeX said ---
<argument> \myr *(\mya 0
+\mya 1)*cos(45)
l.20 \node at (mesh cs:a0=0,a1=0,r=1)
{X};
--- HELP ---
It's probably one of the picture-drawing commands, and you have used
the wrong syntax for specifying an argument. If it's \@array that
doesn't match its definition, then there is something wrong in an
@-expression in the argument of an array or tabular
environment---perhaps a fragile command that is not \protect'ed.
现在,如果我重命名 a0=a 和 a1=b,则不再出现错误:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-cd}
% coordinate system
\usetikzlibrary{calc}
\makeatletter
\define@key{meshkeys}{r}{\def\myr{#1}}
\define@key{meshkeys}{a}{\def\mya{#1}}
\define@key{meshkeys}{b}{\def\myb{#1}}
\tikzdeclarecoordinatesystem{mesh}%
{%
\setkeys{meshkeys}{#1}%
\pgfpointadd{\pgfpointxy{\myr*(\mya+\myb)*cos(45)}%
{\myr*(\mya+\myb)*sin(45)}}%
}
\begin{document}
\begin{tikzpicture}
\node at (mesh cs:a=1,b=1,r=1){X};
\end{tikzpicture}
\end{document}
问题似乎出在 a0、a1 键中使用了数字。有人能告诉我在哪里可以找到这些键的正确语法吗?
答案1
我觉得手册的这一部分有点不合适,因为它看起来像是 T.Tantaubeamer
时代的。无论如何,在我看来,这是更结构化(TikZic ?!)的版本,使用pgfkeys
\documentclass[tikz]{standalone}
\makeatletter
\tikzset{mishm@sh/.cd,
r/.store in=\myr,
a/.store in=\mya,
b/.store in=\myb
}
\tikzdeclarecoordinatesystem{mesh}{%
\tikzset{mishm@sh/.cd,#1}%
\pgfpointadd{\pgfpointxy{\myr*(\mya+\myb)*cos(45)}{\myr*(\mya+\myb)*sin(45)}}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node at (mesh cs:a=0,b=0,r=1) {X};
\draw (mesh cs:a=2,b=1,r=4) -- (mesh cs:a=-3,b=-2,r=2);
\end{tikzpicture}
\end{document}