1

I need to add a subcaption at the side of each plot within a \groupplots environment. After some searching I found this topic here on Tex.SE which shows how to add a subcaption to groupplots. I changed this code a bit to display the caption at the side of the plot.

The problem is, that the node needs to have a text width. Without this parameter, TikZ will yell at you. When setting a text width, however, you have to tweak the values to match the design you are going for.

In my case, the subcaption should display some numbers. As these numbers have a different number of digits, the result looks a bit weird. The cause is, that captions are always centred inside their box, in this case, the node with the given text width.

So my first question is what I could do, to left align the entire caption. I don't want to just left align the text inside the caption which can be achieved with justification = raggedright, but left align the entire block.

If this is managed, a second issue is waiting ;)

If you look at my example, you will see, that the subcaption brings some paddings with it. These paddings do not allow me, to vertically centre the node... So the second question is how to remove the subcaption padding only on the subcaptions in this plot.

MWE

\documentclass{article}

\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}

\pgfplotsset{compat=1.14}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure}[htb!]
    \centering
    \begin{tikzpicture}
        \begin{groupplot}[group style = {
            group size = 1 by 2,
            xlabels at = edge bottom,
            xticklabels at = edge bottom,
            vertical sep = 4pt
        },
        clip = false]

            \nextgroupplot
            \node [text width = 5em, anchor = north west, draw = red] at (rel axis cs: 1.01, 0.5)
                {\subcaption{Eggs \label{fig:a}}};
            \addplot plot coordinates {(0,0) (1,1) (2,2) (3,3)};

            \nextgroupplot
            \node [text width = 5em, anchor = north west, draw = red] at (rel axis cs: 1.01, 0.5) 
                {\subcaption{Bacon \label{fig:b}}};
            \addplot plot coordinates {(0,0) (1,1) (2,4) (3,9)};

        \end{groupplot}
    \end{tikzpicture}
    \caption{Caption of figure}
\end{figure}

Figure \ref{fig:a} and \ref{fig:b}

\end{document}
1

I have a rather simple proposal for your first problem but none for your second, also because I do not fully understand what's the problem. (Can't you just set the anchor to 170, say?) Just define a node style that measures the width of the caption and sets the text width accordingly.

\documentclass{article}

\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}

\pgfplotsset{compat=1.14}
\usepgfplotslibrary{groupplots}
\tikzset{subcaption/.style args={#1 with label #2}{text
width=width("#1")+0.7cm,node contents={\subcaption{#1 \label{#2}}},anchor =170}}
\begin{document}

\begin{figure}[htb!]
    \centering
    \begin{tikzpicture}
        \begin{groupplot}[group style = {
            group size = 1 by 2,
            xlabels at = edge bottom,
            xticklabels at = edge bottom,
            vertical sep = 4pt
        },
        clip = false]

            \nextgroupplot
            \node[subcaption={Eggs with label fig:a},draw =
            red,at={(rel axis cs: 1.01, 0.5)}];
            \addplot plot coordinates {(0,0) (1,1) (2,2) (3,3)};
            \nextgroupplot
            \node[subcaption={Bacon and Ham with label fig:b}, 
            draw = red,at={(rel axis cs: 1.01, 0.5)}];
            \addplot plot coordinates {(0,0) (1,1) (2,4) (3,9)};

        \end{groupplot}
    \end{tikzpicture}
    \caption{Caption of figure}
\end{figure}

Figure \ref{fig:a} and \ref{fig:b}
\end{document}

enter image description here

  • Thanks for this solution for the first problem. I kind of like it, even if I think that a simple left align-solution should be possible. If not, the LaTeX team should implement one :-P – Sam Sep 11 '18 at 21:26
  • To the second problem: As you see in my (and in your) example, the node is taller than the text inside, so the node positioning positions the node in the vertical centre of the graph, but not the text. This is because the padding on the top is larger than the one on the bottom and thus, the text isn't centred vertically in the node. If I could remove the padding on the top and on the bottom, or adjust them to the same value, I would be able to vertically centre the text (not the node). Does that explain, what I'm aiming for? – Sam Sep 11 '18 at 21:29
  • 1
    @Sam I am not in the LaTeX team and it is quite possible that there are simpler ways. I did not touch \subcaption at all since I am afraid that any change may backfire at other places. However, if you know how much larger the upper margin is than the lower margin, a simple yshift of the nodes will do the job. – user121799 Sep 11 '18 at 21:40
  • 1
    Great idea! yshift = 1.5pt did the trick. Thanks a lot!! – Sam Sep 11 '18 at 21:51

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.