tikz 'flow chart'/protocol diagram with no hard coded postions

I'm trying to create a particular style of flow chart/ protocol diagram. This seems readily doable with tikz, but I'm having no luck figuring out how to get tikz to even attempt to do what I want.

The hope is that the bounding box will expand the page it's in (or minipage in the case) and all layout will be relative. This is going to be reused relatively frequently

I currently have an approach that achieves 90% the desired effect simply using minipage, flushleft/right, and framed. Importantly, I can easily put arbitrary equations, itemize, etc in the boxes and not worry about layout much. So it's flexible. But I don't think it is possible to easily get the connecting arrows in the way I want. Hence tikz.

for what it's worth, that approach is below.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb,framed}
\begin{document}
\begin{figure*}[t]
\begin{minipage}{\linewidth}
\begin{framed}
\begin{minipage}{.75\textwidth}
\begin{framed}
Do some stuff. Works like normal tex. can do arbitrary stuff in here
\begin{align*}
\pi=PK\{(a,b,c,d):&a=b(y;r) \land c(y^\prime;r^\prime) \\ &\land foo(bar,c) %
\end{align*}%

\end{framed}
\end{minipage}$\xrightarrow{\makebox[.15\textwidth]{$C$}}$ M
\begin{flushright}
C $\xrightarrow{\makebox[.14\textwidth]{$W$}}$
\begin{minipage}{.75\textwidth}
\begin{framed}
Do some  more stuff ....
\end{framed}
\end{minipage}
\end{flushright}
\begin{minipage}{.75\textwidth}
\begin{framed}
Yet more stuff
\end{framed}
\end{minipage}$\xrightarrow{\makebox[.15\textwidth]{$x$}}$ M
\end{framed}
\end{minipage}
\end{figure*}
\end{document}

• Should the rectangles and therefore the arrows have a more of less fixed size? – Alenanno May 8 '16 at 17:13
• Yes. say .75\textwidth and .25\textwidth – imichaelmiers May 8 '16 at 17:59

Here's a version with Tikz, the command is \dbox and it takes one mandatory argument, the text. The optional argument is the label on the arrow. You can choose not to write it. Of course, the first occurrence of \dbox cannot have an arrow label, you can write it, but it won't yield any result.

The letter indicating the box (A, B, ...) is automatically inserted; also, each rectangle will not have any indent. Without this option, leaving a blank line between rectangles would create an indentation. Now blank lines are not a problem.

The alignment of the boxes is also automatic. I created a counter which increases by 1 each time you draw a box. If the counter is odd, the box will be flushed to the left, otherwise it will be drawn on the right.

So in summary the command is like this:

\dbox[ <arrow label> ]{ <text> }


Code

\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz}
\usepackage{lipsum}

\usetikzlibrary{arrows.meta}
\newcounter{boxes}
\setcounter{boxes}{1}

\newcommand\dbox[2][]{%
\noindent
\ifodd\theboxes
\tikz[remember picture, overlay]{%
\node[draw, anchor=west, text width=.75\textwidth] (n\arabic{boxes}) {\Alph{boxes}: #2};%
\ifnum\theboxes>1\relax
\pgfmathtruncatemacro\prev{\arabic{boxes}-1}
\draw (n\prev.west) -- (n\prev.west-|n\arabic{boxes}.north west) coordinate (a) node[above, midway, text width=.2\textwidth] {#1};
\draw[-{Latex}] (a) -- (n\arabic{boxes}.north west);
\fi}\par\vspace*{2cm}
\else%
\begin{flushright}%
\tikz[remember picture, overlay]{%
\node[draw, anchor=east, text width=.75\textwidth] (n\arabic{boxes}) {\Alph{boxes}: #2};
\ifnum\theboxes>1\relax
\pgfmathtruncatemacro\prev{\arabic{boxes}-1}
\draw (n\prev.east) -- (n\prev.east-|n\arabic{boxes}.north east) coordinate (a) node[above, midway, text width=.2\textwidth] {#1};
\draw[-{Latex}] (a) -- (n\arabic{boxes}.north east);
\fi}\par\vspace*{2cm}
\end{flushright}%
\fi%
\stepcounter{boxes}%
}%

\begin{document}
\dbox{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.}

\dbox[Here's a label!]{Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra me- tus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc.}

\dbox{Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra me- tus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc.}

\end{document}

• Thanks. This works well. The only unfortunate problem is that any syntax errors in the input text get reported as coming on the line of the closing brace. No idea how to avoid that in general though. – imichaelmiers May 8 '16 at 22:31
• @imichaelmiers are you inputting code in the node? Anyways, yes, that's expected, it's not because of this code. – Alenanno May 8 '16 at 22:34
• yes, usually crypto. And no, it's not because of your code. It's just a quirk of latex I wish there was a workaround for. – imichaelmiers May 8 '16 at 22:44