5

Subfloat captions are not usually aligned with their corresponding xlable in PGFplots. enter image description here enter image description here

One way is to manually move the captions horizontally to align them. For example, (copied from here)

\documentclass{article}
\usepackage{subfig}
\usepackage[demo]{graphicx}
\begin{document}
\begin{figure}
  \centering
  \captionsetup[subfigure]{oneside,margin={2cm,0cm}}
  \subfloat[]{\includegraphics[width=4cm]{test1}}
  \hspace{1cm}
  \subfloat[]{\includegraphics[width=4cm]{test1}}
  \caption{Caption text}
\end{figure}
\end{document}

However, this process would be tedious if there are too many figures in the document each requiring separate alignment.

Is there any possible way to automatically align subcaptions to (detect if any and) center to their x axis o PGFplots?

EDIT:

Using tikzscale to resize the figures.

\documentclass{article}
\usepackage{subfig}
\usepackage{pgfplots}
\usepackage{tikzscale}
\begin{document}
\begin{figure}
  \centering
  \captionsetup[subfigure]{oneside}
  \subfloat[]{ %
    \includegraphics[width=5cm,height=4cm]{plot1.tikz}
  }%
  \hspace{1cm}
  \subfloat[]{%
    \includegraphics[width=5cm,height=4cm]{plot2.tikz}
  }
  \caption{Caption text}
\end{figure}
\end{document}

where plot1.tikz is:

  \begin{tikzpicture}[]
  \begin{axis}[
    xlabel=$x$,
    ylabel={$f(x) = x^2 - x +4$}
  ]
  \addplot {x^2 - x +4};
  \end{axis}
  \end{tikzpicture}%

and plot2.tikz :

  \begin{tikzpicture}[]
  \begin{axis}[
    xlabel=$x$,
    ylabel=$\sin(x)$
  ]
  \addplot {sin(deg(x))};
  \end{axis}
  \end{tikzpicture}%

The result will be: enter image description here

1
  • 4
    That would be the wrong direction. TeX just sees a black box to be captioned and doesn't know what the content is. You need to go the other way namely centering the picture wrt to the axis not axis + ylabel.
    – percusse
    Sep 20, 2013 at 13:37

1 Answer 1

7

You can use the options trim axis left and/or trim axis right for tikzpicture:

\documentclass{article}
\usepackage{subfig}
\usepackage{pgfplots}

\begin{document}
\begin{figure}
  \centering
  \captionsetup[subfigure]{oneside}
  \subfloat[]{%
  \begin{tikzpicture}[trim axis left,trim axis right]
  \begin{axis}[
    xlabel=$x$,
    ylabel={$f(x) = x^2 - x +4$},
    width=5cm
  ]
  \addplot {x^2 - x +4};
  \end{axis}
  \end{tikzpicture}%
  }%
  \hspace{2cm}
  \subfloat[]{%
  \begin{tikzpicture}[trim axis left,trim axis right]
  \begin{axis}[
    xlabel=$x$,
    ylabel=$\sin(x)$,
    width=5cm
  ]
  \addplot {sin(deg(x))};
  \end{axis}
  \end{tikzpicture}%
  }
  \caption{Caption text}
\end{figure}
\end{document}

enter image description here

4
  • Thanks for the nice solution. Is there any way to align them when using tikzscale as well, where trim axis left or trim axis right are disabled?
    – M.Reza
    Sep 20, 2013 at 15:39
  • I am not familiar with tikzscale; is that an option for tikzpicture, or are you referring to the scale option? Can you please provide an example document using that option? Sep 20, 2013 at 16:23
  • tikzscale is a package which modifies \includegraphics to input tikzpicture pictures to occupy exactly the specified width and height (the whole picture including labels and titles). The documentation can be found <a href="ctan.org/pkg/tikzscale">Here</a>. For proper operation of tikzscale the options 'width',height,trim axis left` and trim axes right must be disabled. I edited the post to include an example.
    – M.Reza
    Sep 21, 2013 at 5:40
  • @M.Reza Pgfplots supports width and height keys so you don't need to use tikzscale pgfplots graphics (pure tikz pictures don't have that hence the tikzscale package). Supply it to the axis options and you are free to use trimming.
    – percusse
    Sep 21, 2013 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .