Question
I want to place two TikZ pictures side by side:
Note:
- They are vertically centered.
- One is wider than half the page.
- The three gaps are equal (as if by using
\hfil <a> \hfil <b> \hfil
). - The captions are vertically aligned.
Long captions should also be handled correctly (i.e. they should wrap, just like they normally do).
The solution should avoid trial-and-error lengths such as \begin{minipage}{5.95cm}
, \begin{minipage}{0.3\textwidth}
and \qquad
.
What I've tried
I've looked at about two dozen questions here but none of the answers were satisfactory. This section is an attempt to summarize them and address their flaws.
Assume the following preamble:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subfig}
\usepackage{floatrow}
\usepackage{tabularx}
\newcommand\rectangle[2]{%
\begin{tikzpicture}[baseline=(current bounding box.center)]
\draw(0,0) rectangle(#1, #2);
\end{tikzpicture}%
}
minipage
\vspace{0.5cm}
\noindent
\begin{minipage}{5.95cm} % (1)
\centering
\rectangle{4}{8}
\captionsetup{type=figure}
\caption{A tall boi.}
\end{minipage}
\begin{minipage}{8cm}
\rectangle{8}{4}
\captionsetup{type=figure}
\caption{A wide boi.} % (2)
\end{minipage}
\vspace{0.5cm} % (3)
Problems
- The minipage widths are determined through trial and error.
- The captions aren't vertically aligned.
- I don't know how much vspace to use. If I don't use any, there is no vertical space between the figures and the paragraphs.
subfloat
\begin{figure}[H]
\centering
\subfloat[\centering A tall boi.]{% (1)
\rectangle{4}{8}
}%
\qquad% (2)
\subfloat[\centering A wide boi.]{% (3)
\rectangle{8}{4}
}
\end{figure}
Problems
- The captions are a different style and have different numbering.
- The spacing between the figures is determined through trial and error.
- The captions aren't vertically aligned.
floatrow
\begin{figure}[H]
\centering
\begin{floatrow}
\ffigbox[\FBwidth]{\caption{A tall boi.}}{%
\rectangle{4}{8}
}% (1)
\ffigbox[\FBwidth]{\caption{A wide boi.}}{%
\rectangle{8}{4} % (2)
}%
\end{floatrow}
\end{figure}
Problems
- The spacing is wrong.
- The figures aren't vertically centered.
tabularx
\vspace{0.5cm} % (1)
\noindent
\begin{tabularx}{\textwidth}{XX} % (2)
\rectangle{4}{8} &
\rectangle{8}{4} \\
\captionsetup{type=figure}
\caption{A tall boi.} &
\captionsetup{type=figure}
\caption{A wide boi.}
\end{tabularx}
Problems
\vspace
again.- Both columns are equally wide (wide enough to fit the second figure).
This is the best one so far
Minimal working example
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
\newcommand\rectangle[2]{%
\begin{tikzpicture}[baseline=(current bounding box.center)]
\draw(0,0) rectangle(#1, #2);
\end{tikzpicture}%
}
\begin{document}
\lipsum[1][1-3]
\noindent\hfil
\rectangle{4}{8}
\hfil
\rectangle{8}{4}
\hfil
\lipsum[2][1-3]
\end{document}