使用 tikz 对 \caption 进行绝对定位

使用 tikz 对 \caption 进行绝对定位

我正在使用 tikz 在页面上布置图像并精确控制其(绝对)位置。

我现在想为该图像附加一个标题,并将标题的(底部)基线与图片底部对齐。

虽然如果我在 tikz 节点内手动排版标题,这种方法可以完美运行(参见 MWE 第 1 页),但使用 \caption 宏无法获得相同的结果。事实上,\caption 似乎嵌入在某种小页面中,我只能将小页面的底部与图像的底部对齐。

我想继续使用 \captionof 宏,因为我的页面布局是一个更大文档的一部分,其中包含其他浮动图形、图形列表和图形的连续编号。

如何获得 \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 导致正确的基线对齐 \captionof 边界框只是底部对齐

答案1

包中有一个选项caption可以最小化此问题。如果我将该strut=off选项传递给标题样式,封闭框现在会紧贴标题文本段落。

但是我仍然无法测量由 captionof 命令生成的框/小页面的深度,也无法因此对框进行 yshift(参见 MWE 第 3 页)。

解决方案可能是触发“幻影标题”以获取正确的标签和列表条目,并在通用宏中单独打印标题。

\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} 

在此处输入图片描述

相关内容