動作例

動作例

いくつかのカスタム シェイプを使用したところ、シェイプの境界ボックスの計算でテキスト ラベルが考慮されないことがわかりました。境界ボックスを生成するには、何らかの (目に見えない) シェイプを描画する必要がありますか、それともそれを直接指定する方法がありますか?

以下は、問題を示す最小限の例です。テキストがフレーム内に収まるように、形状を適切に定義する必要があります。

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

2つを比較すると、結果は次のようになります。

ここに画像の説明を入力してください

答え1

汚い仕事をしたくない場合は、\inheritsavedanchorsと を使用できます。\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}

関連情報