tikz - 如何重新定义锚点?

tikz - 如何重新定义锚点?

如何在 tikz 中重新定义图像锚点?例如,我希望将文本“左上”放置在图像的西北角,将“右上”放置在东北角。以下代码将它们都放置在西北角。

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{tikz}% loads graphicx

\begin{document}
\begin{tikzpicture}
    \node[anchor=north west,inner sep=0] (image) at (0,0) {\includegraphics{example-image-a}};
    \begin{scope}[anchor=north west]
        \node[draw,fill=white] at (0,0) {Top Left};
    \end{scope}
    \begin{scope}[anchor=image.north east]
        \node[anchor=north east,draw,fill=white] at (0,0) {Top Right};
    \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

但我希望它固定在图片的东北部

在此处输入图片描述

答案1

像这样?

在此处输入图片描述

为此,您不需要使用scope图形上角的节点,您可以在没有它们的情况下插入:

\documentclass{article}
\usepackage{tikz}% loads graphicx

\begin{document}
    \begin{tikzpicture}[
lbl/.style = {draw, outer sep=2\pgflinewidth, fill=white}
                        ]
\node[anchor=north west,inner sep=0] (image) {\includegraphics{example-image-a}};
%
\node[lbl,below right] at (image.north west) {Top Left};
\node[lbl,below left]  at (image.north east) {Top Right};
    \end{tikzpicture}
\end{document}

编辑: 或者定义标签锚点:

\documentclass{article}
\usepackage{tikz}% loads graphicx
    \begin{tikzpicture}[
lbl/.style = {draw, outer sep=2\pgflinewidth, fill=white, anchor=#1}
                        ]
\node[anchor=north west, inner sep=0] (image) {\includegraphics{example-image-a}};
%
\node[lbl=north west] at (image.north west) {Top Left};
\node[lbl=north east] at (image.north east) {Top Right};
    \end{tikzpicture}
\end{document}

结果和以前一样。

相关内容