3

I have a tikzpicture with 2 nodes. One node is image and other node is axis label. I want to center the caption with respect to image node, rather than on the complete tikzpicture. Please help and below is my latex,

\begin{figure}
  \begin{minipage}[b]{0.5\textwidth}
    \centering
    \begin{tikzpicture}
      \node (image) at (0,0) {\includegraphics {}}
      \node (ylabel) at (-3.25,0) {$\frac{test}{test}$}
    \end{tikzpicture}
    \captionsetup{type=figure,font=footnotesize}
    \captionof{figure}{This is some junk text as caption for figure 1}
  \end{minipage}

  \begin{minipage}[b]{0.5\textwidth}
    \centering
    \begin{tikzpicture}
      \node (image) at (0,0) {\includegraphics {}}
      \node (ylabel) at (-3.25,0) {$\frac{test}{test}$}
    \end{tikzpicture}
    \captionsetup{type=figure,font=footnotesize}
    \captionof{figure}{This is some junk text as caption for figure 2}
  \end{minipage}
\end{figure}

enter image description here

3
  • Welcome to TeX.SE! Do I understand your problem correctly that you want to have the text Figure 3.8: This is some junk text as caption for figure 1 centered vertically and horizontally within the left image? Is the Figure 3.8: part also necessary? Jun 11 '16 at 16:33
  • Yes you are right. Figure 3.8: This is some junk text as caption for figure 1 should be horizontally centered to the left image. I wanted the caption of Figure 3.8 and 3.9 to be vertically aligned, that's why I use minipage and it works. Hence, there is no issue with vertical alignment. For your second question, Figure 3.8 is necessary for me
    – Selva
    Jun 11 '16 at 16:37
  • Moreover, I tried trim left but it didn't work :(
    – Selva
    Jun 11 '16 at 16:58
2

After the image node, add \useasboundingbox (image.south east) rectangle (image.north west);. Anything added after that will not be considered with respect to the bounding box of the tikzpicture.

To make room for the node with the fraction, I reduced the width of the minipages a bit, and added \hfill between them. Note also that as you are inside a figure environment, you do not need to use \captionof.

\documentclass{article}
\usepackage[demo]{graphicx}

\usepackage{tikz,caption}


\begin{document}
\begin{figure}
  \captionsetup{type=figure,font=footnotesize}
  \begin{minipage}[b]{0.45\textwidth}
    \centering
    \begin{tikzpicture}
      \node [inner sep=0pt] (image) at (0,0) {\includegraphics{}};
      \useasboundingbox (image.south east) rectangle (image.north west);
      \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
    \end{tikzpicture}
    \caption{This is some junk text as caption for figure 1}
  \end{minipage}\hfill
  \begin{minipage}[b]{0.45\textwidth}
    \centering
    \begin{tikzpicture}
      \node [inner sep=0pt] (image) at (0,0) {\includegraphics{}};
      \useasboundingbox (image.south east) rectangle (image.north west);
      \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
    \end{tikzpicture}
    \caption{This is some junk text as caption for figure 2}
  \end{minipage}
\end{figure}
\end{document}

enter image description here

1
  • Thx a lot for the solution :)
    – Selva
    Jun 11 '16 at 17:55
1

EDIT: Please have a look at Torbjørn's solution, I believe it is easier and cleaner than mine.


If you want to center the caption to the complete minipage/tikzpicture: Thats easy. Just add justification=centering to your \captionformat.

\documentclass{scrartcl}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}

\begin{document}
\begin{figure}
    \begin{minipage}[b]{0.5\textwidth}
    \centering
        \begin{tikzpicture}
            \node (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image}};
            \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
        \end{tikzpicture}
        \captionsetup{type=figure,font=footnotesize,justification=centering,format=plain}
        \captionof{figure}{This is some junk text as caption for figure 1}

        % Just for demo, you can remove these two lines
        \noindent\rule{1pt}{6pt}
        \noindent\rule{\textwidth}{1pt}
    \end{minipage}
    \begin{minipage}[b]{0.5\textwidth}
    \centering
        \begin{tikzpicture}
            \node (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image}};
            \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
        \end{tikzpicture}
        \captionsetup{type=figure,font=footnotesize,justification=centering,format=plain}
        \captionof{figure}{This is some junk text as caption for figure 2}

        % Just for demo, you can remove these two lines
        \noindent\rule{1pt}{6pt}
        \noindent\rule{\textwidth}{1pt}

    \end{minipage}
\end{figure}
\end{document}

enter image description here

The additional lines show that the captions are centered within the minipage.


If you want to center the caption to the "real" image - the grey boxes in my example - then you have to tell the caption where your image starts and ends. This can be accomplished with the margin={<left>,<right>} parameter for \captionsetup. I've done this here for my gray boxes with margin={1cm,0.4cm}, but it might be necessary to adjust those values for your image.

\documentclass{scrartcl}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}

\begin{document}
\begin{figure}
    \begin{minipage}[b]{0.5\textwidth}
    \centering
        \begin{tikzpicture}
            \node (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image}};
            \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
        \end{tikzpicture}
        \captionsetup{type=figure,font=footnotesize,justification=centering,margin={1cm,0.4cm},format=plain}
        \captionof{figure}{This is some junk text as caption for figure 1}
        \noindent\rule{1pt}{6pt}
        \noindent\rule{\textwidth}{1pt}
    \end{minipage}
    \begin{minipage}[b]{0.5\textwidth}
    \centering
        \begin{tikzpicture}
            \node (image) at (0,0) {\includegraphics[width=0.8\textwidth]{example-image}};
            \node (ylabel) at (-3.25,0) {$\frac{test}{test}$};
        \end{tikzpicture}
        \captionsetup{type=figure,font=footnotesize,justification=centering,margin={1cm,0.4cm},format=plain}
        \captionof{figure}{This is some junk text as caption for figure 2}
        \noindent\rule{1pt}{6pt}
        \noindent\rule{\textwidth}{1pt}

    \end{minipage}
\end{figure}
\end{document}

enter image description here

As you can see, the caption is now centered relative to the \included image.

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.