[Tex/LaTex] Two independent figures side by side, problem with caption size

captionsfloats

I want to display two figures side by side but captions seem to overlap no matter the minipage width. Is there anyway to reduce caption size locally? Or make it respect margins?

Here is my code:

\documentclass[12pt,a4paper,openright,oneside]{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{tikz}

\newcommand{\exedout}{
\begin{tikzpicture}
\path node (LL) {}
    ++ (0.8\textwidth, 0.4\textheight) node (UR) {}
    (LL -| UR) node (LR) {}
    (LL |- UR) node (UL) {};
\draw (LL) rectangle (UR) (LL) -- (UR) (UL) -- (LR);
\end{tikzpicture}
}


\begin{document}

\begin{figure}
    \begin{minipage}{0.45\textwidth}
        \exedout
    \caption{Caption number one with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...}
    \end{minipage}% 
    \begin{minipage}{0.45\textwidth}
        \exedout
    \caption{Caption number two with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...} 
    \end{minipage}
\end{figure}

\end{document}

Which outputs:

enter image description here

How can I fix this?

PD: How do I use the example images such as 'example-image-a' and so on that i have seen numerous times on this site?

Best Answer

Since you're using the caption package, it is easy to set the width of the caption as you wish. You can add \captionsetup{width=0.4\textwidth}, for example, to limit the width of the caption text to only 0.4\textwidth.

\documentclass[12pt,a4paper,openright,oneside]{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{tikz}

\newcommand{\exedout}{
\begin{tikzpicture}
\path node (LL) {}
    ++ (0.8\textwidth, 0.4\textheight) node (UR) {}
    (LL -| UR) node (LR) {}
    (LL |- UR) node (UL) {};
\draw (LL) rectangle (UR) (LL) -- (UR) (UL) -- (LR);
\end{tikzpicture}
}    

\begin{document}

\begin{figure}
\captionsetup{width=0.4\textwidth}
    \begin{minipage}{0.45\textwidth}\centering
        \exedout
    \caption{Caption number one with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...}
    \end{minipage}% 
    \begin{minipage}{0.45\textwidth}\centering
        \exedout
    \caption{Caption number two with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...}
    \end{minipage}
\end{figure}

\end{document}

enter image description here

Another important observation is that the minipages have no space in-between. So, if you just add \hfill between them, the problem totally disappears (assuming, of course, that the sum of the two widths is less than a linewidth):

\begin{figure}
    \begin{minipage}{0.45\textwidth}\centering
        \exedout
    \caption{Caption number one with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...}
    \end{minipage}%
    \hfill 
    \begin{minipage}{0.45\textwidth}\centering
        \exedout
    \caption{Caption number two with enough text to overlap with caption number two bla... bla...bla... bla...bla... bla...}
    \end{minipage}
\end{figure}

enter image description here