工作範例

工作範例

使用一些自訂形狀,我發現計算形狀的邊界框時不考慮文字標籤。我是否需要繪製一些(不可見的)形狀來產生邊界框,或者是否有直接的方法來指定它?

下面是顯示問題的最小範例。我想要一個正確的形狀定義,以便文字保留在框架內。

\documentclass{article}

\usepackage{tikz}

\pgfdeclareshape{test}{
    \anchor{center}{\pgfpointorigin}
    \anchor{text}{\pgfpointorigin}
}

\begin{document}
\framebox{%
\begin{tikzpicture}
\draw node[test] {Some text};
\end{tikzpicture}
}
\end{document}

基本上我想複製以下內容

\framebox{%
\begin{tikzpicture}
\draw node[inner sep=0] {Some text};
\end{tikzpicture}
}

兩者比較結果如下:

在此輸入影像描述

答案1

如果您不想做骯髒的工作,您可以使用\inheritsavedanchorsand 。\inheritbackgroundpath

工作範例

\documentclass{article}

\usepackage{tikz}

\pgfdeclareshape{test}{
    \anchor{center}{\pgfpointorigin}
    \anchor{text}{\pgfpointorigin}
    \inheritsavedanchors[from=rectangle]
    \inheritbackgroundpath[from=rectangle]
}

\begin{document}
\framebox{%
\begin{tikzpicture}
\draw node[test] {Some text};
\end{tikzpicture}
}
\end{document}

相關內容