6

What is the best way to declare a constant to be used within TikZ? I am using \pgfmathsetmacro because I have seen others do it on this site. A constant should be a simple thing, so I am bit confused why it should be necessary to use a low level(pgf) command for this!? When using the ellipse command I have a strange problem where I need to wrap the constant in an extra set of {} when used like this:

\draw (6,6) ellipse (\r and 1);.

Error:

! Package PGF Math Error: Unknown operator 'a' or 'an' (in '1and 1').

Why is that?

Related: Problems with TikZ calculations

MWE:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\r}{1}
  \draw (0,0) ellipse (1 cm and 1 cm);    %ok
  \draw (1,1) ellipse (1 and 1);          %ok
  \draw (2,2) ellipse (1 cm and \r cm);   %ok
  \draw (3,3) ellipse (1 and \r);         %ok
  \draw (4,4) ellipse (\r cm and 1 cm);   %ok
  \draw (5,5) ellipse ({\r} and 1);       %ok
  \draw (6,6) ellipse (\r and 1);         %not ok
  \draw (7,7) ellipse (\r{} and 1);       %ok
\end{tikzpicture}
\end{document}

Edit: From @marmot's comment and experiment I have found, that \constant{} also works as in \draw (7,7) ellipse (\r{} and 1);... but what is the correct way to declare and use a constant - I can not find it in the TikZ manual. Should I always use \constant{}, or maybe only when problems arise?

  • 3
    \r and expands to 1and which the parser cannot make sense of. – user121799 May 8 '18 at 3:51
  • @marmot: Yes - but who ate the space? What is correct syntax? I do not suppose it is \r{} !? Like in \newcommand{\abc}{\emph{abc}} \abc{}. – hpekristiansen May 8 '18 at 4:03
  • 2
    Isn't that just the usual behavior of TeX? \documentclass{article} \begin{document} Hello Car\LaTeX how's your day? \end{document} – user121799 May 8 '18 at 4:10
  • @marmot: Yes - I try to always use \LaTeX{} (as I do not use xspace). -but does \pgfmathsetmacro work the same way as commands? – hpekristiansen May 8 '18 at 15:03
  • 1
    It has nothing to do specifically with \pgfmathsetmacro but with the fact that \r is a macro and trailing spaces get eaten up, just like spaces after \LaTeX do. – user121799 May 8 '18 at 16:43
6

It's from the old parts of the code and this is something that should have been done via keys. In tikz.code.tex you can find this

\def\tikz@@@circle(#1){%
  {%
    \pgftransformshift{\tikz@last@position}%
    \pgfutil@in@{ and }{#1}% <==== This is the part that it looks for a pattern
    \ifpgfutil@in@%
      \tikz@@ellipseB(#1)%
    \else%
      \tikz@do@circle{#1}{#1}%
    \fi%
  }%
  \tikz@scan@next@command%
}

Since there is whitespace around the and you need to have whitespace around it.

This usage is deprecated by the developers and they recommend ellipse [x radius=<>, y radius=<>] syntax.

This is both mentioned in the manual and also in the source code comments

% Syntax for circles:
% circle [options] % where options should set, at least, radius
% circle (radius) % deprecated
%
% Syntax for ellipses:
% ellipse [options] % identical to circle.
% ellipse (x-radius and y-radius) % deprecated
%
% radii can be dimensionless, then they are in the xy-system

For example in the positioning syntax, you don't have this problem

%\usetikzlibrary{positioning}

\begin{tikzpicture}
\node (a) {a};
\node[above=1of a] {b};% 1cmof a also works
\end{tikzpicture}

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.