# TikZ draw node border on top of image

I have an image inside a TikZ node. There should be a border around the image, with no gap. The border should have a specified line width.

My problem is that the border seems to be drawn underneath the image. I use inner sep=0 to remove the gap between image and border. The effect is a border with half the intended line width, as demonstrated in the first example:

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}

% Red border to distinguish from example image, which also has a border
\begin{tikzpicture}
\node[draw=red, inner sep=0mm, line width=10mm] {\includegraphics{example-image-a}};
\end{tikzpicture}

% Shows that part of the border was hiding behind the image
\begin{tikzpicture}
\node[draw=red, inner sep=10mm, line width=10mm] {\includegraphics{example-image-a}};
\end{tikzpicture}

\end{document}


I can think of two workarounds:

1. Set the inner sep to half the border width. That way the border should have exactly the space it needs.
2. Keep inner sep=0 and set the line width to twice the intended value. Accept that the border is halved, therefore double it beforehand.

Is there a more elegant solution? Can we tell TikZ to draw the node border after the image? I could live with a small part of the image covered by the border.

Update after question was closed as duplicate:

The linked question has significant overlap and helps me in my specific situation. Good! However, the question whether it's possible to draw the border above the node content is not answered by the other question and remains open. The answer to this question could prove useful to people who want the node border drawn last for other reasons. The linked question may not be useful for them.

• What is happening is that the border of node is centred on the node boundary, so half is inside the node and half outside. The easiest solution is to set line width=20mm. Alternatively, you can use tex.stackexchange.com/questions/281989/….
– user30471
Sep 13 '20 at 5:42

At left image is inner sep=0.5\pgfplinewidth, at right one is  inner sep=0pt. At recent is added semitransparent option, that is better visible, that image is covered by inner half of nodes's border line.


\documentclass[margin=5mm]{standalone}
\usepackage{duckuments}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (n1) [draw=red, line width=10mm, semitransparent,
inner sep=0.5\pgflinewidth] % <---
{\includegraphics{example-image-duck}};
\node (n2) [draw=red, line width=10mm, semitransparent,
inner sep=0, % <---
right=of n1]
{\includegraphics{example-image-duck}};
\end{tikzpicture}
\end{document}


• You implement workaround 1, which I ended up using. However, I will not accept this answer because it does not answer the question whether there is a more elegant solution, and in particular if we can draw the node border on top of/after the image. I am a little nit-picky here, because the question had already been closed as a duplicate. It was reopened after I remarked that the question (draw node border on top of image) had not been answered. Sep 14 '20 at 19:41
• @snwflk Then I'm afraid I don't quite understand what you want in the end. Should the border line only be above the node content? Maybe you can add a simple picture (PAINT etc.), which shows what it should be in the end.
– cis
Sep 14 '20 at 20:06
• @snwflk, sory, but the first senteece in your question is "I have an image inside a TikZ node. There should be a border around the image, with no gap. The border should have a specified line width.". And my answer show you a solution for this and why your approach not work. Now I'm confused, what is your problem ,,, Sep 14 '20 at 20:48

I would use a tcolorbox for that: The problem of overlapping of lines should already be factored in there.
If any TikZ material is required within the picture, this is also possible with tcolorbox.

%\documentclass[tikz,margin=10pt]{standalone}
\documentclass[a4paper]{article}
\usepackage[showframe]{geometry}
\usepackage[most]{tcolorbox}
\begin{document}
\tcbset{sharp corners,
NoGaps/.style={boxsep=0mm, left=0pt, right=0pt, top=0pt, bottom=0pt, before skip=0pt, after skip=0pt,},
}

\begin{tcolorbox}[NoGaps,
colframe=red, boxrule=10mm,
hbox, % box as wide as the content
]
\includegraphics{example-image-a}
\end{tcolorbox}
\end{document}

• This seems like a good additional workaround! However, I will not accept this answer because it does not answer the question whether there is a more elegant solution, and in particular if we can draw the node border on top of/after the image. I am a little nit-picky here, because the question had already been closed as a duplicate. It was reopened after I remarked that the question (draw node border on top of image) had not been answered. Sep 14 '20 at 19:50