在 tikz 中堆叠矩形

在 tikz 中堆叠矩形

我想要像本例一样在 LaTeX 中堆叠圆角矩形。 在此处输入图片描述

这是如何实现的?(我尝试过河内塔,但不太合适)

答案1

您可以使用positioning图书馆。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{myrect/.style={fill=gray!50, rounded corners, inner sep=0pt, minimum height=5mm, minimum width=#1}}

\begin{document}
\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\node[above=of A, myrect=5cm](B){};
\node[above=of B, myrect=7cm](C){};
\node[above=of C, myrect=2cm](D){};
\node[above=of D, myrect=6cm](E){};
\node[above=of E, myrect=3cm](F){};
\node[above=of F, myrect=4cm](G){};
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\node[above=of A, myrect=7cm](B){};
\node[above=of B, myrect=6cm](C){};
\node[above=of C, myrect=5cm](D){};
\node[above=of D, myrect=4cm](E){};
\node[above=of E, myrect=3cm](F){};
\node[above=of F, myrect=2cm](G){};
\end{tikzpicture}

\end{document}

或者如果您愿意,您可以将其放入宏中。

用法是\stack{<bottom center coordinate>}{<stack sequence>}

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{myrect/.style={fill=gray!50, rounded corners, inner sep=0pt, minimum height=5mm, minimum width=#1}}

\newcommand{\stack}[2]{\coordinate(stack1) at (#1);\foreach \n[count=\k, evaluate=\k as \j using int(\k+1)] in {#2}{\node[above=of stack\k, myrect=\n cm](stack\j){};}}

\begin{document}

\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\stack{A}{5,7,2,6,3,4}
\draw(9,0)--coordinate(B)(17,0);
\stack{B}{7,6,5,4,3,2}
\end{tikzpicture}

\end{document}

答案2

使用该包的一个有趣的实现l3draw允许替代和随机排序以及使用颜色列表:

\documentclass[border=10pt]{standalone}
\usepackage{l3draw}

\ExplSyntaxOn

\fp_new:N \l_rectstack_base_width_fp
\int_new:N \l_rectstack_rect_count_int
\fp_new:N \l_rectstack_rect_height_fp
\fp_new:N \l_rectstack_rect_sep_fp
\fp_new:N \l_rectstack_rect_min_width_fp
\fp_new:N \l_rectstack_rect_max_width_fp
\dim_new:N \l_rectstack_rect_corner_radius_dim
\clist_new:N \l_rectstack_rect_colors_clist
\clist_new:N \l_rectstack_rect_order_clist
\bool_new:N \l_diststack_random_order_bool

\int_new:N \l__rectstack_rect_color_count_int
\int_new:N \l__rectstack_current_rect_int
\fp_new:N \l__rectstack_current_rect_width_fp
\fp_new:N \l__rectstack_current_rect_ypos_fp
\seq_new:N \l__rectstack_rect_order_seq

\cs_generate_variant:Nn \int_set:Nn { Ne }
\keys_define:nn { rectstack } {
    base ~ width           .fp_set:N    = \l_rectstack_base_width_fp ,
    base ~ width           .initial:n   = { 6cm } ,
    rect ~ count           .int_set:N   = \l_rectstack_rect_count_int ,
    rect ~ count           .initial:n   = { 1 } ,
    rect ~ height          .fp_set:N    = \l_rectstack_rect_height_fp ,
    rect ~ height          .initial:n   = { 0.5cm } ,
    rect ~ sep             .fp_set:N    = \l_rectstack_rect_sep_fp ,
    rect ~ sep             .initial:n   = { 2pt } ,
    rect ~ min ~ width     .fp_set:N    = \l_rectstack_rect_min_width_fp ,
    rect ~ min ~ width     .initial:n   = { 1cm } ,
    rect ~ max ~ width     .fp_set:N    = \l_rectstack_rect_max_width_fp ,
    rect ~ max ~ width     .initial:n   = { 5cm } ,
    rect ~ corner ~ radius .dim_set:N   = \l_rectstack_rect_corner_radius_dim ,
    rect ~ corner ~ radius .initial:n   = { 3pt } ,
    rect ~ colors          .code:n      = {
        \clist_set:Nn \l_rectstack_rect_colors_clist {#1}
        \int_set:Ne \l__rectstack_rect_color_count_int { 
            \clist_count:N \l_rectstack_rect_colors_clist 
        }
    } ,
    rect ~ colors          .initial:n   = { black!25 },
    order                  .clist_set:N = \l_diststack_rect_order_clist , 
    random                 .bool_set:N  = \l_diststack_random_order_bool ,
    random                 .default:n   = { true }
}

\cs_generate_variant:Nn \clist_item:Nn { Ne }
\NewDocumentCommand{\drawrectstack}{ O{} }{
    \keys_set:nn { rectstack } { #1 }
    
    \bool_if:NT \l_diststack_random_order_bool {
        \seq_clear:N \l__rectstack_rect_order_seq
        \int_step_inline:nn { \l_rectstack_rect_count_int } {
            \seq_put_right:Nn \l__rectstack_rect_order_seq { ##1 }
        }
        \seq_shuffle:N \l__rectstack_rect_order_seq
        \clist_set_from_seq:NN \l_diststack_rect_order_clist \l__rectstack_rect_order_seq
    } 
        
    \draw_begin:

    \draw_path_moveto:n { -0.5 * \l_rectstack_base_width_fp , 0cm }
    \draw_path_lineto:n { 0.5 * \l_rectstack_base_width_fp , 0cm }
    \draw_path_use_clear:n { stroke }

    \draw_path_corner_arc:nn 
        { \l_rectstack_rect_corner_radius_dim } 
        { \l_rectstack_rect_corner_radius_dim }
    \int_step_inline:nn { \l_rectstack_rect_count_int } {
        \clist_if_empty:NTF \l_diststack_rect_order_clist { 
            \int_set:Nn \l__rectstack_current_rect_int { ##1 }
        } { 
            \int_set:Ne \l__rectstack_current_rect_int { 
                \clist_item:Nn \l_diststack_rect_order_clist { ##1 }
            }
        }
        \fp_set:Nn \l__rectstack_current_rect_width_fp { 
            \l_rectstack_rect_max_width_fp - 
            ( \l_rectstack_rect_max_width_fp - \l_rectstack_rect_min_width_fp ) / 
            ( \l_rectstack_rect_count_int - 1 ) * ( \l__rectstack_current_rect_int - 1 ) 
        }
        \fp_set:Nn \l__rectstack_current_rect_ypos_fp { 
            ( ##1 - 1 ) * \l_rectstack_rect_height_fp + ##1 * \l_rectstack_rect_sep_fp 
        }
        \exp_args:Ne \color_fill:n { \clist_item:Ne \l_rectstack_rect_colors_clist {
            \int_mod:nn { ##1 - 1 } { \l__rectstack_rect_color_count_int } + 1 } 
        }
        \draw_path_rectangle_corners:nn 
            { -0.5 * \l__rectstack_current_rect_width_fp , 
                \l__rectstack_current_rect_ypos_fp }
            { 0.5 * \l__rectstack_current_rect_width_fp , 
                \l__rectstack_current_rect_ypos_fp + \l_rectstack_rect_height_fp }
        \draw_path_use_clear:n { fill }
    }
    \draw_end:
}


\ExplSyntaxOff


\begin{document}

\drawrectstack[rect count=7]

\drawrectstack[rect count=5, random, rect colors={cyan}]

\drawrectstack[rect count=6, order={4,1,2,6,5,3}, rect colors={red!20, red, blue, green}]

\end{document}

在此处输入图片描述

相关内容