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 am trying to create a multipart node to represent a byte

I wish I could write \Byte{0 1 0 0 1 1 0 0} or \Byte {0, 1, 0, 0, 1, 1, 0, 0} and obtain enter image description here

\documentclass[12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\newcommand{\Byte}{
\node [rectangle split,rectangle split parts=8, rectangle split horizontal,draw ] at (2,2)
{\nodepart{one}0\nodepart{two}1\nodepart{three}0\nodepart{four}0\nodepart{five}1\nodepart{six}1\nodepart{seven}0\nodepart{eight}0};
}
\begin{document}

\begin{tikzpicture}
\Byte
\end{tikzpicture}
\end{document}
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Here's one way to do it with the second of your desired invocations wherein we use a \foreach to built the node contents and thus prepare the node before calling it.

\documentclass[12pt]{standalone}
%\url{http://tex.stackexchange.com/q/67923/86}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\def\numtext#1{%
  \ifcase#1\or one\or two\or three\or four\or five\or six\or seven\or eight\or nine\or ten\or
eleven\or twelve\or thirteen\or fourteen\or fifteen\or sixteen\or seventeen\or eighteen\or nineteen\or twenty\or Lots\fi}

\newcommand{\Byte}[1]{
\def\nodecontents{}%
\foreach[count=\l] \k in {#1} {
    \xappto{\nodecontents}{\noexpand\nodepart{\numtext{\l}}\k}
}
\node [rectangle split,rectangle split parts=8, rectangle split horizontal,draw ] at (2,2)
{\nodecontents};
}
\begin{document}

\begin{tikzpicture}
\Byte {0, 1, 0, 0, 1, 1, 0, 0}
\end{tikzpicture}
\end{document}

Result:

multibyte TikZ command

share|improve this answer
    
There is a fmtcount package for that. tex.stackexchange.com/questions/28326/… –  percusse Aug 20 '12 at 15:23
    
@percusse Sadly, not expandably - at least, not with my simple tests. So I can't use it inside the expansion step. –  Andrew Stacey Aug 20 '12 at 15:33
    
oops.didn't test it sorry. –  percusse Aug 20 '12 at 15:35
    
@percusse Don't apologise. I didn't know of that package so I've learnt something that I'm sure I'll use someday. –  Andrew Stacey Aug 20 '12 at 15:44
    
Merci, I use this answer –  rpapa Aug 20 '12 at 16:14
add comment

Using \def you can write things like \Byte(0,1,0,0,1,0,1,1):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\def\Byte(#1,#2,#3,#4,#5,#6,#7,#8){
\node [rectangle split,rectangle split parts=8, rectangle split horizontal,draw ] at (2,2)
{\nodepart{one}#1\nodepart{two}#2\nodepart{three}#3\nodepart{four}#4\nodepart{five}#5\nodepart{six}#6\nodepart{seven}#7\nodepart{eight}#8};
}

\begin{document}

\begin{tikzpicture}
\Byte(0,1,0,0,1,0,1,1)
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
merci, I try it –  rpapa Aug 20 '12 at 15:14
    
can we use \newcommand instead of \def? –  rpapa Aug 20 '12 at 15:16
    
@rpapa \newcommand doesn't allow for delimited arguments, \def does. –  Gonzalo Medina Aug 20 '12 at 15:23
    
@rpapa No we cannot, since the command has a special formatting (the arguments are seperated by commas instead of put in braces). With \newcommand only the default is possible. –  tohecz Aug 20 '12 at 15:24
add comment

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.