使用 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 指令產生的框/迷你頁的深度,並因此對框進行 y 移動(請參閱 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} 

在此輸入影像描述

相關內容