1

I have some tikzpicture that contains minipage. It works as intended, however I am not able to move the caption directly under the image (the image itself is on the desired location). How to achieve this?

Minimalized example below:

% !TeX program = xelatex
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{caption}
\usepackage{adjustbox}
\begin{document}

\begin{figure}[h!]
    \sbox0{\begin{tikzpicture}
            \noindent\begin{minipage}{0.3\textwidth}
                
                \node [
                above right,
                inner sep=0] (image) at (0,0) 
                {\includegraphics[width=\linewidth,height=7.6cm]{example-image-duck}};
                
                \begin{scope}[
                    x={($0.1*(image.south east)$)},
                    y={($0.1*(image.north west)$)}]
                    \draw[latex-, thick,blue,align=center] 
                    (4.1,6.35) -- (5.4,7.5)
                    node[right,blue,font=\small \bfseries]{1};
                    
                \end{scope}          
            \end{minipage}%
            
    \end{tikzpicture}}%
    \adjustbox{trim={0cm 0cm 0cm 0cm},clip}{\usebox0}
    \captionsetup{width=0.3\textwidth}
    \caption{Front}
    \label{1}
\end{figure}

\end{document}

What I get is seen on picture below.

enter image description here

4
  • 3
    What exactly do you mean by Move caption under the picture, it is under the picture. But you haven't centered your float so the image is left aligned and the caption is the default centered. Try adding \centering just after \begin{figure}[h!].
    – daleif
    Nov 3, 2021 at 9:35
  • 1
    Note I'd use at least \begin{figure}[htp!], just in case the float cannot be placed here about
    – daleif
    Nov 3, 2021 at 9:36
  • Hi, the image itself is on the desired location, what I meant was that I want to move (put) the caption under the image
    – t387
    Nov 3, 2021 at 9:42
  • I made some edits in the post to remove ambiguity.
    – t387
    Nov 3, 2021 at 10:11

2 Answers 2

3

I don't know the intention of this code, but I think it's better to apply the minipage into the figure and not into the tikzpicture. This way the caption can also be inserted into the minipage

% !TeX program = xelatex
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{caption}
\usepackage{adjustbox}
\begin{document}

\begin{figure}[h!]
    \sbox0{\begin{tikzpicture}
%            \noindent\begin{minipage}{0.3\textwidth}
%                
                \node [
                above right,
                inner sep=0] (image) at (0,0) 
                {\includegraphics[width=\linewidth,height=7.6cm]{example-image-duck}};
                
                \begin{scope}[
                    x={($0.1*(image.south east)$)},
                    y={($0.1*(image.north west)$)}]
                    \draw[latex-, thick,blue,align=center] 
                    (4.1,6.35) -- (5.4,7.5)
                    node[right,blue,font=\small \bfseries]{1};
                    
                \end{scope}          
%            \end{minipage}%
%            
    \end{tikzpicture}}%
    \noindent\begin{minipage}{.3\textwidth}
    \adjustbox{trim={0cm 0cm 0cm 0cm},clip, width=\textwidth, height=7.6cm}{\usebox0}
%    \captionsetup{width=0.3\textwidth}
    \caption{Front}
    \label{1}
    \end{minipage}
\end{figure}

\end{document}

enter image description here

1
  • Thank you, it works as desired. I inserted some text on the right side of image (not shown in this question), which is why I used minipage in the first place.
    – t387
    Nov 3, 2021 at 10:46
1

A slight variation using two minipages and two saveboxes.

There are other ways to adjust the bounding box for a tikzpicture than \adjustbox. You can use \pgfinteruptboundingbox to ignore certain steps. You can use \pgfresetboundingbox and only include certain coordinates in a \path after.

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{caption}
\usepackage{adjustbox}
\begin{document}

\begin{figure}[h!]
    \sbox0{\begin{tikzpicture}
            \noindent\begin{minipage}{0.3\textwidth}
                
                \node [
                above right,
                inner sep=0] (image) at (0,0) 
                {\includegraphics[width=\linewidth,height=7.6cm]{example-image-duck}};
                
                \begin{scope}[
                    x={($0.1*(image.south east)$)},
                    y={($0.1*(image.north west)$)}]
                    \draw[latex-, thick,blue,align=center] 
                    (4.1,6.35) -- (5.4,7.5)
                    node[right,blue,font=\small \bfseries]{1};
                    
                \end{scope}          
            \end{minipage}%
            
    \end{tikzpicture}}%
    \sbox1{\adjustbox{trim={0cm 0cm 0cm 0cm},clip}{\usebox0}}% measure width
    \begin{minipage}{\wd1}
      \usebox1
      \caption{Front}
      \label{1}
    \end{minipage}
\end{figure}
\end{document}
2
  • Hi, I finally checked this solution. It is working after I of course add \end{document} and after I change \width=\wd1 with just \wd1.
    – t387
    Nov 11, 2021 at 13:19
  • Oops. Sorry about that. Nov 11, 2021 at 14:25

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.