如何用 TikZ 绘制此图片/图形?

如何用 TikZ 绘制此图片/图形?

在此处输入图片描述

我尝试使用 TikZ 绘制这幅图,但不幸的是没有成功。如果能得到任何帮助,我将不胜感激!这是我目前所做的:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fadings,calc,positioning,decorations.pathreplacing,arrows}


\begin{document}

\begin{figure}
\centering

% Define block styles
\tikzstyle{algo} = [text centered, minimum height=.5em, node font=\footnotesize] 
\tikzstyle{weight} = [rectangle, draw, text width=10em, text centered, minimum height=.1em, node font=\footnotesize]
\tikzstyle{length} = [rectangle, text width=10em, text centered, minimum height=.1em, node font=\footnotesize]

\begin{tikzpicture}
%Place nodes
\node [algo] (leer) {};
\node [algo, below of=leer] (plain) {Plain ISD:};
\node [algo, below of=plain] (lee) {Lee-Brickell:};
\node [algo, below of=lee] (leon) {Leon:};
\node [algo, below of=leon] (stern) {Stern:};


\node [weight, right of=plain, node distance=3cm] (plainone) {$0$};
\node [weight, right of=plainone, node distance=3.7cm] (plaintwo) {$\varepsilon$};
\node [weight, right of=lee, node distance=3cm] (leeone) {$p$};
\node [weight, right of=leeone, node distance=3.7cm] (leetwo) {$\varepsilon-p$};

\node [length, above of=plainone] (leerk) {$k$};
\node [length, above of=plaintwo] (leernk) {$n-k$};


\draw [stealth' - stealth', semithick] (leerk.south west)  -- (leerk.south east);
\draw [stealth' - stealth', semithick] (leernk.south west)  -- (leernk.south east);

\end{tikzpicture}
\end{figure}
\end{document}

答案1

这是另一个版本元帖子包裹在luamplib(因此编译lualatex或改编为良好生产规范或普通 MP)。

在此处输入图片描述

\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}

笔记

  • algobox宏返回一个picture,您可以直接用它绘制draw或保存等。

  • 宏展示了一种处理不同类型的可变数量参数的方法。

答案2

我可能不会使用 TiZ 图片,但带有 Ti 的表格Z 注释。更新:带框和不带框的文本现在应该对齐了。

\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}

在此处输入图片描述

答案3

这是一个简单的解决方案,带有tablemulticolumn一些 Ti

在此处输入图片描述

\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}

答案4

环境tcbitemize来自tcolorbox可以使用,可以使用 TikZ 代码轻松添加注释,并且比基于表的解决方案更容易地对每个元素进行单独装箱,并且输入每个元素比每次创建完整的 TikZ 节点稍微容易一些。

这种方法的主要限制(可能是我的无知)是在 中指定不同列宽时缺乏灵活性tcbraster。虽然可以根据需要对每个框使用raster force size=falseadd to width,但这似乎与 不兼容raster multicolumn

基本对齐可以设置为

\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document}  
\begin{tcbitemize}[raster width=15cm,raster columns=5,
raster equal height=rows,
enhanced,sharp corners,colback=white,colframe=black,
top=0mm,bottom=0mm,
halign=center,valign=center,halign upper=center,halign lower=center,
raster left skip=0mm,raster right skip=0mm,
raster column skip=0.5mm,
raster column 1/.style={blanker,halign=left},
]
\tcbitem Plain ISD:
\tcbitem[raster multicolumn=2] $0$
\tcbitem[raster multicolumn=2] $t$
\tcbitem Lee-Brickell:
\tcbitem[raster multicolumn=2] $p$
\tcbitem[raster multicolumn=2] $t-p$
\tcbitem Leon:
\tcbitem[raster multicolumn=2] $p$
\tcbitem $0$
\tcbitem $t-p$
\tcbitem Stern:
\tcbitem $p$
\tcbitem $p$
\tcbitem $0$
\tcbitem $t-2p$
\end{tcbitemize}
\end{document}

其中的raster multicolumn关键是创建跨越多列的框。框样式设置可以在选项中设置tcbitemizeraster column 1/.style={blanker,halign=left}为每个第 1 列框应用不同的样式,而不必为每个框单独应用不同的样式。

可以使用带有 TikZ 代码的空白框输入注释,使用每个角上定义的节点,通过at end tikz单独的键\tcbitemraster row 1/.style={blanker}raster row column 1/.style=上面一样将整行箭头设置为空白框。最后,只要虚线只划分一个框,就可以使用\tcbitem[sidebyside]和 使用\tcblower来划分左右内容,然后可以自定义分隔符。

\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document} 
\begin{tcbitemize}[raster width=15cm,raster columns=5,
raster equal height=rows,
enhanced,sharp corners,colback=white,colframe=black,
top=0mm,bottom=0mm,
halign=center,valign=center,halign upper=center,halign lower=center,
raster left skip=0mm,raster right skip=0mm,
raster column skip=0.5mm,
raster column 1/.style={blanker,halign=left},
raster row 1/.style={blanker},
raster row 4/.style={blanker}
]
\tcbitem 
\tcbitem [raster multicolumn=2,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$k$};}] 
\tcbitem [raster multicolumn=2,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$n-k$};}]
\tcbitem Plain ISD:
\tcbitem[raster multicolumn=2] $0$
\tcbitem[raster multicolumn=2] $t$
\tcbitem Lee-Brickell:
\tcbitem[raster multicolumn=2] $p$
\tcbitem[raster multicolumn=2] $t-p$
\tcbitem \rule{0mm}{5mm}
\tcbitem [raster multicolumn=2] 
\tcbitem [at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$l$};}]
\tcbitem [at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$n-k-l$};}]
\tcbitem Leon:
\tcbitem[raster multicolumn=2] $p$
\tcbitem $0$
\tcbitem $t-p$
\tcbitem Stern:
\tcbitem $p$
\tcbitem $p$
\tcbitem $0$
\tcbitem $t-2p$
\tcbitem Finiasz/Sendrier:
\tcbitem[raster multicolumn=3,sidebyside] $p$ \tcblower $p$
\tcbitem $t-2p$
\tcbitem Bernstein (Ball):
\tcbitem[raster multicolumn=2,sidebyside] $p_1$ \tcblower $p_1$
\tcbitem[sidebyside] $p_2$ \tcblower $p_2$
\tcbitem $t-2p_1-2p_2$
\end{tcbitemize}
\end{document}

产生

在此处输入图片描述

如前所述,raster force size=false似乎需要将列设置为不同的宽度,但我不确定如何add to width在同时使用时成功调整宽度raster multicolumn,因此调整需要更多的手动努力。

\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document} 
\begin{tcbitemize}[raster width=15cm,raster columns=5,
raster equal height=rows,
raster force size=false,
enhanced,sharp corners,colback=white,colframe=black,
top=0mm,bottom=0mm,
left=0mm,right=0mm,middle=0mm,sidebyside gap=0mm,
halign=center,valign=center,halign upper=center,halign lower=center,
raster left skip=0mm,raster right skip=0mm,
raster column skip=0.5mm,
raster column 1/.style={blanker,halign=left},
raster row 1/.style={blanker},
raster row 4/.style={blanker}
]
\tcbitem 
\tcbitem [add to width=2cm,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$k$};}] 
\tcbitem [add to width=4cm,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$n-k$};}]
\tcbitem[blanker,add to width=-3cm]
\tcbitem[blanker,add to width=-3cm]
\tcbitem Plain ISD:
\tcbitem[add to width=2cm] $0$
\tcbitem[add to width=4cm] $t$
\tcbitem[blanker,add to width=-3cm]
\tcbitem[blanker,add to width=-3cm]
\tcbitem Lee-Brickell:
\tcbitem[add to width=2cm] $p$
\tcbitem[add to width=4cm] $t-p$
\tcbitem[blanker,add to width=-3cm]
\tcbitem[blanker,add to width=-3cm]
\tcbitem \rule{0mm}{5mm}
\tcbitem[add to width=2cm] 
\tcbitem[add to width=-1cm,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$l$};}]
\tcbitem[add to width=2cm,at end tikz={\draw [<->] (frame.south east) -- (frame.south west) node[midway,above] {$n-k-l$};}]
\tcbitem[blanker,add to width=-3cm]
\tcbitem Leon:
\tcbitem[add to width=2cm] $p$
\tcbitem[add to width=-1cm] $0$
\tcbitem[add to width=2cm] $t-p$
\tcbitem[blanker,add to width=-3cm]
\tcbitem Stern:
\tcbitem[add to width=-0.5cm] $p$
\tcbitem[add to width=-0.5cm] $p$
\tcbitem[add to width=-1cm] $0$
\tcbitem[add to width=2cm] $t-2p$
\tcbitem Finiasz/Sendrier:
\tcbitem[add to width=4cm,sidebyside] $p$ \tcblower $p$
\tcbitem[add to width=2cm] $t-2p$
\tcbitem[blanker,add to width=-3cm]
\tcbitem[blanker,add to width=-3cm]
\tcbitem Bernstein (Ball):
\tcbitem[add to width=2cm,sidebyside] $p_1$ \tcblower $p_1$
\tcbitem[add to width=-1cm,sidebyside] $p_2$ \tcblower $p_2$
\tcbitem[add to width=2cm] $t-2p_1-2p_2$
\end{tcbitemize}
\end{document}

产生

在此处输入图片描述

相关内容