Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to define:

 anglearray(\A,\I,\L) = array(\A,Mod(\I,\L)) + 360*floor((\I+0.1)/\L);

but I get strange errors about "! Missing number, treated as zero." and the "array" part appears to evaluate to 0. Here is a fairly minimal example that shows a really dumb-looking work-around (that more or less loses any of the advantages of writing it as a function).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\showB}[1]{\typeout{\string#1=\meaning#1}}.
\begin{tikzpicture}[
    declare function={
      anglearray1(\AI,\I,\L) = \AI + 360*floor((\I+0.1)/\L);
      anglearray2(\A,\I,\L) = array(\A,Mod(\I,\L)) + 360*floor((\I+0.1)/\L);
    }
]
% Spin around a circle stopping at 10 and 190 degrees
\foreach \i in {-10,...,10} {
  \pgfmathsetmacro{\X}{anglearray1(array({10,190},Mod(\i,2)),\i,2)}
  \showB\X
}
% Spin around a circle stopping at 10 and 190 degrees; doesn't work
\foreach \i in {-10,...,10} {
  \pgfmathsetmacro{\X}{anglearray2({10,190},\i,2)}
  \showB\X
}
\end{tikzpicture}
\end{document}

I'm using PGF 2.10 from TexLive 2011 of today.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

As @JackSchmidt pointed out, with public pgf functions, a pgf array {1,2,3} is parsed and transformed to {1}{2}{3} before being handled by private pgf functions. So you need to access those private functions (see the pgfmanual, Math engine, Customizing the math engine).

\documentclass{article}
\usepackage{tikz}
\begin{document}

\tikzset{%
  declare function={%
    anglearray1(\AI,\I,\L) = \AI + 360*floor((\I+0.1)/\L);}}

% Spin around a circle stopping at 10 and 190 degrees
\foreach \i in {-10,...,10} {%
  \pgfmathsetmacro{\X}{anglearray1(array({10,190},Mod(\i,2)),\i,2)}
  \X\par}

\noindent\hrulefill

\makeatletter
\pgfmathdeclarefunction{anglearray2}{3}{%
  % #1 an array (represented in pgfmath internal format, ie 
  % {<index 0>}{<index 1>}...{<index N-1>})
  % #2 \I
  % #3 \L
  \pgfmathparse{Mod(#2,#3)}
  % The @ is needed (see pgfmanual, math engine, custumizing the math
  % engine) 
  \pgfmatharray@{#1}{\pgfmathresult}
  \pgfmathparse{anglearray1(\pgfmathresult,#2,#3)}}
\makeatother

% Spin around a circle stopping at 10 and 190 degrees; doesn't work
\foreach \i in {-10,...,10} {
  \pgfmathsetmacro{\X}{anglearray2({10,190},\i,2)}
  \X\par}

\end{document}
share|improve this answer

Here is a goofy answer. It appears that by the time your code in declare function is called, the array has already been turned into a sequence of braced entries, so the PGF array {1,2,3,4} becomes the sequence of macro aguments {1}{2}{3}{4}. Obviously none of the PGF math functions work on such things, so one has to use more basic tex programming. Such programming appears to be well beyond me, so instead of worrying about the "tail end" of things, I instead work on the "head":

\def\anglearray(#1,#2,#3){array({#1},Mod(#2,#3)) + 360*floor((#2+0.1)/#3)}

This is used as:

\pgfmathsetmacro{\X}{\anglearray({10,190},\i,2)}

Notice the leading backslash since this is a tex macro, not a pgf math function.

share|improve this answer
    
<cite>Obviously none of the PGF math functions work on such things</cite>: I don't think so. See the "private" functions and my answer. –  cjorssen Nov 28 '11 at 20:57
    
@cjorssen: none of the documented functions work on such things. I gave your answer a +1 since it is clearly the right way to do things. It would be good to document these functions in the manual (especially regarding arrays). The "dim" function would also be amazing. –  Jack Schmidt Nov 28 '11 at 21:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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