# blocks attached to each other

For my thesis, I often need to write blocks attached to each other. More explicitely, I want to have blocks as in page 13 of http://eprint.iacr.org/2006/392.pdf.

EDIT: @morbusg. Is it possible to (easily?) fix the following (in order to get what we want)

 \begin{figure}
\centerline{\vbox{%
\offinterlineskip % no lead between lines
\dimen0=4.5em % size of a square cell
\mathsurround0pt % precaution; although I've newer seen it used
\def\cr{\crcr\noalign{\hrule}} % put a rule between every row with \cr
\halign{&\vrule\hbox to\dimen0{$\vcenter to\dimen0{}\hfil\scriptstyle#\hfil$}\crcr
\omit& \omit& \multispan3\hrulefill\crcr\noalign{\kern-.4pt}
% ^omit drawing a box for the first two columns, then, span the next
% three columns drawing the "hat", and finally, back up the amount of
% the width of the rule so that the vertical and horizontal rules
% fit together.
\omit& \omit&\omit & W(-k-1,1) & W(-k,1) & W(-k+1,1) &\omit\vrule \cr
W(-k-4,0) & W(-k-3,0) & W(-k-2,0) & W(-k-1,0) & W(-k,0) & W(-k+1,0) & W(-k+2,0) & W(-k+3,0)&\omit\vrule \cr
}
}}
\caption{}
\end{figure}


And also

\begin{figure}[H]

\centerline{\vbox{%
\offinterlineskip % no lead between lines
\dimen0=5em % size of a square cell
\mathsurround0pt % precaution; although I've newer seen it used
\def\cr{\crcr\noalign{\hrule}} % put a rule between every row with \cr
\halign{&\vrule\hbox to\dimen0{$\vcenter to\dimen0{}\hfil\scriptstyle#\hfil$}\crcr
\omit& \omit& \multispan3\hrulefill\crcr\noalign{\kern-.4pt}
% ^omit drawing a box for the first two columns, then, span the next
% three columns drawing the "hat", and finally, back up the amount of
% the width of the rule so that the vertical and horizontal rules
% fit together.
\omit        & W(k-1,2n+2)   & W(k,2n+2) & W(k+1,2n+2) & ? &\omit\vrule \cr
? & W(k-1,2n+1) & W(k,2n+1) & W(k+1,2n+1)   & ? &\omit\vrule \cr
? & W(k-1,2n)   & W(k,2n)   & W(k+1,2n)   & ?  &\omit\vrule \cr
? & W(k-1,2n-1) & W(k,2n-1) & W(k+1,2n-1)   & ? &\omit\vrule \cr
}
}}
\label{Sdouble}
\end{figure}

-
What have you already tried? –  Stephan Lehmke Mar 26 '12 at 12:29
This is an ideal place to use xcoffins. –  rdhs Mar 26 '12 at 12:36
The & character is the column separator character. The \omit omits the preamble definition taking place in the cell it is called in. The \multispan takes a number as its first argument which tells how many columns it ought to span, and the \hrulefill fills the cell content with a rule (/line). –  morbusg Apr 23 '12 at 11:05

Here's another:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[34]
\begin{figure}
\centerline{\vbox{%
\offinterlineskip % no lead between lines
\dimen0=4em % size of a square cell
\mathsurround0pt % precaution; although I've newer seen it used
\def\cr{\crcr\noalign{\hrule}} % put a rule between every row with \cr
\halign{&\vrule\hbox to\dimen0{$\vcenter to\dimen0{}\hfil\scriptstyle#\hfil$}\crcr
\omit& \omit& \multispan3\hrulefill\crcr\noalign{\kern-.4pt}
% ^omit drawing a box for the first two columns, then, span the next
% three columns drawing the "hat", and finally, back up the amount of
% the width of the rule so that the vertical and horizontal rules
% fit together.
\omit& \omit& (k-1,1) & (k,1) & (k+1,1) &\omit\vrule \cr
(k-3,0) & (k-2,0) & (k-1,0) & (k,0) & (k+1,0) & (k+2,0) & (k+3,0) & (k+4,0)&\omit\vrule \cr
}
}}
\caption{Boxes boxes everywhere}
\end{figure}
\end{document}


You could make a macro for it, too:

\documentclass{article}
\usepackage{lipsum}
\def\myboxything#1#2{\vbox{%
\def\hatfor##1{\multispan{##1}\hrulefill\crcr\noalign{\kern-.4pt}}
\offinterlineskip
\mathsurround=0pt
\def\cr{&\omit\vrule\crcr\noalign{\hrule}}
\halign{&\vrule\hbox to#1{$\vcenter to#1{}\hfil\scriptstyle##\hfil$}\crcr
#2\cr}}}
\begin{document}

\lipsum[34]

\begin{figure}
\centerline{\myboxything{4em}{%
\multispan2& \hatfor3
\multispan2& (k-1,1)& (k,1)& (k+1,1) \cr
(k-3,0)& (k-2,0)& (k-1,0)& (k,0)& (k+1,0)& (k+2,0)& (k+3,0)& (k+4,0)}
}
\caption{Boxes boxes everywhere}
\end{figure}

\begin{figure}
\centerline{\myboxything{4.5em}{%
\multispan3& \hatfor3
\multispan3& W(-k-1,1)& W(-k,1)& W(-k+1,1) \cr
W(-k-4,0)& W(-k-3,0)& W(-k-2,0)& W(-k-1,0)& W(-k,0)& W(-k+1,0)& W(-k+2,0)&
W(-k+3,0)}
}
\caption{Even more boxes}
\end{figure}

\begin{figure}
\centerline{\myboxything{5.5em}{%
\omit& \hatfor4
\omit& W(k-1,2n+2)& W(k,2n+2)& W(k+1,2n+2)& ? \cr
?& W(k-1,2n+1)& W(k,2n+1)& W(k+1,2n+1)& ? \cr
?& W(k-1,2n)& W(k,2n)& W(k+1,2n)& ? \cr
?& W(k-1,2n-1)& W(k,2n-1)& W(k+1,2n-1)& ?}
}
\end{figure}
\end{document}

-
Why does the above code not work correctly if I want to "omit" the first 3 blocks instead of the first two? If add one time \omit& the result is (for the first row, the second row is ok): the first two blocks are empty, the third block has a rule at the top, the sixth block has no rule at the top. –  Nadori Apr 19 '12 at 12:46
@Nadori: because the “hat” is in the row above, so one would need to add the omit there as well. Admittedly, this is by no means an easy to understand way to build such constructs, but it is much, much lighter than TikZ. –  morbusg Apr 19 '12 at 13:14
Thank you. I have edited the question above. Is it possible to fix this? –  Nadori Apr 23 '12 at 10:26

You could use TikZ …

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{block/.style={minimum size=1.5cm,outer sep=0pt,draw,rectangle,node distance=0pt}}

\begin{document}
\begin{tikzpicture}[every node/.style={block},]
\node (A) {$k=1$};
\node (B) [left=of A] {$k=2$};
\node (C) [left=of B] {$k=3$};
\node (D) [right=of A] {$k=0$};
\node (E) [above=of B] {$i=1$};
\node (F) [right=of E] {$i=2$};
\end{tikzpicture}
\end{document}


-
Note that \tikzstyle is deprecated. It's never described in the current pgfmanual, but funny often used there. The official way would be to use \tikzset{block/.style={minimum size=1.5cm,outer sep=0pt,draw,rectangle,node distance=0pt}. The expression can also be used in the optional argument of tikzpicture, e.g. to keep it local if wanted. –  Martin Scharrer Mar 26 '12 at 13:00
@MartinScharrer: Funny … I normally use \tikzset{... but I saw \tikzstyle so often that I thought it’s the preferred way. Then I’ll use \tikzset it’s even my preferred code :-) –  Tobi Mar 26 '12 at 13:55
The pgfmanual really needs some updating in this regard! ;-) –  Martin Scharrer Mar 26 '12 at 14:05
How can I add a caption? –  Nadori Mar 26 '12 at 14:12
@Nadori: Use a {figure} or try the float package to make your own float environment. Furthermore you may need \captionof{<type>}{<caption>} (form KOMA-Script classes or with caption) to type the caption outside of floating environments. If this doesn’t help pleas consider the LaTeX (beginners) guide of your preference ;-) –  Tobi Mar 26 '12 at 14:36

Just for completeness, here is an example of how you can use the xcoffins package to produce the same result.

I use some boxes through \mybox command, and positioned them using xcoffins. For more information on the positioning capabilities of the package you can check the manual.

\documentclass{article}
\usepackage{xcoffins}

\newcommand{\mybox}[2]{{\fbox{\vbox to #1 {\vfill \hbox to #1 {#2} \vfill}}}}

\pagestyle{empty}
\begin{document}

\NewCoffin \main
\SetHorizontalCoffin \main {\mybox{40pt}{$i=1$}}
\NewCoffin \Ab
\SetHorizontalCoffin \Ab {\mybox{40pt}{$i=2$}}
\NewCoffin \Ba
\SetHorizontalCoffin \Ba {\mybox{40pt}{$k=3$}}
\NewCoffin \Bb
\SetHorizontalCoffin \Bb {\mybox{40pt}{$k=2$}}
\NewCoffin \Bc
\SetHorizontalCoffin \Bc {\mybox{40pt}{$k=1$}}
\NewCoffin \Bd
\SetHorizontalCoffin \Bd {\mybox{40pt}{$k=0$}}

% Join \main using the (r)ight (v)ertical (c)enter with the \Ab (l)eft (v)ertical (c)enter
\JoinCoffins \main[r,vc] \Ab[l,vc]