使用子图对齐多个 tikz 和算法

使用子图对齐多个 tikz 和算法
\documentclass{article}

\usepackage{graphicx,subcaption}
\usepackage{algpseudocode}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{shapes,arrows,chains}
\tikzstyle{line} = [draw, -latex']


\begin{document}
    \begin{figure}[h]
        \centering
        \begin{subfigure}[t]{.5\textwidth}
            \centering
            \begin{algorithmic}[1]
                \Function{CCCCCCCCCCCC}{}
                \State $AAAAAAAAAAAAAA$
                \State $BBBBBBBBBBBBBBB$
                \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
\State $XXXXXXXXXXXXX$
                \EndFunction
            \end{algorithmic}
        \end{subfigure}
        \begin{subfigure}[c]{.45\textwidth}
            \begin{tikzpicture}[xscale=4,yscale=1]
                % Leiste unten
                \draw[thick] (0,0) -- (1,0);
                \draw[thick] (0,.1) -- (0,.-.1);
                \draw (.2,.05) -- (.2,-.05);
                \draw (.4,.05) -- (.4,-.05);
                \draw (.6,.05) -- (.6,-.05);
                \draw (.8,.05) -- (.8,-.05);
                \draw[thick] (1,.1) -- (1,.-.1);
                % Knoten unten
                
                \newcommand\AONE{.5*0}
                \newcommand\ATWO{.5*1}
                \newcommand\ATHREE{.5*2}
                
                \newcommand\BLOCKHEIGHT{0.4}
                % U
                \draw[fill=red!30] (0,0.2+\AONE) rectangle ++(0.3,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\AONE) rectangle ++(0.2,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\ATWO) rectangle ++(0.2,\BLOCKHEIGHT);
                \draw[fill=green!30] (0.5,0.2+\ATWO) rectangle ++(0.5,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\ATHREE) rectangle ++(0.2,\BLOCKHEIGHT);
                
                
            \end{tikzpicture}  
        \end{subfigure}
            \begin{subfigure}[t]{.5\textwidth}
        \centering
        \begin{algorithmic}[1]
            \Function{CCCCCCCCCCCC}{}
            \State $AAAAAAAAAAAAAA$
            \State $BBBBBBBBBBBBBBB$
            \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
            \EndFunction
        \end{algorithmic}
    \end{subfigure}
    \begin{subfigure}[c]{.45\textwidth}
        \begin{tikzpicture}[xscale=4,yscale=1]
            % Leiste unten
            \draw[thick] (0,0) -- (1,0);
            \draw[thick] (0,.1) -- (0,.-.1);
            \draw (.2,.05) -- (.2,-.05);
            \draw (.4,.05) -- (.4,-.05);
            \draw (.6,.05) -- (.6,-.05);
            \draw (.8,.05) -- (.8,-.05);
            \draw[thick] (1,.1) -- (1,.-.1);
            % Knoten unten
            
            \newcommand\AONE{.5*0}
            \newcommand\ATWO{.5*1}
            \newcommand\ATHREE{.5*2}
            
            \newcommand\BLOCKHEIGHT{0.4}
            % U
            \draw[fill=red!30] (0,0.2+\AONE) rectangle ++(0.3,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\AONE) rectangle ++(0.2,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\ATWO) rectangle ++(0.2,\BLOCKHEIGHT);
            \draw[fill=green!30] (0.5,0.2+\ATWO) rectangle ++(0.5,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\ATHREE) rectangle ++(0.2,\BLOCKHEIGHT);
            
            
        \end{tikzpicture}  
    \end{subfigure}
        \caption{AAAA}
        \label{fig:AA-AA}
    \end{figure}
\end{document}

我该如何解决这个问题?我希望两种算法都顶部对齐,并且每张图片都相对于左侧的相应算法垂直居中。

答案1

如果您希望 tikzpictures 与算法的中心对齐,则必须将两者的基线都放在中心(这是子图的默认设置)。算法会扩展以填充可用空间,因此\centering不会执行任何操作。Tikzpictures 可以居中,但我怀疑您希望它们左对齐。

\documentclass{article}

\usepackage{graphicx,subcaption}
\usepackage{algpseudocode}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{shapes,arrows,chains}
\tikzstyle{line} = [draw, -latex']


\begin{document}
    \begin{figure}[h]
        \centering
        \begin{subfigure}{.5\textwidth}
            \begin{algorithmic}[1]
                \Function{CCCCCCCCCCCC}{}
                \State $AAAAAAAAAAAAAA$
                \State $BBBBBBBBBBBBBBB$
                \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
                \EndFunction
            \end{algorithmic}
        \end{subfigure}\hfil
        \begin{subfigure}{.45\textwidth}
          \centering
            \begin{tikzpicture}[xscale=4,yscale=1]
                % Leiste unten
                \draw[thick] (0,0) -- (1,0);
                \draw[thick] (0,.1) -- (0,.-.1);
                \draw (.2,.05) -- (.2,-.05);
                \draw (.4,.05) -- (.4,-.05);
                \draw (.6,.05) -- (.6,-.05);
                \draw (.8,.05) -- (.8,-.05);
                \draw[thick] (1,.1) -- (1,.-.1);
                % Knoten unten
                
                \newcommand\AONE{.5*0}
                \newcommand\ATWO{.5*1}
                \newcommand\ATHREE{.5*2}
                
                \newcommand\BLOCKHEIGHT{0.4}
                % U
                \draw[fill=red!30] (0,0.2+\AONE) rectangle ++(0.3,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\AONE) rectangle ++(0.2,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\ATWO) rectangle ++(0.2,\BLOCKHEIGHT);
                \draw[fill=green!30] (0.5,0.2+\ATWO) rectangle ++(0.5,\BLOCKHEIGHT);
                \draw[fill=blue!30] (0.3,0.2+\ATHREE) rectangle ++(0.2,\BLOCKHEIGHT);
                
                
            \end{tikzpicture}  
        \end{subfigure}
        \par\vskip\floatsep
        \begin{subfigure}{.5\textwidth}
        \begin{algorithmic}[1]
            \Function{CCCCCCCCCCCC}{}
            \State $AAAAAAAAAAAAAA$
            \State $BBBBBBBBBBBBBBB$
            \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
            \State $XXXXXXXXXXXXX$
            \EndFunction
        \end{algorithmic}
    \end{subfigure}\hfil
    \begin{subfigure}{.45\textwidth}
        \centering
        \begin{tikzpicture}[xscale=4,yscale=1]
            % Leiste unten
            \draw[thick] (0,0) -- (1,0);
            \draw[thick] (0,.1) -- (0,.-.1);
            \draw (.2,.05) -- (.2,-.05);
            \draw (.4,.05) -- (.4,-.05);
            \draw (.6,.05) -- (.6,-.05);
            \draw (.8,.05) -- (.8,-.05);
            \draw[thick] (1,.1) -- (1,.-.1);
            % Knoten unten
            
            \newcommand\AONE{.5*0}
            \newcommand\ATWO{.5*1}
            \newcommand\ATHREE{.5*2}
            
            \newcommand\BLOCKHEIGHT{0.4}
            % U
            \draw[fill=red!30] (0,0.2+\AONE) rectangle ++(0.3,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\AONE) rectangle ++(0.2,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\ATWO) rectangle ++(0.2,\BLOCKHEIGHT);
            \draw[fill=green!30] (0.5,0.2+\ATWO) rectangle ++(0.5,\BLOCKHEIGHT);
            \draw[fill=blue!30] (0.3,0.2+\ATHREE) rectangle ++(0.2,\BLOCKHEIGHT);
            
            
        \end{tikzpicture}  
    \end{subfigure}
        \caption{AAAA}
        \label{fig:AA-AA}
    \end{figure}
\end{document}

演示

相关内容