4

I would like to know how I can go about automatically centering the plots subcaptions in the standalone environment. This is what I would like:

enter image description here

Here is my code:

\documentclass[varwidth]{standalone}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usepackage{graphicx}
\usepackage{float}
\usepackage{subfig}
\usepackage{xcolor}
\definecolor{ocre}{RGB}{0,173,239} %  mybluei Blue color
\usetikzlibrary{pgfplots.groupplots, matrix, backgrounds}
\pgfplotsset{%
    compat=1.12,
    every tick label/.append style={font=\scriptsize},
    every axis plot/.append style={line width=0.8pt},
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
    }

\captionsetup[subfigure]{labelfont={color=ocre,scriptsize,it,bf},textfont={color=ocre,scriptsize,it,bf},labelformat=parens,labelsep=space}

\begin{document}
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\setcaptionsubtype
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=1 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=10cm
            ,height=5cm
            ,try min ticks=5
            ,xlabel={\bfseries{\emph{\footnotesize{Time (ms)}}}}
            ,ylabel={\bfseries{\emph{\footnotesize{Temperature (Deg C)}}}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot%
\addplot[cyan]{rand};\label{plots:ltone}

\nextgroupplot%
\addplot[cyan]{rand};\label{plots:lttwo}

\addplot [smooth,red,thick]{rand};

\end{groupplot}

\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Lock Time Response\label{subplot:ltone}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Zoomed-In Lock Time Response\label{subplot:lttwo}}};

\end{tikzpicture}
%\caption[]{This is a test Plot.}
\end{center}
\end{figure}
\end{document}
3
  • The node for the caption is centered, but the caption itself is a bit left aligned. Adding justification=centering to your \captionsetup fixes that, but I don't know if that will mess up any of your other subfigure captions. Jan 6 '16 at 6:19
  • @TorbjørnT. To limit the effect to this very figure one could place \captionsetup{justification=centering} right after \setcaptionsubtype.
    – user2574
    Jan 8 '16 at 10:21
  • @AxelSommerfeldt Thanks! I'll add an answer then. Jan 8 '16 at 10:24
5

As you can see by adding draw to the node options, the node containing the caption is positioned correctly, but the alignment of the caption inside the node is wrong.

enter image description here

The caption alignment can be changed by the justification key in \captionsetup, i.e. \captionsetup{justification=centering}. Presumably you don't want all the captions in the document centered, but by adding \captionsetup{justification=centering} right after \setcaptionsubtype it will be local to the tikzpicture (as mentioned by Axel Sommerfeldt in a comment).

One additional note: Neither \bfseries nor \footnotesize are commands that take an argument, the way \emph is used. They are both switches that affect the following text, and as such should be used as {\bfseries\footnotesize text}

Complete code and output:

enter image description here

\documentclass[varwidth,border=4mm]{standalone}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{subfig}
\definecolor{ocre}{RGB}{0,173,239} %  mybluei Blue color
\usetikzlibrary{pgfplots.groupplots, matrix, backgrounds}
\pgfplotsset{%
    compat=1.12,
    every tick label/.append style={font=\scriptsize},
    every axis plot/.append style={line width=0.8pt},
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
    }

\captionsetup[subfigure]{labelfont={color=ocre,scriptsize,it,bf},textfont={color=ocre,scriptsize,it,bf},labelformat=parens,labelsep=space}

\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\setcaptionsubtype
\captionsetup{justification=centering} % <-- added
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=1 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=10cm
            ,height=5cm
            ,try min ticks=5
            ,xlabel={\bfseries\footnotesize\emph{Time (ms)}}
            ,ylabel={\bfseries\footnotesize\emph{Temperature (Deg C)}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot%
\addplot[cyan]{rand};\label{plots:ltone}

\nextgroupplot%
\addplot[cyan]{rand};\label{plots:lttwo}

\addplot [smooth,red,thick]{rand};

\end{groupplot}

\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Lock Time Response\label{subplot:ltone}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Zoomed-In Lock Time Response\label{subplot:lttwo}}};

\end{tikzpicture}
\caption[]{This is a test Plot.}
\end{figure}
\end{document}

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.