4

I am trying to make a simple figure using TikZ.

Simple figure

Here is my code

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
  \tikzset{memory/.style={draw,minimum width = 100pt, minimum height = 25pt, node distance = 0}}

  % Draw memory cells with addresses.
  \newcounter{add}  % memory address
  \setcounter{add}{202}
  \node [memory, label=left:0x\theadd] (a)  {a};
  \stepcounter{add}  % name of above node
  \newcounter{aboveNode}
  \setcounter{aboveNode}{1}
  \foreach \n in {b,c,d,e,f} {
    \node [memory, label=left:0x\theadd, below = of {\alph{aboveNode}}] (\n)  {\n};
    \stepcounter{add}
    \stepcounter{aboveNode}
  }
\end{tikzpicture}

\end{document}

It seems to me that I should be able to do this without having to use the aboveNode counter. I found out about \tikzlastnode but using it as follows does not work.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
  \tikzset{memory/.style={draw,minimum width = 100pt, minimum height = 25pt, node distance = 0}}

  % Draw memory cells with addresses.
  \newcounter{add}
  \setcounter{add}{202}
  \node [memory, label=left:0x\theadd] (a)  {a};
  \stepcounter{add}
  \foreach \n in {b,c,d,e,f} {
    \node [memory, label=left:0x\theadd, below = of \tikzlastnode] (\n)  {\n};
    \stepcounter{add}
  }
\end{tikzpicture}

\end{document}

LaTeX gives the error

ERROR: Undefined control sequence.

--- TeX said ---
<argument> of \tikzlastnode 

l.20       }

Is there some way to access the last node drawn? Or a simpler way to draw my diagram? At the moment I use more lines than if I had drawn each node manually.

UPDATE

As pointed out in the answer below, remember can be used to get rid of the aboveNode counter. Can the add counter be similarly eliminated?

  • Welcome to TeX.SX! – jub0bs Jun 24 '14 at 17:10
  • Thanks! I am a frequent visitor and find the site extremely useful. This is the first time I have not found a solution to my problem here and had to post my own question. – wsaleem Jun 24 '14 at 17:12
  • You came to the right place, and your first question is formulated in an examplary fashion :) – jub0bs Jun 24 '14 at 17:19
  • This diagram could be done with a tabular, do you need a TiKZ solution for any special reason? – Ignasi Jun 25 '14 at 11:29
  • I am getting into TikZ so I thought to start off with something simple. Also, what I showed above is part of the eventual diagram which contains another table and arrows between the cells of the tables. TikZ provides good arrows though I am sure there are other options. It seems to me that I can get most diagramming tasks done with just TikZ, whereas I would otherwise need to learn several different packages for different tasks. – wsaleem Jun 25 '14 at 17:10
5

Updated You can use the remember key to access the name of the last node, and use count key to replace the add counter. For people still using PGF 2.10, chaning the variable list from {b,c,d,e,f} to {b,...,f} will overcome a bug in that version.

MWE

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
  \tikzset{memory/.style={draw,minimum width = 100pt, minimum height = 25pt, node distance = 0}}

  % Draw memory cells with addresses.
  \node [memory, label=left:0x{202}] (a)  {a};
  \foreach \n[remember=\n as \lastn (initially a), count=\ctr from 203] in {b,...,f} {
    \node [memory, label=left:0x\ctr, below = of \lastn] (\n)  {\n};
  }
\end{tikzpicture}

\end{document}

Output

enter image description here

| improve this answer | |
  • 2
    Perhaps you should use \lastn instead of \j; \lastn would be more descriptive. – jub0bs Jun 24 '14 at 17:22
  • Very strange. I cannot post a picture of the output in the comment but on my system your code draws node 'a' and then draws nodes 'b' onward all on top of each other just below 'a'. – wsaleem Jun 24 '14 at 17:23
  • 1
    @wsaleem: Try putting TikZ version: \pgfversion. somewhere in your document and see if your PGF package is up to date. Mine is 3.0.0. – Herr K. Jun 24 '14 at 17:31
  • 3
    @wsaleem It is a known problem and fixed in the new version. Add \def\lastn{a} before the \foreach loop. – percusse Jun 24 '14 at 17:35
  • 1
    @wsaleem Oops, sorry then change the variable list to {b,...,f}. One or the other should work :) It's a strange bug – percusse Jun 24 '14 at 17:47

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.