如何使用 tikz 绘制带有矩形的下图?

如何使用 tikz 绘制带有矩形的下图?

我想使用 tikz 绘制以下内容(在指定的位置使用灰色和黑色):

在此处输入图片描述

请注意$\chi-\delta/2$$\chi-\delta/4$以灰色区域为中心(而不是整个灰色+白色区域),而$\delta$以整个黑色+白色区域为中心(而不是仅黑色区域)。

梅威瑟:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tikzpicture}
\node[rectangle,draw,minimum width=1in, minimum height=1in, fill=white] (a) {}
node[rectangle,draw,minimum width=2in, minimum height=1in, fill=gray] (b) [left=of a] {}
node[rectangle,draw,minimum width=0.5in, minimum height=1in, fill=white] (c) [left=of a] {}
node[rectangle,draw,minimum width=0.5in, minimum height=1in, fill=black] (d) [right=of c] {};
\end{tikzpicture}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

输出: 在此处输入图片描述

谢谢!

答案1

像这样:

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

\begin{document}
    \begin{tikzpicture}

    \draw[->] (0cm,.5cm) -- (0cm,-2.5cm)
        node[right] {$\gamma$};

    \foreach \y / \l / \da / \db / \la / \lb in {
        0cm / $0$ / .5cm / .5cm / $\delta$ / $\chi - \frac{\delta}{2}$ ,
        -2cm / $\infty$ / .25cm / .25cm / $\delta$ / $\chi - \frac{\delta}{4}$
    } {
        \draw (-.1cm,\y) -- (.1cm,\y)
            node[right] {\l}; 
        
        \fill ({-.5cm-\da},{\y+.5cm}) rectangle +({-1cm+\da},-1cm);
        \draw (-.5cm,{\y+.5cm}) rectangle +(-1cm,-1cm); 
        \draw[->] (-.5cm,\y) -- +(-\da,0cm);
        
        \node at (-1cm,{\y+0.75cm}) {\la};

        \fill[gray] ({-2cm-\db},{\y+.5cm}) rectangle +({-2cm+\db},-1cm);
        \draw (-2cm,{\y+.5cm}) rectangle +(-2cm,-1cm);  
        \draw[->] (-2cm,\y) -- +(-\db,0cm);
        
        \node at ({-3cm-\db/2},{\y+0.75cm}) {\lb};
        
        \node at (-1.75cm,\y) {$+$};
    }
    
    \end{tikzpicture}
\end{document}

在此处输入图片描述


使用(实验性)包的实现l3draw

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

\begin{document}
    \ExplSyntaxOn
    \draw_begin:
    
    \cs_new:Npn \anonymous_diagram_arrow_tip:nn #1#2 {
        \draw_scope_begin:
            \draw_transform_shift:n { #1 }
            \draw_transform_rotate:n { #2 }
            \draw_path_moveto:n { -.1cm , -.1cm }
            \draw_path_lineto:n { 0cm , 0cm }
            \draw_path_lineto:n { .1cm , -.1cm }
            \draw_path_use_clear:n { stroke }
        \draw_scope_end:
    }
    
    \cs_new:Npn \anonymous_diagram_boxes:nnnnn #1#2#3#4#5 {
        \draw_path_rectangle:nn { -.5cm , ( .5cm + #1 ) } { -1cm , -1cm }
        \draw_path_use_clear:n { stroke }
        
        \draw_path_rectangle:nn { ( -.5cm - #2 ) , ( .5cm + #1 ) } { ( -1cm + #2 ) , -1cm }
        \draw_path_use_clear:n { fill , stroke }

        
        \draw_path_moveto:n { ( -.5cm ) , #1 }
        \draw_path_lineto:n { ( -.5cm - #2 ) , #1 }
        \draw_path_use_clear:n { stroke }
        \anonymous_diagram_arrow_tip:nn { ( -.5cm - #2 ) , #1 } { 90 }
        
        \hcoffin_set:Nn \l_tmpa_coffin { #4 }
        \draw_coffin_use:Nnnn \l_tmpa_coffin { vc } { hc } { -1cm , ( .75cm + #1 ) }
        
        
        \draw_path_rectangle:nn { -2cm , ( .5cm + #1 ) } { -2cm , -1cm }
        \draw_path_use_clear:n { stroke }
        
        \draw_scope_begin:
            \color_fill:n { black!50 }
            \draw_path_rectangle:nn { ( -2cm - #3 ) , ( .5cm + #1 ) } { ( -2cm + #3 ) , -1cm }
            \draw_path_use_clear:n { fill , stroke }
        \draw_scope_end:
        
        \draw_path_moveto:n { ( -2cm ) , #1 }
        \draw_path_lineto:n { ( -2cm - #3 ) , #1 }
        \draw_path_use_clear:n { stroke }
        \anonymous_diagram_arrow_tip:nn { ( -2cm - #3 ) , #1 } { 90 }
        
        \hcoffin_set:Nn \l_tmpa_coffin { #5 }
        \draw_coffin_use:Nnnn \l_tmpa_coffin { vc } { hc } { -3cm , ( .75cm + #1 ) }


        \hcoffin_set:Nn \l_tmpa_coffin { $+$ }
        \draw_coffin_use:Nnnn \l_tmpa_coffin { vc } { hc } { -1.75cm , #1 }
    }
    
    \draw_path_moveto:n { 0cm , .5cm }
    \draw_path_lineto:n { 0cm , -2.5cm }
    \draw_path_use_clear:n { stroke }
    
    \anonymous_diagram_arrow_tip:nn { 0cm , -2.5cm } { 180 }
    
    \hcoffin_set:Nn \l_tmpa_coffin { $\gamma$ }
    \draw_coffin_use:Nnnn \l_tmpa_coffin { vc } { l } { .2cm , -2.5cm }
    
    \clist_map_inline:nn { 
        { 0cm , $0$ , .5cm , .5cm , $\delta$ , $\chi - \frac{\delta}{2}$ } , 
        { -2cm , $\infty$ , .25cm , .25cm , $\delta$ , $\chi - \frac{\delta}{4}$ } 
    } { 
        \draw_path_moveto:n { -.1cm , \clist_item:nn { #1 } { 1 } }
        \draw_path_lineto:n { .1cm , \clist_item:nn { #1 } { 1 } }
        
        \hcoffin_set:Nn \l_tmpa_coffin { \clist_item:nn { #1 } { 2 } }
        \draw_coffin_use:Nnnn \l_tmpa_coffin { vc } { l } { .2cm , \clist_item:nn { #1 } { 1 } }
        
        \anonymous_diagram_boxes:nnnnn { 
            \clist_item:nn { #1 } { 1 } 
        } { 
            \clist_item:nn { #1 } { 3 } 
        } { 
            \clist_item:nn { #1 } { 4 } 
        } { 
            \clist_item:nn { #1 } { 5 }
        } {
            \clist_item:nn { #1 } { 6 }
        }   
    }   
        
    \draw_end:
    \ExplSyntaxOff
\end{document}

(与上面的输出大致相同。)

相关内容