How to draw this picture/figure with TikZ?

I would probably not use a TikZ picture, but a tabular with TikZ annotations. UPDATE: Boxed and unboxed texts should be aligned now.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\newlength{\nodeshift}\setlength{\nodeshift}{1.2cm}
\usetikzlibrary{matrix,fadings,calc,positioning,decorations.pathreplacing,arrows}

\newcommand\tikznode[2]{\tikz[remember picture]{\node(#1)[inner sep=0pt]{#2};}}
\newcommand{\WeightNode}[3][3cm]{\tikz[remember picture]{%
\node(#2)[draw,minimum height=1.4em,inner sep=0pt, node font=\footnotesize,text
width=#1,text centered]{#3};}}
\newcommand{\DividedWeightNode}[3][3cm]{\tikz[remember picture]{%
\node(#2)[draw,minimum height=1.4em,inner sep=0pt, node font=\footnotesize,text
width=#1,text centered,append after command={%
  \pgfextra{ %
   \draw[densely dashed] (\tikzlastnode.north) -- (\tikzlastnode.south);
   \path (\tikzlastnode.west) -- (\tikzlastnode.center) node[midway,node font=\footnotesize]{#3};
   \path (\tikzlastnode.center) -- (\tikzlastnode.east) node[midway,node font=\footnotesize]{#3};}}
]{};}}


\newcommand{\AlgoNode}[3][3cm]{\tikz[remember picture]{%
\node(#2)[minimum height=1.4em,inner sep=0pt ,text
width=#1,text centered,align=left]{#3};}}

\begin{document}

\lipsum[1]

\begin{figure}[h]
\centering

\begin{tabular}{p{3cm}p{1.3cm}p{1.3cm}p{1cm}p{2.8cm}}
& & & & \\[0.5cm] % because the overlay may overshoot otherwise
\AlgoNode{plain}{Plain ISD:} & \multicolumn{2}{p{2.8cm}}{\WeightNode{plainone}{$0$}}
& \multicolumn{2}{p{4cm}}{\WeightNode[4cm]{plaintwo}{$t$}}\\
\AlgoNode{lee}{Lee--Brickell:} &
\multicolumn{2}{p{2.8cm}}{\WeightNode{leeone}{$p$}}
& \multicolumn{2}{p{4cm}}{\WeightNode[4cm]{leetwo}{$p-t$}}\\[1.5cm]
\AlgoNode{leon}{Leon:} &
\multicolumn{2}{p{2.8cm}}{\WeightNode{leonone}{$p$}} &
\WeightNode[1cm]{leontwo}{$0$} &
\WeightNode{leonthree}{$t-p$}\\
\AlgoNode{stern}{Stern:} & \WeightNode[1.3cm]{sternone}{$p$} & 
\WeightNode[1.3cm]{sterntwo}{$p$} &
\WeightNode[1cm]{sternthree}{$0$} &
\WeightNode[3cm]{sternfour}{$t-2p$} \\
\AlgoNode{fini}{Finiasz/Sendrier:} & 
\multicolumn{3}{p{3.8cm}}{\DividedWeightNode[4.4cm]{finione}{$p$}} &
\WeightNode{finitwo}{$t-2p$}\\
\AlgoNode{fini}{Bernstein (Ball):} & 
\multicolumn{2}{p{2.8cm}}{\DividedWeightNode{finione}{$p_1$}} &
\DividedWeightNode[1cm]{finione}{$p_2$} &
\WeightNode{finitwo}{$t-2p_1-2p_2$}\\
\end{tabular}
\end{figure}
\tikzstyle{length} = [rectangle, text centered, minimum height=.1em, node font=\footnotesize]
\begin{tikzpicture}[overlay,remember picture]
\node [length, above of=plainone,text width=2.8cm] (leerk) {$k$};
\node [overlay,length, above of=plaintwo,text width=3.8cm] (leernk) {$n-k$};
\draw [stealth' - stealth', semithick] (leerk.south west)  -- (leerk.south east);
\draw [stealth' - stealth', semithick] (leernk.south west)  -- (leernk.south east);
\node [length, above of=leontwo,text width=0.6cm] (leerl) {$\ell$};
\node [overlay,length, above of=leonthree,text width=2.8cm] (leernkl) {$n-k-\ell$};
\draw [stealth' - stealth', semithick] (leerl.south west)  -- (leerl.south east);
\draw [stealth' - stealth', semithick] (leernkl.south west)  -- (leernkl.south east);
\end{tikzpicture}

\lipsum[2]
\end{document}

enter image description here


Here's an alternative version drawn with Metapost wrapped up in luamplib (so compile with lualatex or adapt it for GMP or plain MP).

enter image description here

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
% a routine to draw each box.
% Arguments: name, followed by pairs of strings / numerics
% 
vardef algobox(expr name)(text specs) = 
    save p, s, w, i, j;
    % step through the spec and save the strings and widths
    string s[];  numeric w[]; numeric i, j; i = j = 0;
    for $=specs:
        if string $: s[incr i] elseif numeric $: w[incr j] fi := $; 
    endfor
    numeric x; x = 0; path b;
    % make a picture to return
    picture p; p = image(
        for k=1 upto i:
            % define the box then fill it and draw it
            b := unitsquare yscaled dp xscaled (w[k]-gap) shifted (x,0);
            fill b withcolor 7/8[green+red, white]; draw b;
            % do the labels with special case for "split" labels
            if substring (0,1) of s[k] = ":":
                label(substring (1,infinity) of s[k], (x + 1/4 w[k], 1/2 dp));
                label(substring (1,infinity) of s[k], (x + 3/4 w[k], 1/2 dp));
                draw (x + 1/2 w[k], 0) -- (x + 1/2 w[k], dp) dashed evenly scaled 1/2;
            else:
                label(s[k], (x + 1/2 w[k], 1/2 dp));
            fi
            % advance x
            x := x + w[k];
        endfor
        % add the name
        label.rt(name, (x, 1/2 dp));
    ); p
enddef;

beginfig(1);
    % some parameters to control the dp of the boxes, the gap between them 
    % and the values of l, k, and n
    numeric dp, gap, k, l, n; 
    l = 40; k = 100;  n = 240; dp = 14; gap = 2;

    % draw the labelled boxes, shifted as desired...
    draw algobox("Plain ISD",    "$0$", k, "$t$", n-k);
    draw algobox("Lee-Brickell", "$p$", k, "$t-p$", n-k)                        shifted 25 down;
    draw algobox("Leon", "$p$", k, "$0$", l, "$t-p$", n-k-l)                    shifted 80 down;
    draw algobox("Stern", "$p$", 1/2k, "$p$", 1/2k, "$0$", l, "$t-2p$", n-k-l)  shifted 105 down;
    draw algobox("Finiasz/Sendrier", ":$p$", k+l, "$t-2p$", n-k-l)              shifted 130 down;
    draw algobox("Bernstein (Ball)", ":$p_1$", k, ":$p_2$", l, "$t-2p_1 - 2p_2$", n-k-l) shifted 155 down;

    % define some paths for the arrows
    path a[];
    a1 = (0,24) -- (k-gap,24);
    a2 = (k,24) -- (n-gap,24);
    a3 = (k,-56) -- (k+l-gap, -56);
    a4 = (k+l,-56) -- (n-gap, -56);

    % and draw them (with narrower arrow heads)
    ahangle := 30;
    drawdblarrow a1; label.top("$k$", point 1/2 of a1);
    drawdblarrow a2; label.top("$n-k$", point 1/2 of a2);
    drawdblarrow a3; label.top("$\ell$", point 1/2 of a3);
    drawdblarrow a4; label.top("$n-k-\ell$", point 1/2 of a4);

endfig;
\end{mplibcode}
\end{document}

Notes

  • The algobox macro returns a picture, which you can draw directly with draw or save etc.

  • The macro shows one way to deal with variable numbers of arguments of different types.


Here is a simple solution with table with multicolumn and some TikZ

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{arydshln}
\usepackage{booktabs}
\usepackage{tikz}

\newcommand\myarrow[3]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shorten >= 1pt]
  \draw[<->, thick] (#1,0) to (#2,0) node[midway, above]{$#3$};
\end{tikzpicture}%
}


\begin{document}

\begin{table}[!htpb]
\centering
\begin{tabular}{@{}lcclccc@{}}
                                        & \multicolumn{3}{c}{\myarrow{-1.5}{1.5}{k}}              & \multicolumn{3}{c}{\myarrow{-2}{2}{n-k}}                                                  \\[-2ex]
                                        &                            &    &                       &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Plain ISD:}         & \multicolumn{3}{c|}{$0$}                                & \multicolumn{3}{c|}{$t$}                                                                      \\ \cline{2-7}
                                        &                            &    &                       &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Lee-Brickell:}      & \multicolumn{3}{c|}{$p$}                                & \multicolumn{3}{c|}{$t-p$}                                                                    \\ \cline{2-7} 
                                        &                            &    &                       &                            &                             &                                    \\
                                        &                            &    &                       & \multicolumn{2}{c}{\myarrow{-0.75}{0.8}{\ell}}          & \myarrow{-1.4}{1.2}{n-k-\ell}        \\[-2ex]
                                        &                            &    &                       &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Leon:}              & \multicolumn{3}{c|}{$p$}                                & \multicolumn{2}{c|}{$0$}                                 & \multicolumn{1}{c|}{$t-p$}         \\ \cline{2-7} 
                                        &                            &    &                       &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Stern:}             & \multicolumn{1}{c|}{$p$}   & \multicolumn{2}{c|}{$p$}   & \multicolumn{2}{c|}{$0$}                                 & \multicolumn{1}{c|}{$t-2p$}        \\ \cline{2-7} 
                                        &                            &    &                       &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Finniasz/Sendrier:} & \multicolumn{2}{c;{2pt/2pt}}{$\qquad p\qquad$}          & \multicolumn{3}{@{}c|}{$p$} & \multicolumn{1}{c|}{$t-2p$}                                        \\ \cline{2-7} 
                                        &                            &    & \multicolumn{1}{c}{}  &                            &                             &                                    \\ \cline{2-7} 
\multicolumn{1}{l|}{Bernstein (Ball):}   & \multicolumn{1}{c;{2pt/2pt}}{$\quad p_1\quad$} & \multicolumn{2}{c|}{$\quad p_1\quad$} & \multicolumn{1}{c;{2pt/2pt}}{$p_2$} & \multicolumn{1}{c|}{$p_2$} & \multicolumn{1}{c|}{$t-2p_1-2p_2$} \\ \cline{2-7} 
\end{tabular}
\end{table}

\end{document}

Tags:

Tikz Pgf