Make the width of the figure (including 2 minipages) same as the paper

I have 2 minipages side by side as follows:

\begin{figure}
\centering
\fbox{
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{0.48\linewidth}%
left part
\end{minipage}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{0.48\linewidth}%
right part
\end{minipage}
\end{minipage}}
\captionof{figure}{An example program}\label{fig:An example program}
\end{figure}


Now the width of the whole figure is a little bit shorter than the width of the paper. I would like to make them exactly the same.

Could anyone tell me which parameter I could change to realize that?

-
change 0.48 to 0.5? – Roelof Spijker Mar 22 '12 at 16:04
That will exceed the width of the paper... – SoftTimur Mar 22 '12 at 16:05
Always add a % after the trailing { of \fbox to avoid a space being inserted here by the end-of-line character. This will give you some extra whitespace at the left of the border. – Martin Scharrer Mar 22 '12 at 16:19
Also, no need to use \captionof{figure} inside figure. Just use \caption. – Martin Scharrer Mar 22 '12 at 16:21

Use \framebox instead of \fbox.

 \begin{figure}
\centering
\framebox[1\linewidth][t]{%
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{1\linewidth}%
left part is going to be very big sentence that goes into two lines
\end{minipage}%
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{1\linewidth}%
right part is going to be very big sentence that goes into two lines
\end{minipage}%
\end{minipage}}%
\captionof{figure}{An example program}\label{fig:An example program}
\end{figure}


EDIT: As per Martin's comments on using \framebox and verbatim text/catcode changing material, this code will work as desired using \fbox

\begin{figure}
\centering
\fbox{%
\begin{minipage}[t]{1\linewidth-2\fboxsep-2\fboxrule}%
\begin{minipage}[t]{0.48\linewidth}%
left part right part is going to be very big sentence that goes into two lines
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}%
right part right part is going to be very big sentence that goes into two lines
\end{minipage}
\end{minipage}}
\captionof{figure}{An example program}\label{fig:An example program}
\end{figure}


However, you have to use

\usepackage{calc} % as per Stephan's advice.


for this to work properly.

PS. If you want this to go up to the paper width (do you really want to do that?), you have to reduce the margins to zero using geometry.

I am of the opinion that you can simply use \caption inside figure environment (instead of \captionof which will be used outside \figure environment)

-
Always add a % after the trailing { to avoid a space being inserted here by the end-of-line character. You can see on your example image that there is an extra space on the left because of this. – Martin Scharrer Mar 22 '12 at 16:19
@MartinScharrer Thanks for noting. A typo which occured due to some laziness. I have corrected it. Thanks – Harish Kumar Mar 22 '12 at 16:22
I think it should be \dimexpr\linewidth-2\fboxsep-2\fboxrule\relax instead of 1\linewidth to compensate for the space added by the frame. Otherwise the content of the framebox will be exactly be \linewidth, not the framebox itself., – Martin Scharrer Mar 22 '12 at 16:24
@MartinScharrer Does it look like that? Pl. see the new image. – Harish Kumar Mar 22 '12 at 16:30

You need to remove the space after \fbox{:

\fbox{%


Secondly, replace 0.48\linewidth by

0.5\linewidth-\fboxsep-\fboxrule


which means deducing the amount of space added horizontally by \fbox.

You need to load the calc package if you're not already doing that.

As Harish Kumar noticed, \linewidth is 'counted locally' inside minipage, so don't apply this twice if you don't want to (why are you nesting minipages anyway?)

Your example again, with changes and some unneccessary stuff removed:

\begin{figure}
\centering
\fbox{%
\begin{minipage}[t]{0.5\linewidth-\fboxsep-\fboxrule}%
left part
\end{minipage}\hfill
\begin{minipage}[t]{0.5\linewidth-\fboxsep-\fboxrule}%
right part
\end{minipage}}
\captionof{figure}{An example program}\label{fig:An example program}
\end{figure}

-

The other answers tell you how to force the frame width. But in case the frame was just for debugging, or just out of interest as it's not mentioned in the other answers; the reason why the edges of your two minipages were not forced out to the page edges was that the \hfill between them has no effect and is the same as \hspace{0pt} \fbox (like \mbox or \hbox) sets its content to natural width and \hfill glue, although stretchy has natural width 0pt. By using \framebox (or just removing the \fbox) TeX is trying to set the line to a specified width and so the stretch implied by \hfill is allowed to push the minipages apart.

-

If you want your minipages exactly 0.5\textwidth, and you want to frame it, I would recommend the mdframed package, otherwise you have to do manual adjustments for the \fbox command as demonstrated in the other answers.

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{mdframed}

\begin{document}
\begin{figure}
\begin{mdframed}
\begin{minipage}[t]{0.5\linewidth}
\begin{minipage}[t]{0.48\linewidth}
left part
\end{minipage}
\end{minipage}% <--- NOTE!!
\begin{minipage}[t]{0.5\linewidth}
\begin{minipage}[t]{0.48\linewidth}
right part
\end{minipage}
\end{minipage}
\end{mdframed}
\caption{An example program}\label{fig:An example program}
\end{figure}
\end{document}

-

You can force a frame with a given content width using \framebox{<width>}{<content>} instead of \fbox{<content>}. It places the content first in a horizontal box of the given width before drawing the frame around it. The text width is given either by \textwidth or \linewidth (the latter is effected by list-like environment including quotations). If you want the frame to be exactly the text width than you need to subtract the frame rule width and separation twice (once for each side) from that value.

I'm not sure why you are using double minipages so I didn't changed anything here. Note that I additionally added a % after the trailing { to avoid a spurious space there and the change to \caption.

\begin{figure}
\centering
\framebox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{%
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{0.48\linewidth}%
left part
\end{minipage}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}%
\begin{minipage}[t]{0.48\linewidth}%
right part
\end{minipage}
\end{minipage}}
\caption{An example program}\label{fig:An example program}
\end{figure}


Note that \framebox reads the content as a macro argument and therefore you can't use verbatim or either catcode changing material inside it. If you want that you need to either use \Framebox from the realboxes package or \adjustbox{minipage=\linewidth-2\fboxsep-2\fboxrule,fbox} (also available as adjustbox environment) from the adjustbox package (both actually written by me). Depending on the content you could also use the adjustbox keys width=\linewidth-2\fboxsep-2\fboxrule,fbox instead which zoom the content to the given width first. However, you seem to have text content where this is not recommendable.

-

To make them equal to paper width as you requested, you need to firstly compensate for the \fboxsep and fboxrule widths and also adjust your geometry and remove the geometry parameters. If you want them to be equal to the \textwidth then replace paper width with \textwidth in the minimal below:

\documentclass{article}
\usepackage[showframe=true,left=0pt,right=0pt]{geometry}
\usepackage{caption,xcolor}
\fboxsep=5pt
\fboxrule=10.0pt
\begin{document}
\begin{figure}
\centering
\color{red}
\fbox{%
\begin{minipage}[t]{\the\dimexpr(0.5\paperwidth-\the\fboxrule-\the\fboxsep)\relax}
\fbox{\begin{minipage}[t]{0.9\linewidth}
\color{black}
left part
\end{minipage}}
\end{minipage}\hspace{3pt}%
\begin{minipage}[t]{\the\dimexpr(0.5\paperwidth-\the\fboxrule-\the\fboxsep)\relax}
\fbox{\begin{minipage}[t]{0.9\linewidth}
\color{black}
right part
\end{minipage}}
\end{minipage}}%
\captionof{figure}{An example program}\label{fig:An example program}
\end{figure}
\end{document}


-