# Center overfull subfloats

I have some plots that I want to group together into one figure as subfigures using subfloat. I want to align them as two per row but it seems they are too big (Overfull \hbox). Nonetheless I want them to be on one row and be centered which means they should have the same distance to the left and right border of the sheet.

The following code is a stripped down version of one of my figures and exhibits the problem.

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}
\begin{figure}
\centering
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\end{figure}
\blindtext
\end{document}


As you can see in the resulting page, the figures are much closer to the right border than to the left.

I don't seem to be able to move them in any way. As you can see I tried \centering and I also have tried \subfloatrow to no avail.

• Do you want to resize or just shift them? You can easily resize them to \textwidth using \resizebox{\textwidth}{!}{ } (inside the figure environment), IMHO it would look better that way. Do you really want to place it outside the margin?
– masu
Nov 15, 2013 at 20:54
• @masu: It seems that it doesn't works. I still get overfull warning! Maybe I do something wrong. Nov 15, 2013 at 21:02
• If resizing is an option, I'll submit it as answer.
– masu
Nov 15, 2013 at 21:05
• I guess some resizing would be okay although my original intention was to only shift as there seems to be enough space on the page. Nov 16, 2013 at 11:21

Use \makebox to set the "official width" to zero. This will remove the overfull \hbox warning (because TeX now considers the images to be zero points wide) and also center the box:

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}
\begin{figure}
\centering
\makebox[0pt]{
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
}
\end{figure}
\blindtext
\end{document}


• This feels a bit hacky. Could it interfere with other features? Nov 16, 2013 at 11:22
• @Nobody: Well, I guess some members of this site would consider your intent (figures that overlap into the margins) as "a bit hacky" :-) However, the implementation with \makebox isn't. It's just the standard way to tell LaTeX that we want it to not care about the horizontal oversize. Nov 16, 2013 at 12:32
• They were overlapping anyways, resizing is not (really) an option and breaking between the images disconnects the text from the images (that should be on the same height anyway). So what to do other than overlapping and at least have them centered? Nov 16, 2013 at 12:38
• @Nobody: I would do exactly this and also have done it a couple of times. Nov 16, 2013 at 23:27

Do you want to resize or just shift them? You can easily resize them to \textwidth using \resizebox{\textwidth}{!}{ } (inside the figure environment), IMHO it would look better that way.

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}

\begin{figure}
\resizebox{\textwidth}{!}{
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\subfloat[]{
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
}
\end{figure}

\blindtext

\end{document}


Here's a sample code for shifting, with the use subcaption as Donut E. Knot mentioned in his comment, it would be better than subfloat. This should get you started.

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{pgfplots}
\usepackage{subcaption}
\usepackage{subfig}
\usepackage{blindtext}

\newsavebox{\mybox}

\newlength{\boxwidth}

\newenvironment{myenv}{%
\begin{lrbox}{\mybox}%
}{%
\end{lrbox}%
\setlength{\boxwidth}{\the\wd\mybox}%
\hspace*{-0.5\boxwidth}\makebox[\the\wd\mybox][c]{%
\usebox{\mybox}%
}%
}

\begin{document}
\blindtext
\begin{figure}[hbtp]
\begin{myenv}
\begin{subfigure}[c]{0.6\textwidth}
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
\caption{Left figure}
\label{fig:Left}
\end{subfigure}
\begin{subfigure}[c]{0.6\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[]
\end{axis}
\end{tikzpicture}
\caption{Right figure}
\label{fig:Right}
\end{subfigure}
\end{myenv}
\caption{Left and right figures side by side}
\label{fig:Group}
\end{figure}
\blindtext
\end{document}

• My initial intention was and is to only shift them. Nov 16, 2013 at 11:22
• @Nobody: Have you tried the answers on this? tex.stackexchange.com/questions/128411/…
– masu
Nov 17, 2013 at 11:28
• @Nobody: Just added the shifting. I've used that instead of centering because you might want shift that a bit more because of the axis label.
– masu
Nov 18, 2013 at 13:00

A recommended solution with subcaption.

\documentclass[preview,border=12pt]{standalone}% change it back to your document class
\usepackage{pgfplots}
\usepackage{subcaption}
\usepackage{blindtext}

\begin{document}
\blindtext
\begin{figure}[hbtp]
\centering
\subfigure[c]{.4\linewidth}
\centering
\begin{tikzpicture}[scale=0.6]
\begin{axis}[]
\end{axis}
\end{tikzpicture}
\caption{Left figure}
\label{fig:Left}
\endsubfigure
\subfigure[c]{.4\linewidth}
\centering
\begin{tikzpicture}[scale=0.6]
\begin{axis}[]
\end{axis}
\end{tikzpicture}
\caption{Right figure}
\label{fig:Right}
\endsubfigure
\caption{Left and right figures side by side}
\label{fig:Group}
\end{figure}
\blindtext
\end{document}


• I forgot about the scale option. However, doesn't this scale down the ticks' texts either? Anyways, I did not want to resize. Nov 16, 2013 at 11:23
• @nobody nobody but you: You can adjust my code above. It is really the recommended solution. Nov 16, 2013 at 11:26
• What do you mean by this? It is the recommended solution to resize? Or it is recommended to use subcaption? Or both? Nov 16, 2013 at 11:30
• @nobody: To use the subcaption. Resizing is your own decision to meet your preferences. Nov 16, 2013 at 11:32

Try to use scale option for tikzpicture as shown in code below:

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}
\begin{figure}
\centering
\subfloat[]{
\begin{tikzpicture}[scale=0.7]
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\subfloat[]{
\begin{tikzpicture}[scale=0.7]
\begin{axis}[]
\end{axis}
\end{tikzpicture}
}
\end{figure}
\blindtext
\end{document}


• The OP never said that resize is an option (but I asked for it in a comment). ;) You should delete your answer until that time (because it is misleading).
– masu
Nov 15, 2013 at 20:58
• I do not have the intention to resize but to center the graphs horizontally on the page. Nov 16, 2013 at 11:25