2

I'm trying to draw a picture (actually it's more of a diagram) like this

enter image description here

but I don't know which packages to user or even even if it's possible to do such a thing using for example TikZ.

Thanks in advance for your help.

5
  • Welcome! It is possible. I don't know if TikZ is the best way. What is the diagram supposed to show exactly? If I saw this in a paper I was reading, I'd be mystified. Is it a kind of diagram specific to your discipline? If so, what kind is it? There may be a special package for diagrams of that kind.
    – cfr
    Jul 7 '15 at 22:47
  • Otherwise, I'd do it as a tabular, I think. With tikzmark or something if necessary for things like the arrows.
    – cfr
    Jul 7 '15 at 22:48
  • It's more like a toy example of a cryptographic scheme I'm implementing, so I need to "draw" some matrices and present them in certain order which I've seen done (in some papers on crypto) with diagrams such as this one.
    – HollowMan
    Jul 7 '15 at 22:56
  • 1
    Can you post an example of what you've tried? Or at least with the sample content?
    – cfr
    Jul 7 '15 at 23:01
  • Have you looked at the cryptocode package at all? It seems to be quite new.
    – cfr
    Jul 7 '15 at 23:05
6

Here is a way to do it with pure tikz. Initially i planned to create it with nested matrices until i realized that this is unsupported by tikz till now (pdflatex: You cannot nest pgfmatrix environments, yet). So i tried to recreate the tabular look by some more tikz code with using some helper coordinates.

As all elements are realized using matrices of nodes you can access every node and reuse it without relying on libraries like tikzmark. (See the red line between the two nodes).

The code is just a proof of concept and could need some prettying-up. Perhaps someone also finds a nicer solution of creating the table that holds all the information.

\documentclass[tikz, border=6mm]{standalone}
\usetikzlibrary{positioning, calc, fit, matrix, decorations.pathreplacing}

\begin{document}
 \begin{tikzpicture}[every matrix/.style={matrix of nodes, nodes in empty cells, draw}]
   %%% First row
   \matrix[label={0:$= M$}] (m1_1) {
     |(a1)| 106 & |(a2)| 429 & |(b1)| 032 & |(b2)| 375  \\
     277 & 398 & 345 & 492 \\
    };

    % Braces over first matrix
    \draw [decorate, decoration={raise=.5cm, brace}](a1.west) -- (a2.east) node [midway, yshift=.75cm] {$A$};
    \draw [decorate, decoration={raise=.5cm, brace}](b1.west) -- (b2.east) node [midway, yshift=.75cm] {$B$};

    \matrix[right=of m1_1, label={0:$= P_1$}] (m1_2) {
        469 & 487 \\
        312 & 001 \\
    };

    \matrix[right=of m1_2, label={0:$= P_2$}] (m1_3) {
        355 & 469 \\
        398 & 492 \\
    };

    \matrix[right=of m1_3, label={0:$= \Delta$}] (m1_4) {
        443 & 000 \\
        000 & 503 \\
    };

    \matrix[right=of m1_4, label={0:$= D_1$}] (m1_5) {
        +1 & -1 \\
        -1 & +1 \\
    };

    \matrix[right=of m1_5, label={0:$= D_2$}] (m1_6) {
        +64 & +0 \\
        -1 & +64 \\
    };

    \node [right=2cm of m1_6] (rand1) {Random Text};

    %%% Second row
    \matrix[below=of m1_1, label={0:$= P_1$}] (m2_1) {
        179 & 109 & 152 & 242 \\
        005 & 349 & 430 & 267 \\
    };

    \matrix[below=.5cm of m2_1, label={0:$= P_2$}] (m2_2) {
        302 & 307 & 411 & 215 \\
        290 & 295 & 126 & 043 \\
    };

    \matrix[right=3cm of m2_1, label={0:$= D_1$}] (m2_3) {
        443 & -503 \\
        -443 & 503 \\
    };

    \matrix[below=.5cm of m2_3, label={0:$= D_2$}] (m2_4) {
        218 & 000 \\
        -443 & 411 \\
    };

    \matrix[right=3cm of m2_3, label={0:$= M'_1$}] (m2_5) {
        179 & 109 & 152\textbf{+443} & 242\textbf{-503} \\
        005 & 349 & 430\textbf{-443} & 267\textbf{+503} \\
    };

    \matrix[below=.5cm of m2_5, label={0:$= M'_2$}] (m2_6) {
        302 & 307 & |(m262)| 193\textbf{+218} & 215\textbf{+000} \\
        290 & 295 & 126\textbf{-443} & 043\textbf{+411} \\
    };

    \node [below=2.5cm of rand1] (rand2) {Random Text};

    %%% Third row
    \matrix[below right=1cm and -2.5cm of m2_2, label={180:$M'_1 =$}] (m3_1) {
        179 & 109 & 074 & 260 \\
        005 & 349 & 508 & 249 \\
    };

    \matrix[below=.5cm of m3_1, label={180:$M'_2 =$}] (m3_2) {
        302 & 307 & |(m323)| 411 & 215 \\
        290 & 295 & 204 & 451 \\
    };

    \matrix[right=8cm of m3_1, label={0:$= M_1$}] (m3_5) {
        179 & 109 & 074 & 260 \\
        005 & 349 & 508 & 249 \\
    };

    \matrix[below=.5cm of m3_5, label={0:$= M_2$}] (m3_6) {
        302 & 307 & 411 & 215 \\
        290 & 295 & 204 & 451 \\
    };

    \coordinate (helper_l) at ($(m3_1)!.5!(m3_2)$);
    \coordinate (helper_r) at ($(m3_5)!.5!(m3_6)$);
    \coordinate (helper) at ($(helper_l)!.5!(helper_r)$);

    \matrix at (helper) (m3_3) {
        |(n11)| 1 & |(n12)| 2 & |(n13)| 3 & |(n14)| 4 \\
        & & & \\ & & & \\   & & & \\ & & & \\   & & & \\    & & & \\    & & & \\
        |(n21)| 1 & |(n22)| 2 & |(n23)| 3 & |(n24)| 4 \\
    };

    \node [below=3.5cm of rand2] (rand3) {Random Text};

    \begin{scope}[>=latex]
        \foreach \x\y in {11/23,12/21,13/24,14/22} \draw [->] (n\x) -- (n\y);
        \foreach \x in {m3_1,m3_2} \draw [->] (\x) -- (\x-|m3_3.west);
        \foreach \x in {m3_5,m3_6} \draw [<-] (\x) -- (\x-|m3_3.east);

        \draw [red, thick, ->] (m262) to [in=90, out=125] (m323);
    \end{scope}

    \begin{scope}[thick] % fake table
        \node [fit=(m1_1) (m3_6) (rand3), draw, inner sep=1cm, rounded corners] (bg) {};
        \coordinate (helper_1) at ($(m2_1)!.5!(m1_2)$); 
        \coordinate (helper_2) at ($(m3_1)!.5!(m2_2)$);
        \coordinate (helper_3) at ($(rand2.west)+(-.5,0)$);
        \draw (bg.west|-helper_1) -- (helper_1) -- (helper_1-|bg.east);
        \draw (bg.west|-helper_2) -- (helper_2) -- (helper_2-|bg.east);
        \draw (bg.north-|helper_3) -- (helper_3) -- (helper_3|-bg.south);
    \end{scope}
 \end{tikzpicture}
\end{document}

Rendered code: rendered image

[EDIT] Update for resizing the image

You can use different approaches for rescaling. At first you could use the options scale=<factor> and font=<fontsize> for your tikzpicture-environment. Otherwise have a look at here. I prefer the adjustbox-approach:

...
\usepackage{adjustbox}
...

\adjustbox{width=.5\textwidth}{ % or use width=5cm, etc.
  \begin{tikzpicture}
  ...
  \end{tikzpicture}
}
....

[/EDIT]

1
  • thanks, thats a very nice solution, just another question if you dont mind, now I have to insert that diagram inside a page, how can I rezise it? i've tried "include graphics" resize but it says that something like unscalable graphic. can you help?
    – HollowMan
    Jul 8 '15 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.