3

I am writing a Python script to draw large diagrams with TikZ. These diagrams require a (d+1)-dimensional coordinate system with coordinates say a0,a1,...,ad.

How can I define a coordinate system in TikZ of arbitrarily large dimension?

Here is a M(not)WE of what I want to achieve:

\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}

But pdflatex returns the following error:

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.

Now, if I rename a0=a and a1=b there are no more errors:

\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}

The problem seems to be the use of numbers in the keys a0, a1. Could somebody point me in the right direction on where to find the correct syntax for such keys?

  • The problem is the use of numbers in the \defined commands ("regular" command names cannot have numbers, although you could use some \csname \endcsname trivkery to allow numbers), not in the key names; you could use \define@key{meshkeys}{r}{\def\myr{#1}} \define@key{meshkeys}{a0}{\def\myaz{#1}} \define@key{meshkeys}{a1}{\def\myai{#1}} \tikzdeclarecoordinatesystem{mesh}% {% \setkeys{meshkeys}{#1}% \pgfpointadd{\pgfpointxy{\myr*(\myaz+\myai)*cos(45)}% {\myr*(\myaz+\myai)*sin(45)}}% } – Gonzalo Medina Jul 10 '15 at 15:43
  • I see... Ideally I would like to use numbers to have nicer code, but I guess can live with it. Thanks! Feel free to post your comment as an answer so I cann accept it. – Gustavo Jul 10 '15 at 15:49
3

I find that part of the manual a bit out of place as it looks like it is from T.Tantau's beamer days. Anyway here is, in my opinion, more structured (TikZic ?!) version of that using 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}
  • Will this allow to use \mya0, \mya1, ... for the keys? – Gustavo Jul 10 '15 at 17:23
  • @Gustavo Yes key names can have numbers because we don't touch the inner macros but only names. – percusse Jul 10 '15 at 17:23
  • @percusee I still get "ERROR: Use of \mya doesn't match its definition." when using \mya0 and \mya1 (replace \mya=\mya1 and \myb=\mya2) – Gustavo Jul 10 '15 at 17:26
  • @Gustavo You don't touch the macros only change the key names. You really wouldn't want to use numbers in the macros. Call them \myaone,\myatwo because you won't interact with them on the frontend. The key name doesn't need to match the macro name it is stored., – percusse Jul 10 '15 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.