0

How do I redefine the image anchor in tikz? For example I would like the position the text "Top Left" on the north west corner of the image and the "Top Right" on the north east. The following code I have positions them both on the north west.

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

enter image description here

But I would like it anchored on the north east of the image

enter image description here

1 Answer 1

3

Like this?

enter image description here

For this you not need to use scope, nodes at upper corners of figure you can insert without them:

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

Edit: or with defining of labels anchors:

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

Result is the same as before.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .