2

Possible Duplicate:
TikZ: Variable arithmetic result in node name

\documentclass{standalone}

\usepackage{tikz,pgf}

\usetikzlibrary{decorations.markings,%
    snakes,fit,scopes,arrows,calc,shapes.misc,%
    shapes.arrows,chains,matrix,positioning,decorations.pathmorphing,shapes,backgrounds,%
    decorations.text}

\begin{document}

\newcommand{\bbrect}[3]{
    \pgfmathsetmacro\result{#1+1};
    \node[rectangle,draw,fit=(A-#1-\result.north west) (A-#1-#2.south east), inner sep = 0pt] (block#3) {}
}

\begin{tikzpicture}
    \ttfamily
    \begin{scope}
            \matrix (A) [matrix of nodes, ampersand replacement = \&] {
                {0} \& {0} \& {1} \& {0} \& {1} \& {0}\\
                {0} \& {0} \& {0} \& {1} \& {1} \& {1}\\
                {0} \& {0} \& {0} \& {0} \& {1} \& {0}\\
                {0} \& {0} \& {0} \& {0} \& {0} \& {1}\\
                {0} \& {0} \& {0} \& {0} \& {0} \& {1}\\
                {0} \& {0} \& {0} \& {0} \& {0} \& {0}\\
            };
    \end{scope}
    \bbrect{1}{5}{1};
\end{tikzpicture}

\end{document}

Inside the newcommand the entry A[i,i+1] is accessed, but this gives "illegal unit of measure" error.

  • 1
    Use \pgfmathtruncatemacro instead of \pgfmathsetmacro. (Disclaimer: this might only work with PGF2.10, I don't remember when the \pgfmathtruncatemacro macro was introduced.) – Loop Space Jan 9 '12 at 8:06
  • 1
    Here's a few related questions: tex.stackexchange.com/a/28242/86 tex.stackexchange.com/q/29527/86 tex.stackexchange.com/q/27822/86 tex.stackexchange.com/q/4275/86 (The first and second contain explanations as to why to do this.) – Loop Space Jan 9 '12 at 8:11
  • As this is closely related to the questions that I linked to, and an answer here would not add anything to what is already at those questions, we'll (probably) close this question as a duplicate of one of those to make it easier for others to find the answer to this sort of problem (which, as you can probably guess, is quite a common one!). – Loop Space Jan 9 '12 at 9:02

Browse other questions tagged or ask your own question.