8

Please how to horizontally center-justify the following array of figures with the subfig package ?

\documentclass[11pt]{article}
\usepackage[dvips]{graphicx}
\usepackage{subfig}

\begin{document}

\begin{figure}[htp]
  \centering
    \subfloat[From $n=0$ to $n=-1$]{\label{fig:a}
    \includegraphics[scale=0.45]{Figures/IncludePyramid_a_reverse.eps}
    }                
  \qquad\qquad\qquad
    \subfloat[From $n=-1$ to $n=0$]{\label{fig:aa}
    \includegraphics[scale=0.45]{Figures/IncludePyramid_a.eps}
  } \\
    \subfloat[From $n=-1$ to $n=-2$]{\label{fig:b}
    \includegraphics[scale=0.45]{Figures/IncludePyramid_b_reverse.eps}
    }                
  \qquad\qquad\qquad
    \subfloat[From $n=-2$ to $n=-1$]{\label{fig:bb}
        \includegraphics[scale=0.45]{Figures/IncludePyramid_b.eps}
  }
  \\
    \subfloat[From $n=-2$ to $n=-3$]{\label{fig:c}
    \includegraphics[scale=0.45]{Figures/IncludePyramid_c_reverse.eps}
    }                
  \qquad\qquad\qquad
    \subfloat[From $n=-3$ to $n=-2$]{\label{fig:cc}
        \includegraphics[scale=0.45]{Figures/IncludePyramid_c.eps}
  }    
  \caption{Random walk on the Pascal pyramid}
  \label{fig:bratelli}
\end{figure}

\end{document}

enter image description here

2
  • As a general note, providing driver options to graphicx, like the dvips in your case, is no longer required and also not recommended. Modern LaTeX versions don't have an issue detecting the correct driver themselves and stating a wrong driver will cause problems. – Martin Scharrer Jul 1 '13 at 10:35
  • Have a look at the adjustbox package, especially the center key, i.e. \begin{adjustbox}{center=<value>} .. \end{adjustbox}. Here a value of about .5\textwidth-5mm should be right. I will post a full answer later when I have more time. – Martin Scharrer Jul 1 '13 at 10:39
5

You have to work a bit harder. The following set of macros sets each subfloat in a zero width box, so adding filling space will do.

  1. The \csubfloat macro has the same syntax as \subfloat; beware, though, that images should not fill the whole line.

  2. The \centerhfill macro has an optional argument, default \quad to add a space in the center. If it's not enough, you can experiment with \centerhfill[\qquad].

In the example I have used the demo option to graphicx; don't use it yourself, but don't add dvips either.

The \fakeig macro is just for producing the test and you don't need it; just remove the lines and uncomment the \includegraphics ones.

\documentclass[11pt]{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}

\newcommand{\csubfloat}[2][]{%
  \makebox[0pt]{\subfloat[#1]{#2}}%
}
\newcommand{\centerhfill}[1][\quad]{\hspace{\stretch{0.5}}#1\hspace{\stretch{0.5}}}

% This is for the example
\newcommand{\fakeig}[2]{\includegraphics[width=#1,height=#2]{foo}}

\begin{document}

\begin{figure}[htp]
  \centering
  \hspace*{\fill}%
  \csubfloat[From $n=0$ to $n=-1$]{\label{fig:a}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_a_reverse.eps}%
    \fakeig{2cm}{2cm}%
  }\centerhfill
  \csubfloat[From $n=-1$ to $n=0$]{\label{fig:aa}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_a.eps}%
    \fakeig{2cm}{2cm}%
  }\hspace*{\fill}

  \hspace*{\fill}%
  \csubfloat[From $n=-1$ to $n=-2$]{\label{fig:b}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_b_reverse.eps}%
    \fakeig{3cm}{3cm}%
  }\centerhfill
  \csubfloat[From $n=-2$ to $n=-1$]{\label{fig:bb}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_b.eps}%
    \fakeig{3cm}{3cm}%
  }\hspace*{\fill}

  \hspace*{\fill}%
  \csubfloat[From $n=-2$ to $n=-3$]{\label{fig:c}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_c_reverse.eps}%
    \fakeig{4cm}{4cm}%
  }\centerhfill
  \csubfloat[From $n=-3$ to $n=-2$]{\label{fig:cc}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_c.eps}%
    \fakeig{4cm}{4cm}%
  }\hspace*{\fill}

  \caption{Random walk on the Pascal pyramid}
  \label{fig:bratelli}
\end{figure}

\end{document}

Note a generous usage of % to mask end-of-lines that act like spaces, which are not wanted.

enter image description here

Another option would be to include each \subfloat in a minipage as wide as half the \textwidth, but you have less control about the center spacing; this could be modified using a fraction of \textwidth, say \begin{minipage}{.8\textwidth} or similar.

\documentclass[11pt]{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}

% This is for the example
\newcommand{\fakeig}[2]{\includegraphics[width=#1,height=#2]{foo}}

\begin{document}

\begin{figure}[htp]
  \centering
  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=0$ to $n=-1$]{\label{fig:a}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_a_reverse.eps}%
    \fakeig{2cm}{2cm}%
  }
  \end{minipage}%
  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=-1$ to $n=0$]{\label{fig:aa}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_a.eps}%
    \fakeig{2cm}{2cm}%
  }
  \end{minipage}

  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=-1$ to $n=-2$]{\label{fig:b}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_b_reverse.eps}%
    \fakeig{3cm}{3cm}%
  }
  \end{minipage}%
  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=-2$ to $n=-1$]{\label{fig:bb}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_b.eps}%
    \fakeig{3cm}{3cm}%
  }
  \end{minipage}

  \hspace*{\fill}%
  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=-2$ to $n=-3$]{\label{fig:c}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_c_reverse.eps}%
    \fakeig{4cm}{4cm}%
  }
  \end{minipage}%
  \begin{minipage}{.5\textwidth}\centering
  \subfloat[From $n=-3$ to $n=-2$]{\label{fig:cc}%
%    \includegraphics[scale=0.45]{Figures/IncludePyramid_c.eps}%
    \fakeig{4cm}{4cm}%
  }
  \end{minipage}

  \caption{Random walk on the Pascal pyramid}
  \label{fig:bratelli}
\end{figure}

\end{document}
3
  • Great work! Though Mico's solution is easier, I accept yours because it only uses the subfig package. – Stéphane Laurent Jun 30 '13 at 13:08
  • Looks like a good case for the center key of adjustbox to me. – Martin Scharrer Jul 1 '13 at 10:40
  • @MartinScharrer Are you referring to the definition of \csubfloat? I don't want the box to have a width. – egreg Jul 1 '13 at 10:52
6

Instead of using the subfig package and its \subfloat macro, you may want to use the subcaption package and its subfigure environment. Specifically, you could specify six subfigure environments of a given fixed width of, say, 0.4\textwidth and use \centering instructions within each of these environments.

Not having access to your image files, it's not possible to guarantee that the following code will work without a need for further tweaking. In particular, you may need to change the widths of the subfigure environments to accommodate the graphs in the third row of subfigures.

\documentclass[11pt]{article}
\usepackage[demo]{graphicx} % omit 'demo' option in real document
\usepackage{subcaption} % for 'subfigure' environment
\begin{document}
\begin{figure}
\centering

    \begin{subfigure}{0.4\textwidth}
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_a_reverse.eps}
       \caption{From $n=0$ to $n=-1$}\label{fig:a}
    \end{subfigure}          
    \qquad\qquad\qquad  % spacing between the subfigures
    \begin{subfigure}{0.4\textwidth}  
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_a.eps}
       \caption{From $n=-1$ to $n=0$}\label{fig:aa}
    \end{subfigure}    

    \begin{subfigure}{0.4\textwidth}
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_b_reverse.eps}
       \caption{From $n=-1$ to $n=-2$}\label{fig:b}
    \end{subfigure}          
    \qquad\qquad\qquad  % spacing between the subfigures
    \begin{subfigure}{0.4\textwidth}  
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_b.eps}
       \caption{From $n=-2$ to $n=-1$}\label{fig:bb}
    \end{subfigure}    

    \begin{subfigure}{0.4\textwidth}
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_c_reverse.eps}
       \caption{From $n=-2$ to $n=-3$}\label{fig:c}
    \end{subfigure}          
    \qquad\qquad\qquad  % spacing between the subfigures
    \begin{subfigure}{0.4\textwidth}  
       \centering
       \includegraphics[scale=0.45]{Figures/IncludePyramid_c.eps}
       \caption{From $n=-3$ to $n=-2$}\label{fig:cc}
    \end{subfigure}    

\caption{Random walk on the Pascal pyramid} \label{fig:bratelli}
\end{figure}
\end{document}
1
  • Excellent. Moreover the caption widths are very nice. – Stéphane Laurent Jun 30 '13 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.