I've done some Googling and haven't found any way to create the below image in latex:
The derivative isn't important; it's the line with the +
, 0
below it including the -1
and 2
.
Any ideas how to do this?
chartsmath-mode
I've done some Googling and haven't found any way to create the below image in latex:
The derivative isn't important; it's the line with the +
, 0
below it including the -1
and 2
.
Any ideas how to do this?
This is also doable just with the calc
library and nothing else.
Layers deployment
At first we should locate the various livelli
(italian plural of livello
): nodes are just fine. Let's then define their aspect:
\tikzset{layer/.style={draw,
rectangle,
fill=green!85!blue!60,
minimum width=2cm
}
}
Since they have very similar labels (just the layer number changes), I think the best choice here is to locate them by means a foreach
loop; but... how to locate nodes sequentially? This is a possible way:
\foreach \i in {1,...,5}{
\node[layer] (layer-1-\i) at (0,0+\i*1cm) {Livello \i};
}
Indeed, in such a way each time \i
increments, also the y coordinate of the layer is incremented as well as the "counter" of its name and label. Each node is placed at a given vertical distance thanks to the syntax 0+\i*1cm
: change 1cm
to increase or reduce the space between the layers.
For the other stack it is just needed to change x coordinate and the name (layer-2-\i
rather than layer-1-\i
):
\foreach \i in {1,...,5}{
\node[layer] (layer-2-\i) at (5.5,0+\i*1cm) {Livello \i};
}
Now the large block on the bottom. Well, to have it of the exact size we can use a function able to compute it:
\makeatletter
\def\CalcDistance(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{west}}{\pgfpointanchor{#2}{east}}
\pgfmathsetmacro{\myheight}{veclen(\pgf@x,\pgf@y)}
\global\expandafter\edef\csname #3\endcsname{\myheight}
}
\makeatother
This, applied to a couple of our layers gives the \distance
:
\CalcDistance(layer-1-1,layer-2-1){distance}
The large block now should be located:
\node[layer, minimum width=\distance, yshift=-1cm] (low-module) at ($(layer-1-1.east)!0.5!(layer-2-1.west)$) {Mezzo fisico};
Its position can be computed to be placed exactly in the middle of the lowest level layers: this result should be shifted down to avoid covering layer livello
1.
Arrows and labels deplyoment
Once finished with the layers, the arrows. We can define a style for the type:
\tikzset{connective arrow/.style={
stealth-stealth
}
}
and now we're ready to add them to the picture.
Without considering for the moment the large module, for the others there's a similar behaviour: the arrow starts from the north
anchor of the lower layer up to the south
anchor of the next layer. So basically we need to be able to reference two counters: this is again a job for the foreach
.
\foreach \i[evaluate=\i as \nexti using int(\i+1)] in {1,...,4}{
\draw[connective arrow] (layer-1-\i.north)--(layer-1-\nexti.south)
node [midway,anchor=east, font=\footnotesize,left=0.15cm]{interfaccia livello \i/\nexti};
}
The evaluation allows indeed not only to identify the layers via their name, but it is also helpful for the label interfaccia livello \i/\nexti
, put as a node at the middle (midway
) left (anchor=east,left=0.15cm
) of the arrow. The font size is a bit reduced to make the picture look better: the font
key is of course used.
Same thing for the other column (just change the column name and the position of the arrow labels from left to right):
\foreach \i[evaluate=\i as \nexti using int(\i+1)] in {1,...,4}{
\draw[connective arrow] (layer-2-\i.north)--(layer-2-\nexti.south)
node [midway,anchor=west, font=\footnotesize,right=0.15cm]{interfaccia livello \i/\nexti};
}
Let's now take into account the protocollo livello
connection. It's basically similar to what did before, without having to evaluate anything:
\foreach \i in {1,...,5}{
\draw[connective arrow,draw=violet!50!blue] (layer-2-\i.west)--(layer-1-\i.east)
node [midway, font=\footnotesize,above=-0.05cm]{protocollo livello \i};
}
The above=-0.05cm
is used to have the label a bit more near the arrow.
Final addings
The picture is almost done: just a couple of things should be added. The first one is the connections between the two layers 1 and the large module; by computing the intersection via this is very simple:
\draw[connective arrow] (layer-1-1.south)--(low-module.north-|layer-1-1.south);
\draw[connective arrow] (layer-2-1.south)--(low-module.north-|layer-2-1.south);
Since they are just two arrows I won't use a foreach
for this purpose. Even for the host
label, I would use simple nodes:
\node[above=0.15cm,font=\Large,red] at (layer-1-5.north) {Host 1};
\node[above=0.15cm,font=\Large,red] at (layer-2-5.north) {Host 2};
Here the font size has been made bigger always via the font
key.
And now our picture is really finished therefore here it is the whole code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{layer/.style={draw,
rectangle,
fill=green!85!blue!60,
minimum width=2cm
},
connective arrow/.style={
stealth-stealth
}
}
\makeatletter
\def\CalcDistance(#1,#2)#3{%
\pgfpointdiff{\pgfpointanchor{#1}{west}}{\pgfpointanchor{#2}{east}}
\pgfmathsetmacro{\myheight}{veclen(\pgf@x,\pgf@y)}
\global\expandafter\edef\csname #3\endcsname{\myheight}
}
\makeatother
\begin{document}
\begin{tikzpicture}
% Drawing the modules
% Column 1
\foreach \i in {1,...,5}{
\node[layer] (layer-1-\i) at (0,0+\i*1cm) {Livello \i};
}
% Column 2
\foreach \i in {1,...,5}{
\node[layer] (layer-2-\i) at (5.5,0+\i*1cm) {Livello \i};
}
% Bottom module
\CalcDistance(layer-1-1,layer-2-1){distance}
\node[layer, minimum width=\distance, yshift=-1cm] (low-module) at ($(layer-1-1.east)!0.5!(layer-2-1.west)$) {Mezzo fisico};
% Arrows
% Column 1
\foreach \i[evaluate=\i as \nexti using int(\i+1)] in {1,...,4}{
\draw[connective arrow] (layer-1-\i.north)--(layer-1-\nexti.south)
node [midway,anchor=east, font=\footnotesize,left=0.15cm]{interfaccia livello \i/\nexti};
}
% Column 2
\foreach \i[evaluate=\i as \nexti using int(\i+1)] in {1,...,4}{
\draw[connective arrow] (layer-2-\i.north)--(layer-2-\nexti.south)
node [midway,anchor=west, font=\footnotesize,right=0.15cm]{interfaccia livello \i/\nexti};
}
% Between columns
\foreach \i in {1,...,5}{
\draw[connective arrow,draw=violet!50!blue] (layer-2-\i.west)--(layer-1-\i.east)
node [midway, font=\footnotesize,above=-0.05cm]{protocollo livello \i};
}
% LAST THINGS
% arrows towards bottom module
\draw[connective arrow] (layer-1-1.south)--(low-module.north-|layer-1-1.south);
\draw[connective arrow] (layer-2-1.south)--(low-module.north-|layer-2-1.south);
% host labels
\node[above=0.15cm,font=\Large,red] at (layer-1-5.north) {Host 1};
\node[above=0.15cm,font=\Large,red] at (layer-2-5.north) {Host 2};
\end{tikzpicture}
\end{document}
Considerations
The picture is perfectly scalable: it means that using
\begin{tikzpicture}[scale=0.5,transform shape]
its dimensions are correctly set to be half of the previous one. This is because, even if some fixed units have been used (1cm
for the block vertical distance, above=-0.05cm
for protocol labels and above=0.15cm
for host labels), their positioning, actually, is always defined in terms of other nodes or in the midway of a path.
To introduce the possibility to easy customize the vertical block distance, one might think about introducing a new key (in the preamble):
\pgfkeys{/tikz/.cd,
vertical distance/.initial=1cm,
vertical distance/.get=\vertdist,
vertical distance/.store in=\vertdist,
}
Then, \vertdist
should be applied to:
% Drawing the modules
% Column 1
\foreach \i in {1,...,5}{
\node[layer] (layer-1-\i) at (0,0+\i*\vertdist) {Livello \i};
}
% Column 2
\foreach \i in {1,...,5}{
\node[layer] (layer-2-\i) at (5.5,0+\i*\vertdist) {Livello \i};
}
% Bottom module
\CalcDistance(layer-1-1,layer-2-1){distance}
\node[layer, minimum width=\distance, yshift=-\vertdist] (low-module) at ($(layer-1-1.east)!0.5!(layer-2-1.west)$) {Mezzo fisico};
In such a way, using:
\begin{tikzpicture}[vertical distance=2cm]
the block distance will be doubled with respect to the current setting.
<shameless plug>
May I suggest the paper Leyla and I wrote some time ago about inclusion of plots generated by outside programs in LaTeX? Here is it: http://www.tug.org/TUGboat/tb34-3/tb108veytsman.pdf
</shameless plug>
Best Answer
Here's an approach using a plain TeX alignment and
cleaders
...