5

I would like to use pgf's math operations to refer to matrix coodinates in tikz. This works for the second coordinate, but apparently miraculously:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{equation}
   \begin{tikzpicture}
      [description/.style={fill=white,inner sep=2pt},baseline=(current bounding box.center)]
      \matrix (m) [matrix of math nodes, row sep=1em, column sep=1.5em, text height=1.5ex, text depth=0.25ex]
      {
      0 & \Omega^k_X                & Z k k & Z k {k-1} & \cdots & Z k 1 & Z k 0 & 0\phantom .\\
      0 & \Lambda^k \mathcal E^\vee & Z k k & Z k {k-1} & \cdots & Z k 1 & Z k 0 & 0.\\
      };
      \foreach \a in {2,3,...,8} {
      \pgfmathsetmacro{\myresult}{\a-1}
      \draw[->] (m-1-\myresult) -- (m-1-\a);
      \draw[->] (m-2-\myresult) -- (m-2-\a);
      };
   \end{tikzpicture}
\end{equation}
\end{document} 

The following, however, fails spectacularly.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{equation}
   \begin{tikzpicture}
      [description/.style={fill=white,inner sep=2pt},baseline=(current bounding box.center)]
      \matrix (m) [matrix of math nodes, row sep=1em, column sep=1.5em, text height=1.5ex, text depth=0.25ex]
      { 
      0 \\
      \Omega^k_X  \\
      Z k k \\
      Z k {k-1} \\
      cdots \\
      Z k 1 \\
      Z k 0 \\
      };
      \foreach \a in {2,3,...,7} {
      \pgfmathsetmacro{\myresult}{\a-1}
      \draw[->] (m-\myresult-1) -- (m-\a-1);
      };
   \end{tikzpicture}
\end{equation}
\end{document} 

Apparently, this is because \myresult is 1.0, 2.0, etc, and tikz does not like. An otherwise fruitless google search turned up \pgfmathfloatint, but tikz barfs on \usetikzlibrary{fpu}. Is there any way to turn 1.0 into 1?

1
  • 1
    You should post always complete, compilable examples so that people can simple cut&paste the code and run it for themselves. Otherwise people have to guess which packages you are using. This is especially true for TikZ which has many libraries. Mar 30, 2011 at 20:03

1 Answer 1

6

There is \pgfmathtruncatemacro which works like \pgfmathsetmacro but truncates the result to an integer.

BTW: The reason why m-1-\myresult works but m-\myresult-1 isn't is because the first one will expand to m-1-2.0 which is iterpreted as node m-1-2 with anchor 0 (the . is a node.anchor separator). The 0 and any other number is taken as an angle, i.e. 0 degree (see also my answer to How to draw lines between nodes that point at the node center, but stop at the nodes edge). So it kind of works.

The second form m-\myresult-1 will expand to m-2.0-1 which is taken as node m-2 and anchor 0-1, where both most likely don't exit. The node might be, but there is no anchor with that name.

4
  • the problem here comes from the names of nodes. It's also possible to add .0 in these names but only for the fun :) Another frequent problem is to add a blank (a ) instead of (a) Mar 30, 2011 at 20:12
  • Thanks for the nice explanation of exactly what was going wrong :)
    – JRG
    Mar 30, 2011 at 20:15
  • That's a useful command to know about. I've been using \pgfmathparse{int(... expression ...)}\edef\macro{\pgfmathresult} to accomplish the same end, but from now on I'll use this. Mar 30, 2011 at 20:51
  • @Andrew: :-) I used to have some #1.#2{#1} macro to truncate it. I tried int before and that wasn't good enough as well. Assigning it to a counter didn't removed the fraction... Mar 30, 2011 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .