如何将 tikz 图片并排放置?

如何将 tikz 图片并排放置?

我想将这两个图并排放在 A4 纸上。我不知道为什么subfigurenorresizebox不起作用。我正在 Overleaf 中写这篇文章。

在此处输入图片描述

这是我对上图的代码:

\documentclass{article}
\usepackage[utf8]{inputenc}

%\usepackage{caption}
\usepackage{subcaption}
\usepackage{graphicx}
%\usepackage{subfig}
\usepackage{tikz}
\usepackage{multirow}
\usetikzlibrary{positioning}

\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{125} % view angles
\tdplotsetrotatedcoords{0}{0}{0} 

\begin{document}
\begin{figure}
\centering
    \begin{subfigure}[b]{0.4\linewidth}
    \begin{tikzpicture}
    %\scalebox{0.8}{\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        scale=1,tdplot_rotated_coords,
        cube/.style={line width=1pt,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,line width=2pt,-latex},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %draw a grid in the x-y plane
    %\foreach \x in {-0.5,0,...,2.5}
    %    \foreach \y in {-0.5,0,...,2.5}
    %    {
    %        \draw[grid] (\x,-0.5,4) -- (\x,2.5,4);
    %        \draw[grid] (-0.5,\y,4) -- (2.5,\y,4);
    %    }

    %draw the main coordinate frame axes
    \draw[axis,tdplot_main_coords] (0,0,0) -- (3,0,0) node[anchor=north]{x};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,3,0) node[anchor=north]{y};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,4.5) node[anchor=west] {z};

    %draw the upper cube
    \draw[cube]                   (0.5,0,2) -- (2.5,0,2) -- (2.5,2,2) -- (0.5,2,2) -- cycle;
    \draw[cube]                                 (2.5,0,2) -- (2.5,0,4) -- (2.5,2,4) -- (2.5,2,2) -- cycle;
    \draw[cube]                                 (2.5,2,2) -- (0.5,2,2) -- (0.5,2,4) -- (2.5,2,4) -- cycle;
    \draw[cube]                                 (2.5,0,4) -- (2.5,2,4) -- (0.5,2,4) -- (0.5,0,4) -- cycle;
    \draw[cube]                                 (2.5,0,2) -- (2.5,0,4) -- (0.5,0,4) -- (0.5,0,2) -- cycle;
    \draw[cube]                                 (0.5,0,4) -- (0.5,2,4) -- (0.5,2,2) -- (0.5,0,2) -- cycle;
    %draw the bottom cube
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
    \draw[cube]                                 (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
    \draw[cube]         (0,0,2) -- (2,0,2) -- (2,2,2) -- (0,2,2) -- cycle;
    \draw[cube]                                 (2,2,2) -- (0,2,2) -- (0,2,0) -- (2,2,0) -- cycle;
    \draw[cube]                                 (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
    \end{tikzpicture}%
    \caption{}
    \label{}
    \end{subfigure}
    
    \begin{subfigure}[b]{0.4\linewidth}
    \begin{tikzpicture}
    %\scalebox{0.4}{\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        scale=1,tdplot_rotated_coords,
        cube/.style={line width=1pt,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,line width=2pt,-latex},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %draw a grid in the x-y plane
    %\foreach \x in {-0.5,0,...,2.5}
    %    \foreach \y in {-0.5,0,...,2.5}
    %    {
    %        \draw[grid] (\x,-0.5,4) -- (\x,2.5,4);
    %        \draw[grid] (-0.5,\y,4) -- (2.5,\y,4);
    %    }

    %draw the main coordinate frame axes
    \draw[axis,tdplot_main_coords] (0,0,0) -- (3,0,0) node[anchor=north]{x};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,3,0) node[anchor=north]{y};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,4.5) node[anchor=west] {z};

    %draw the upper cube
    \draw[cube]                   (-0.5,0,2) -- (1.5,0,2) -- (1.5,2,2) -- (-0.5,2,2) -- cycle;
    \draw[cube]                                 (1.5,0,2) -- (1.5,0,4) -- (1.5,2,4) -- (1.5,2,2) -- cycle;
    \draw[cube]                                 (1.5,2,2) -- (-0.5,2,2) -- (-0.5,2,4) -- (1.5,2,4) -- cycle;
    \draw[cube]                                 (1.5,0,4) -- (1.5,2,4) -- (-0.5,2,4) -- (-0.5,0,4) -- cycle;
    \draw[cube]                                 (1.5,0,2) -- (1.5,0,4) -- (-0.5,0,4) -- (-0.5,0,2) -- cycle;
    \draw[cube]                                 (-0.5,0,4) -- (-0.5,2,4) -- (-0.5,2,2) -- (-0.5,0,2) -- cycle;
    %draw the bottom cube
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
    \draw[cube]                                 (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
    \draw[cube]       (0,0,2) -- (2,0,2) -- (2,2,2) -- (0,2,2) -- cycle;
    \draw[cube]                                 (2,2,2) -- (0,2,2) -- (0,2,0) -- (2,2,0) -- cycle;
    \draw[cube]                                 (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
    \end{tikzpicture}%
    \caption{}
    \label{}
    \end{subfigure}
    
\caption{Caption}
\label{fig:Caption}
\end{figure}

\end{document}

答案1

您的代码几乎满足您的要求。唯一的问题是空行会发出\par,即换行符。只需将空行替换为\quad,例如。graphicx由 加载tikz,您可能希望避免使用空标签。

\documentclass{article}
\usepackage[utf8]{inputenc}

%\usepackage{caption}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{tikz}
\usepackage{multirow}
\usetikzlibrary{positioning}

\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{125} % view angles
\tdplotsetrotatedcoords{0}{0}{0} 

\begin{document}
\begin{figure}
\centering
    \begin{subfigure}[b]{0.4\linewidth}
    \begin{tikzpicture}
    %\scalebox{0.8}{\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        scale=1,tdplot_rotated_coords,
        cube/.style={line width=1pt,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,line width=2pt,-latex},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %draw a grid in the x-y plane
    %\foreach \x in {-0.5,0,...,2.5}
    %    \foreach \y in {-0.5,0,...,2.5}
    %    {
    %        \draw[grid] (\x,-0.5,4) -- (\x,2.5,4);
    %        \draw[grid] (-0.5,\y,4) -- (2.5,\y,4);
    %    }

    %draw the main coordinate frame axes
    \draw[axis,tdplot_main_coords] (0,0,0) -- (3,0,0) node[anchor=north]{x};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,3,0) node[anchor=north]{y};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,4.5) node[anchor=west] {z};

    %draw the upper cube
    \draw[cube]                   (0.5,0,2) -- (2.5,0,2) -- (2.5,2,2) -- (0.5,2,2) -- cycle;
    \draw[cube]                                 (2.5,0,2) -- (2.5,0,4) -- (2.5,2,4) -- (2.5,2,2) -- cycle;
    \draw[cube]                                 (2.5,2,2) -- (0.5,2,2) -- (0.5,2,4) -- (2.5,2,4) -- cycle;
    \draw[cube]                                 (2.5,0,4) -- (2.5,2,4) -- (0.5,2,4) -- (0.5,0,4) -- cycle;
    \draw[cube]                                 (2.5,0,2) -- (2.5,0,4) -- (0.5,0,4) -- (0.5,0,2) -- cycle;
    \draw[cube]                                 (0.5,0,4) -- (0.5,2,4) -- (0.5,2,2) -- (0.5,0,2) -- cycle;
    %draw the bottom cube
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
    \draw[cube]                                 (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
    \draw[cube]         (0,0,2) -- (2,0,2) -- (2,2,2) -- (0,2,2) -- cycle;
    \draw[cube]                                 (2,2,2) -- (0,2,2) -- (0,2,0) -- (2,2,0) -- cycle;
    \draw[cube]                                 (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
    \end{tikzpicture}%
    \caption{Left figure.}
    \label{fig:Left}
    \end{subfigure}
 \quad   
    \begin{subfigure}[b]{0.4\linewidth}
    \begin{tikzpicture}
    %\scalebox{0.4}{\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        scale=1,tdplot_rotated_coords,
        cube/.style={line width=1pt,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,line width=2pt,-latex},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %draw a grid in the x-y plane
    %\foreach \x in {-0.5,0,...,2.5}
    %    \foreach \y in {-0.5,0,...,2.5}
    %    {
    %        \draw[grid] (\x,-0.5,4) -- (\x,2.5,4);
    %        \draw[grid] (-0.5,\y,4) -- (2.5,\y,4);
    %    }

    %draw the main coordinate frame axes
    \draw[axis,tdplot_main_coords] (0,0,0) -- (3,0,0) node[anchor=north]{x};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,3,0) node[anchor=north]{y};
    \draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,4.5) node[anchor=west] {z};

    %draw the upper cube
    \draw[cube]                   (-0.5,0,2) -- (1.5,0,2) -- (1.5,2,2) -- (-0.5,2,2) -- cycle;
    \draw[cube]                                 (1.5,0,2) -- (1.5,0,4) -- (1.5,2,4) -- (1.5,2,2) -- cycle;
    \draw[cube]                                 (1.5,2,2) -- (-0.5,2,2) -- (-0.5,2,4) -- (1.5,2,4) -- cycle;
    \draw[cube]                                 (1.5,0,4) -- (1.5,2,4) -- (-0.5,2,4) -- (-0.5,0,4) -- cycle;
    \draw[cube]                                 (1.5,0,2) -- (1.5,0,4) -- (-0.5,0,4) -- (-0.5,0,2) -- cycle;
    \draw[cube]                                 (-0.5,0,4) -- (-0.5,2,4) -- (-0.5,2,2) -- (-0.5,0,2) -- cycle;
    %draw the bottom cube
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    \draw[cube]                                 (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
    \draw[cube]                                 (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
    \draw[cube]       (0,0,2) -- (2,0,2) -- (2,2,2) -- (0,2,2) -- cycle;
    \draw[cube]                                 (2,2,2) -- (0,2,2) -- (0,2,0) -- (2,2,0) -- cycle;
    \draw[cube]                                 (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
    \end{tikzpicture}%
    \caption{Right figure.}
    \label{fig:Right}
    \end{subfigure}
    
\caption{Caption.}
\label{fig:Caption}
\end{figure}

\end{document}

在此处输入图片描述

相关内容