1

I want to keep an image and a word about it, right next to it, inside a node. I want to align the text in the middle (i.e. it should be on the line connecting the midpoints of the vertical sides of the rectangular node). But the text is moving to the bottom right corner. How can I fix it?

% code for generating the image shown
\documentclass[tikz]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]
\usetikzlibrary{positioning}
\usetikzlibrary{shadows.blur}

\begin{document}

\begin{tikzpicture}

\node[draw=black, thick] (balls) {\includegraphics[scale=0.05]{sample.jpg} Balls};

\end{tikzpicture}

\end{document}

Result:

The text is at the bottom right corner instead of the center line

2 Answers 2

3

Welcome! One of the many ways to get something like this is

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}
\node (duck) {\includegraphics[width=4cm]{example-image-duck} };
\node[right=1em of duck] (balls) {Balls};
\node[draw,thick,fit=(duck)(balls)]{};
\end{tikzpicture}
\end{document}

enter image description here

or

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw,thick] (balls) 
{$\vcenter{\hbox{\includegraphics[width=4cm]{example-image-duck}}}\quad
\mbox{Balls}$};
\end{tikzpicture}
\end{document}
1
  • Thanks a lot !! I had the first solution in my mind but this node would part of a larger diagram and I wanted to keep minimal clutter. I used the second solution. It is also very elegant. Apr 28, 2020 at 21:19
3

Following solutions uses tikzpicture's background rectangle to draw the border and a label to print the text.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[
    background rectangles/.style={thick, draw},
    show background rectangle]
    \node[label=right:Frog]{\includegraphics[width=3cm]{frog}};
\end{tikzpicture}
\end{document}

enter image description here

Update If this set should be part of a larger diagram (see comment to Schrödinger's cat answer), another solution could be a matrix.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, draw, nodes={anchor=center}] (A)
    {\includegraphics[width=3cm]{frog} & Frog 1\\};

\matrix[matrix of nodes, draw, nodes={anchor=center}, right= of A] (B)
    {\includegraphics[width=3cm]{frog} & Frog 2\\};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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