I don't really get the question so I hope this is what you wanted. If you include a full document (such that we copy paste and see the problem on our systems) things are much more easier.
Here, you can change the default setting within a scope but your block
style had a node distance
which was resetting every time it is issued. I've made it 2mm such that we can see the difference easier.
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}[decision/.style={diamond, draw, text width=4.5em, text badly centered, node distance=3.5cm, inner sep=0pt},
block/.style ={rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em, minimum height=2em},
cloud/.style ={draw, ellipse, minimum height=2em},
line/.style ={draw,-latex'},
node distance = 1cm,
auto]
\node [block] (1st) {1st};
\node [block, right= of 1st] (2nd1) {2nd1};
\begin{scope}[node distance=2mm and 10mm]%Here we change it for everything inside this scope
\node [block, above= of 2nd1] (2nd2) {2nd2};
\node [block, below= of 2nd1] (2nd3) {2nd3};
\node [block, right= of 2nd1] (3rd1) {3rd1};
\node [block, above= of 3rd1] (3rd2) {3rd2};
\node [block, above= of 3rd2] (3rd3) {3rd3};
\end{scope}
\node [block, below= of 3rd1] (3rd4) {3rd4};
\node [block, below= of 3rd4] (3rd5) {3rd5};
\path [line] (1st) -- (2nd1);
\path [line] (2nd1) -- (2nd2);
\path [line] (2nd1) -- (2nd3);
\path [line] (2nd2) -- (3rd3);
\path [line] (2nd1) -- (3rd1);
\path [line] (1st) -- (2nd1);
\end{tikzpicture}
\end{document}
I think it's easy to draw some diagram like that if you start placing comments and after that use them as references to draw message interchanges.
Next code shows a minimal example. I've used matrix
to create all comments
and server
and client
labels. Of course, you don't need to use a matrix
and can place them using relative positions.
The tricky part is how to align message arrows with corresponding comment first line. May be there exist better solutions but I've used \subnode
command from tikzmark
library. It creates an internal reference inside every comment node.
Finally it's easy to draw message interchanges with the help of intersection coordinates (|-
or -|
).
This is a complete code for a minimal example:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,positioning,tikzmark}
\begin{document}
\begin{tikzpicture}[remember picture, % --- Dont' forget it with tikzmark ---
comment/.style={text width=10cm, align=left},
font=\small\ttfamily]
%Place `Client`, `Server` and `comment` nodes
\matrix{%
\node (client) {Client}; &
\node (server) {Server}; & \\
& &
\node[comment] (1) {\subnode{A}{Header}:
GET (T=CON, Code=0.01, MID=0x7d34)\\
Uri-Path: "temperature"}; \\
& &
\node[comment] (2) {\subnode{B}{Header}:
2.05 Content (T=ACK, Code=2.05, MID=0x7d34)\\
Payload: "22.3 C"}; \\};
%Draw vertical lines
\draw (client) -- (client|-2.south);
\draw (server) -- (server|-2.south);
%Draw message interchanges
\draw[->] (A-|client)--node[below]{GET} (A-|server) ;
\draw[<-] (B-|client)-- node[below]{2.05} (B-|server) ;
\end{tikzpicture}
\end{document}
Compile previous code twice to get
Update
If you want to align comments around colons, you can use a tabular
inside comments.
In this case you have to use ampersand replacement
option inside \matrix
because it's necessary to have different &
symbols inside tabular
and matrix
.
\begin{tikzpicture}[remember picture,
comment/.style={text width=10cm, align=left},
font=\small\ttfamily]
%Place `Client`, `Server` and `comment` nodes
\matrix [ampersand replacement=\&]{%
\node (client) {Client}; \&
\node (server) {Server}; \& \\
\& \&
\node[comment] (1) {\begin{tabular}{r@{ }l}\subnode{A}{Header}:&
GET (T=CON, Code=0.01, MID=0x7d34)\\
Uri-Path:& "temperature"\end{tabular}}; \\
\& \&
\node[comment] (2) {\begin{tabular}{r@{ }l}\subnode{B}{Header}:&
2.05 Content (T=ACK, Code=2.05, MID=0x7d34)\\
Payload:& "22.3 C"\end{tabular}}; \\};
%Draw vertical lines
\draw (client) -- (client|-2.south);
\draw (server) -- (server|-2.south);
%Draw message interchanges
\draw[->] (A-|client)--node[below]{GET} (A-|server) ;
\draw[<-] (B-|client)-- node[below]{2.05} (B-|server) ;
\end{tikzpicture}
The result is now
But, if you want to align all comments, it's better to put all of them inside a tabular
. You can avoid using \matrix
and place server
and client
labels after it. In order to separate different comments you can use \\[vertical distance]
inside tabular
.
Solution looks like:
\begin{tikzpicture}[remember picture,
font=\small\ttfamily]
%Place a `comment` node with all text inside a tabular
\node(comments) {\begin{tabular}{r@{ }l}
\subnode{A}{Header}:&
GET (T=CON, Code=0.01, MID=0x7d34)\\
Uri-Path:& "temperature"\\[3mm]
\subnode{B}{Header}:&
2.05 Content (T=ACK, Code=2.05, MID=0x7d34)\\
Payload:& "22.3 C"\end{tabular}};
% Place `server` and `client` nodes
\node[above left=1mm and 0mm of comments] (server) {Server};
\node[left=5mm of server] (client) {Client};
%Draw vertical lines
\draw (client) -- (client|-comments.south);
\draw (server) -- (server|-comments.south);
%Draw message interchanges
\draw[->] (A-|client)--node[below]{GET} (A-|server) ;
\draw[<-] (B-|client)-- node[below]{2.05} (B-|server) ;
\end{tikzpicture}
And the result is:
Best Answer
As egreg mentions in
his comment
,tikz-cd
makes the arrows extend to fill the available space and (I agree with him) this is a desirable feature.Regarding the second part of the question, you can use
\tikzcdset
to pass options to the path/tikz/commutative diagrams
so they will affect alltikzcd
diagrams; in the case of your question, you can useshorten
:A complete example:
If you want to use this options locally, use them in the optional argument of
tikzcd
: