Posicionamento absoluto de \caption com tikz

Posicionamento absoluto de \caption com tikz

Estou usando o tikz para exibir uma imagem em uma página e controlar com precisão sua posição (absoluta).

Agora quero anexar uma legenda a esta imagem e alinhar a linha de base (inferior) da minha legenda com a parte inferior da imagem.

Embora isso funcione perfeitamente se eu digitar manualmente a legenda em um nó tikz (consulte a página 1 do MWE), não consigo obter o mesmo resultado com a macro \caption. Na verdade, parece que o \caption está incorporado em uma espécie de minipágina e que só consigo alinhar a parte inferior da minipágina com a parte inferior da minha imagem.

Gostaria de continuar usando a macro \captionof porque o layout da minha página faz parte de um documento maior com outras figuras flutuantes, uma lista de figuras e numeração contínua de figuras.

Como posso obter o alinhamento adequado da linha de base \captionof?

\documentclass[a4]{article}
\usepackage{graphicx,lipsum}
\usepackage{adjustbox}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{backgrounds,positioning}
\usepackage{subcaption}

\renewcommand{\fboxsep}{0pt}
\setlength{\fboxrule}{0.1pt}
\newlength{\radius}
\setlength{\radius}{0.5mm}


% absolute positioning of a picture on a page
\newcommand{\mypict}{
    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \coordinate[] (P) at (current page.center);
        \node[anchor=center] at (P){
            \includegraphics[width=8cm]{example-image-a}
        };
        \coordinate (TL) at (current bounding box.north west); % top-left corner
        \coordinate (BL) at (current bounding box.south west); % bottom-left corner
        \coordinate (TR) at (current bounding box.north east); % top-left corner
        \coordinate (BR) at (current bounding box.south east); % bottom-left corner
        \coordinate[xshift=5cm] (B) at (current bounding box.south east); % bottom-left corner
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}
    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \filldraw[red] (TL) circle [radius=\radius];
        \filldraw[red] (BL) circle [radius=\radius];
        \filldraw[red] (TR) circle [radius=\radius];
        \filldraw[red] (BR) circle [radius=\radius];
        \draw[line width=0.5pt,red] (BR)--(B);
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}
}


\begin{document}

% FIRST PAGE
% manual caption
\mypict
\begin{tikzpicture}[remember picture, inner sep=0pt]
    \node[anchor=base west] at (BR){%
            \fbox{\parbox[b]{5cm}{\textbf{Figure 2.3~:} This is my first custom caption and the baseline is correctly aligned with the picture gqp.}}
    };
    \pgfresetboundingbox
    \path[use as bounding box] (0,0);
\end{tikzpicture}

% SECOND PAGE
% auto caption with \captionof
\clearpage
\mypict

\captionsetup{skip=0pt, textfont={sf,small}, labelfont={sf,small,bf}}

\begin{tikzpicture}[remember picture, inner sep=0pt]
    \node[anchor=base west] at (BR){%
            \fbox{\parbox[b]{5cm}{\captionof{figure}[list entry]{This is my first custom caption and the baseline is correctly aligned with the picture gqp.}}}
    };
    \pgfresetboundingbox
    \path[use as bounding box] (0,0);
\end{tikzpicture}

\end{document}

\parbox leva a um alinhamento correto da linha de base \captionof caixa delimitadora está alinhada na parte inferior

Responder1

Existe uma opção no captionpacote que minimiza o problema. Se eu passar a strut=offopção para o estilo da legenda, a caixa delimitadora agora ficará apertada ao parágrafo do texto da legenda.

Porém ainda não consigo medir a profundidade da caixa/minipágina gerada pelo comando captionof e consequentemente deslocar a caixa (ver página 3 do MWE).

A solução pode ser acionar uma "legenda fantasma" para obter o rótulo correto e a entrada da lista e imprimir o título da legenda separadamente em uma macro comum.

\documentclass[a4]{article}
    \usepackage{graphicx,lipsum}
    \usepackage{adjustbox}
    \usepackage{tikz}
    \usepackage{tikzpagenodes}
    \usetikzlibrary{backgrounds,positioning}
    \usepackage{subcaption}

    \renewcommand{\fboxsep}{0pt}
    \setlength{\fboxrule}{0.1pt}
    \newlength{\radius}
    \setlength{\radius}{0.5mm}


    % absolute positioning of a picture on a page
    \newcommand{\mypict}{
        \begin{tikzpicture}[remember picture, inner sep=0pt]
            \coordinate[] (P) at (current page.north);
            \node[anchor=north] at (P){
                \includegraphics[width=8cm]{example-image-a}
            };
            \coordinate (TL) at (current bounding box.north west); % top-left corner
            \coordinate (BL) at (current bounding box.south west); % bottom-left corner
            \coordinate (TR) at (current bounding box.north east); % top-left corner
            \coordinate (BR) at (current bounding box.south east); % bottom-left corner
            \coordinate[xshift=5cm] (B) at (current bounding box.south east); % bottom-left corner
            \pgfresetboundingbox
            \path[use as bounding box] (0,0);
        \end{tikzpicture}
        \begin{tikzpicture}[remember picture, inner sep=0pt]
            \filldraw[red] (TL) circle [radius=\radius];
            \filldraw[red] (BL) circle [radius=\radius];
            \filldraw[red] (TR) circle [radius=\radius];
            \filldraw[red] (BR) circle [radius=\radius];
            \draw[line width=0.5pt,red] (BR)--(B);
            \pgfresetboundingbox
            \path[use as bounding box] (0,0);
        \end{tikzpicture}
    }

    \makeatletter

    \caption@AtBeginDocument{%
        \def\rawcaptionof{\caption@teststar\caption@of{\caption*}\caption}%
    }

    \newcommand*\rawcaption@of[2]{\caption@settype*{#2}#1}

    \long\def\caption@@caption#1[#2]#3{

    \ifcaption@star \else
        \caption@prepareanchor{#1}{#2}%
        \memcaptioninfo{#1}{\csname the#1\endcsname}{#2}{#3}%
        \@nameuse{nag@hascaptiontrue}%
    \fi
    \par
    \caption@beginex{#1}{#2}{#3}%
        % \caption@setfloatcapt{%
        %   \caption@boxrestore
        %   \if@minipage
        %     \@setminipage
        %   \fi
        \caption@normalsize
        \ifcaption@star
            \let\caption@makeanchor\@firstofone
        \fi

        \@makecaption{\csname fnum@#1\endcsname}%
                    {\ignorespaces\caption@makeanchor{#3}}\par
        %   \caption@if@minipage\@minipagetrue\@minipagefalse
        %   }%
    \caption@end}

    \makeatother

    \begin{document}

    % FIRST PAGE
    % manual caption
    \mypict
    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \node[anchor=base west] at (BR){%
                \fbox{\parbox[b]{5cm}{\textbf{Figure 2.3~:} This is my first custom caption and the baseline is correctly aligned with the picture gqp.}}
        };
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}

    % SECOND PAGE
    % auto caption with \captionof
    \clearpage
    \mypict

    \captionsetup{skip=0pt, textfont={sf,small}, labelfont={sf,small,bf}}

    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \node[anchor=base west] at (BR){%
                \fbox{\parbox[b]{5cm}{\rawcaptionof{figure}[list entry]{This is my first custom caption and the baseline is correctly aligned with the picture gqp.}}}
        };
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}


    % THIRD PAGE
    % auto caption with \captionof
    \clearpage
    \mypict

    \captionsetup{skip=0pt, textfont={sf,small}, labelfont={sf,small,bf},strut=off}


    \newsavebox{\mybox}
    \savebox{\mybox}{\parbox[b]{5cm}{\rawcaptionof{figure}[list entry]{This is my first custom caption and the baseline is correctly aligned with the picture gqp.}}}

    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \node[anchor=base west,yshift=-\ht\mybox] at (BR){%
                \fbox{\usebox{\mybox}}
        };
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}

    \begin{tikzpicture}[remember picture, inner sep=0pt]
        \node[anchor=base east,yshift=-\dp\mybox] at (BL){%
                \fbox{\usebox{\mybox}}
        };
        \pgfresetboundingbox
        \path[use as bounding box] (0,0);
    \end{tikzpicture}


\end{document} 

insira a descrição da imagem aqui

informação relacionada