10

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.

The plots are shifted into the right margin

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.

  • 3
    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 '13 at 20:54
  • @masu: It seems that it doesn't works. I still get overfull warning! Maybe I do something wrong. – JardaFait Nov 15 '13 at 21:02
  • If resizing is an option, I'll submit it as answer. – masu Nov 15 '13 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. – Nobody moving away from SE Nov 16 '13 at 11:21

4 Answers4

14

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}

enter image description here

Daniel
  • 37,517
  • This feels a bit hacky. Could it interfere with other features? – Nobody moving away from SE Nov 16 '13 at 11:22
  • 1
    @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. – Daniel Nov 16 '13 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? – Nobody moving away from SE Nov 16 '13 at 12:38
  • @Nobody: I would do exactly this and also have done it a couple of times. – Daniel Nov 16 '13 at 23:27
4

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.

enter image description here

\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}%
    \addtolength{\boxwidth}{-\textwidth}%
    \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}
    \qquad
    \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}
masu
  • 6,571
2

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
    \qquad
    \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}

enter image description here

0

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}

tikzpicture scaling

JardaFait
  • 3,922