9

Assume you have a matrix of 2x2 tikz figures and you decide to use subfloats. The code

\documentclass{report}

\usepackage{subfig} \usepackage{tikz}

\begin{document}

\begin{figure}[ht!] \center \subfloat[Figure 1]{ \begin{tikzpicture} \filldraw (0,0) rectangle (1,1); \end{tikzpicture}
} \quad \subfloat[Figure 2]{ \begin{tikzpicture} \filldraw (0,0) rectangle (3,3); \end{tikzpicture}
} \ \subfloat[Figure 3]{ \begin{tikzpicture} \filldraw (0,0) rectangle (2,2); \end{tikzpicture}
} \quad \subfloat[Figure 4]{ \begin{tikzpicture} \filldraw (0,0) rectangle (5,5); \end{tikzpicture}
} \caption{Test} \label{test} \end{figure}

\end{document}

gives the result

enter image description here

How can one now obtain a symmetric alignment in both lines and columns, as shown below? I suppose this is a standard question and should have a simple solution, but so far I've only found methods such as

  • Using a tabular (see here)
  • Using valign with includegraphics (see here) but I am using tikzpictures.
  • Using minipage (see here)
  • ...

enter image description here

Karlo
  • 3,257

3 Answers3

5

With use of tabularx for positioning of images and their captions:

\documentclass{report}
\usepackage[skip=1ex]{caption}
\usepackage{tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=#1\hsize}X}
\usepackage{tikz}

\begin{document}

\begin{figure}[htp!] \centering \renewcommand\tabularxcolumn[1]{m{#1}} \begin{tabularx}{0.8\linewidth}{C{0.8}C{1.2}} \tikz\filldraw (0,0) rectangle (1,1); & \tikz\filldraw (0,0) rectangle (3,3); \
\caption{Figure 1} & \caption{Figure 2} \ \tikz\filldraw (0,0) rectangle (2,2); & \tikz\filldraw (0,0) rectangle (5,5); \
\caption{Figure 3} & \caption{Figure 4} \[-2ex] \end{tabularx}

\caption{Test} \label{test} \end{figure} \end{document}

enter image description here

Zarko
  • 296,517
4

Probably not the most elegant code, but the following should result in an output close to the expected one.

enter image description here

\documentclass{report}
\usepackage{subcaption}
\usepackage{tikz}

\begin{document}

\begin{figure}[ht!] \centering \begin{subfigure}{0.2\textwidth} \centering \begin{tikzpicture} \filldraw (0,0) rectangle (1,1); \end{tikzpicture}
\end{subfigure}\quad \begin{subfigure}{0.45\textwidth} \centering \begin{tikzpicture} \filldraw (0,0) rectangle (3,3); \end{tikzpicture}
\end{subfigure} \smallskip

\begin{subfigure}{0.2\textwidth}
    \caption{Figure 1} 
\end{subfigure}\quad
\begin{subfigure}{0.45\textwidth}
    \caption{Figure 2} 
\end{subfigure}

\bigskip
\begin{subfigure}{0.2\textwidth}
  \centering
    \begin{tikzpicture}
    \filldraw (0,0) rectangle (2,2);
    \end{tikzpicture}  
\end{subfigure}\quad
\begin{subfigure}{0.45\textwidth}
  \centering
    \begin{tikzpicture}
    \filldraw (0,0) rectangle (5,5);
    \end{tikzpicture}  
\end{subfigure} 
\smallskip

\begin{subfigure}{0.2\textwidth}
    \caption{Figure 3} 
\end{subfigure}\quad
\begin{subfigure}{0.45\textwidth}
    \caption{Figure 4} 
\end{subfigure} 
\caption{Test}
\label{test}

\end{figure}

\end{document}

leandriis
  • 62,593
4

If you don't have too many of these structures, you can cheat.

\documentclass{report}

\usepackage{subfig} \usepackage{tikz}

\begin{document}

\begin{figure}[htp!] \centering

\begin{tabular}{cc} \begin{tabular}{@{}c@{}} \begin{tikzpicture} \filldraw (0,0) rectangle (1,1); \end{tikzpicture} \end{tabular} & \begin{tabular}{@{}c@{}} \begin{tikzpicture} \filldraw (0,0) rectangle (3,3); \end{tikzpicture} \end{tabular} \[-2ex] \subfloat[Figure 1]{\hspace{3cm}} & \subfloat[Figure 2]{\hspace{3cm}} \[6ex] \begin{tabular}{@{}c@{}} \begin{tikzpicture} \filldraw (0,0) rectangle (2,2); \end{tikzpicture} \end{tabular} & \begin{tabular}{@{}c@{}} \begin{tikzpicture} \filldraw (0,0) rectangle (5,5); \end{tikzpicture} \end{tabular} \[-2ex] \subfloat[Figure 3]{\hspace{3cm}} & \subfloat[Figure 4]{\hspace{3cm}} \end{tabular}

\caption{Test} \label{test}

\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712