1

I would like to use tikz in combination with graphicx and subcaption to achieve automatically labeled subfigures that I can refer to in text. Here is my MWE:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}[t]{0.49\linewidth}
    \phantomcaption{}\label{thisfig}
    \begin{tikzpicture}
      \node[rectangle, inner sep=0] (image) {\includegraphics[width=\linewidth]{example-image-a}};
      \node[below right = 0cm and 0cm] (label) at (image.north west) {\textbf{\subref{thisfig}}};
    \end{tikzpicture}
  \end{subfigure}
  \begin{subfigure}[t]{0.49\linewidth}
    \phantomcaption{}\label{thatfig}
    \begin{tikzpicture}
      \node[rectangle, inner sep=0] (image) {\includegraphics[width=\linewidth]{example-image-b}};
      \node[below right = 0cm and 0cm] (label) at (image.north west) {\textbf{\subref{thatfig}}};
    \end{tikzpicture}
  \end{subfigure}
\end{figure}

\end{document}

The result looks like this: MWE output with misaligned labels

If you look closely, you will notice that the labels a and b are vertically misaligned. The top ends of the characters are aligned, resulting in the b to be visually too low. How can I align the labels like in regular text a b, without breaking the structure of two subcaptions with one image each?

3
  • 2
    Try adding anchor=base to the options of the node with the label, or add a \strut in the node text (i.e. {\strut\textbf{\subref... Sort of same basic problem as for example tex.stackexchange.com/questions/235086 I think. Aug 6 at 13:52
  • anchor=base has no effect for me, but \strut seems to fix my problem. Thanks!
    – Quappas
    Aug 6 at 14:22
  • 1
    Sorry, forgot to mention: positioning options like below right also set the anchor, so you need to do below right=..., anchor=... in that order. Try e.g. \node[below=2.5ex, anchor=base west] ... Aug 6 at 14:27

0

Your Answer

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

Browse other questions tagged or ask your own question.