3

I have the following abstracted code. I do not know why the text test is not centered. Do you know how to make it centered?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning, math, fit, backgrounds}

\begin{document}

\begin{tikzpicture}
\node [draw = black] {A TEST};
\coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
\coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
\node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red] {test};
\end{tikzpicture}

\end{document}

Result:

Please focus on the arrows I added manually. enter image description here

  • 1
    The title of your question seems to contradict what you want to achieve. I wonder if you want to change it to ""why is my text in a fit node not centered". – user121799 Apr 2 '18 at 4:14
4

This is the predefined behavior of TikZ, please see section 52 of the pgfmanual. You can place "test" in the center of the node by putting another node at the center of the MEM node.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, math, fit, backgrounds}


\begin{document}

\begin{tikzpicture}
\node [draw = black] {A TEST};
\coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
\coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
\draw (b) circle (3pt);
\node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red] {};
\node (test) at (MEM.center){test};
\end{tikzpicture}

enter image description here

2

In a "fited" node the text baseline is put at the center of the node. If you want your text to be vertically centered, you can use label=center:Test like this

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{fit}

\begin{document}
  \begin{tikzpicture}
    \node [draw = black] {A TEST};
    \coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
    \coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
    \node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red, label = center:Test]{};
  \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.